Java, abstract classes starting from beginners

Introduction

This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ~~ ** The mistaken ** is the center. This is for the purpose of posting the mistaken part during coding and posting it for self-reflection ~~. In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.

environment

Language: Java11, JDK13.0.2 Operating environment: Windows 10

Class that cannot create an object

All of the classes I've dealt with so far have been created with the thought of after creating the object. However, this ** abstract class itself cannot create or instantiate an object **. This ** abstract class ** is a class that needs to "determine the content by considering creating a class", and "when creating a class that inherits this abstract class, please add only this function." Think about the content with that in mind.

About the abstract class and its methods ** methods that do not define the processing content **. The abstract class is characterized by having a method (abstract method) ** that has the ** ʻabstractqualifier and does not define the processing content on the spot. By prefixing theclass with ʻabstract, the abstract class is an abstract class. Can be declared as.

abstractClass.java


abstract class Wild
{
  protected int number;
  public void getNumber(int n)
  {
    number = n;
    System.out.println("This cat" + number + "This is the second cat.");
  }

  abstract void show();

}

As a caveat. Abstract methods are required in the abstract class, but ** concrete methods (methods without ʻabstract`) may be implemented, and concrete methods can be used as inherited methods in subclasses **.

Abstract methods set requirements

A subclass that inherits an abstract class must implement a concrete method by overriding it based on the abstract method with ** ʻabstract` **. Overriding means

  1. Same signature (method name, in this case the argument relationship waits for implementation)
  2. Same or looser access modifier
  3. The return type is the same in principle (excluding covariant return values)

It is necessary to meet this condition.

A method with the same name is required for the subclass, so if you don't understand the contents of the abstract class well, you will continue to throw errors.

HouseCat.java


class HouseCat extends Wild
{
  public void see(int num)
  {
    number = num;
    System.out.println("This cat" + number + "This is the second cat.");
  }
  //Wait a minute, show()Where is it?
}

in conclusion

You can use the ʻinstanceof` operator to find out if the variables in the ** 2 term belong to the same class **. Quoted from Java SE11 Silver Problem Collection (commonly known as Kuromoto).

a instanceof b true if a is an instance of the same class as b or a subclass of b

It is suitable for the check mechanism because it can be incorporated into the conditional branch in the main function.

There is a ** interface ** scheduled for the next time that is very similar to the abstract class we dealt with this time, but I would like to confirm these differences.

reference

I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.

Easy Java 7th Edition Java SE11 Silver Problem Collection (commonly known as Kuromoto)

Recommended Posts

Java, abstract classes starting from beginners
Java starting from beginners, logical operators / conditional operators
Java starting from beginner, override
Java, instance starting from beginner
Java starting from beginner, inheritance
Java life starting from scratch
About abstract classes in java
Java abstract methods and classes
Java overload constructor starting from beginner
Classes and instances Java for beginners
Java starting from beginner, variables and types
Java starting from beginner, nested / break / continue
Java, if statement / switch statement starting from beginner
Getting Started with Java Starting from 0 Part 1
JAVA learning history abstract classes and methods
Java, for statement / while statement starting from beginner
IntelliJ starting from 1
java (abstract class)
[java] abstract class
Programming beginners learn PHP from a Java perspective-variables-
Java starting from beginner, class declaration / object generation
Kantai Collection Java # 1 Classes and Objects [For Beginners]
For Java engineers starting Kotlin from now on
Java abstract modifier [Note]
Call Java from JRuby
Changes from Java 8 to Java 11
Sum from Java_1 to 100
java classes, instances, objects
Eval Java source from Java
[Java] About anonymous classes
Picture-in-picture starting from iOS14
Access API.AI from Java
Java development for beginners to start from 1-Vol.1-eclipse setup
From Java to Ruby !!
Loading classes from Path
[Java] Platforms to choose from for Java development starting now (2020)
java (classes and instances)
About java abstract class
[For beginners] Explanation of classes, instances, and statics in Java
Advanced inheritance abstract, interface -java
Migration from Cobol to JAVA
[Java] Beginner's understanding of Servlet-②
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
(Note) Java classes / variables / methods
[Java] Generics classes and generics methods
Creating ElasticSearch index from Java
New features from Java7 to Java8
[Java] Beginner's understanding of Servlet-①
Connect from Java to PostgreSQL
Object-oriented summary by beginners (Java)
Java learning memo (abstract class)
Java for beginners, data hiding
[Java] Classes, constructors, static members
Java beginners read Hello World
Using Docker from Java Gradle
From Ineffective Java to Effective Java
JavaScript as seen from Java
Java application for beginners: stream
Execute non-Java instructions from Java