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.
Language: Java11, JDK13.0.2 Operating environment: Windows 10
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 the
class 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 **.
A subclass that inherits an abstract class must implement a concrete method by overriding it based on the abstract method with ** ʻabstract` **. Overriding means
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?
}
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.
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