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
Last time I dealt with the relationship between superclasses and subclasses.
The subclass inherits the superclass method, but sometimes you want to define another process while using the same method name.
For example, when preparing a method for checking members with show 〇〇 ()
. Even if you check the same, you will be able to use watch 〇 〇 (), see 〇 〇 (), check 〇 〇 ()
properly. However, different arguments and types make it very difficult to remember and use, and the unnecessarily long code reduces readability.
In the subclass, you can define a new method that is exactly the same as the one implemented in the ** superclass, and when calling from an object, the subclass takes precedence. ** **
As a condition,
The fact that subclass methods act on behalf of superclass members is called ** overriding **.
As I write many times, subclasses inherit superclass members. And by overriding, you can write a process dedicated to the subclass and make it function preferentially. Therefore, when using a subclass that is a child of the same superclass, the processing can be described collectively by the method of the superclass.
arrayCatsShow.java
class WildCat
{
protected int number;
protected double weight;
public WildCat()
{
number = 1;
weight = 1.0;
}
public void getCat(int n,double w)
{
number = n;
weight = w;
System.out.println(number +"The weight of the second cat" + weight + "It is kg.");
}
public void show()
{
System.out.println("This cat" + number + "This is the second cat.");
System.out.println("The weight of this cat" + weight + "It is kg.");
}
}
Based on this WildCat
class, prepare HouseCat
and MyCat
classes.
arrayCatsShow.java
class HouseCat extends WildCat
{
public void show()
{
System.out.println("This domestic cat" + number + "It is the second." );
}
}
class MyCat extends WildCat
{
public void show()
{
System.out.println("Of these cats" + number + "The current weight is" + weight + "was." );
}
}
Then prepare an individual cat.
arrayCatsShow.java
class arrayCatsShow
{
public static void main(String[] args)
{
WildCat[] cats = new WildCat[4];
cats[0] = new WildCat();
cats[0].getCat(1, 5.2);
cats[1] = new HouseCat();
cats[1].getCat(2, 4.2);
cats[2] = new MyCat();
cats[2].getCat(3, 5.6);
cats[3] = new MyCat();
cats[3].getCat(4, 8.4);
for(int i=0; i< cats.length; i++){
cats[i].show();
}
}
}
Since we prepared an array of superclasses and then new
individually, sentences based on eachshow ()
definition appear.
When you want to process superclass + subclass.
HouseCatShow.java
class HouseCat extends WildCat
{
public void show()
{
super.show();
System.out.println("This domestic cat" + number + "It is the second." );
}
}
You don't have to write everything again because you can bring the superclass processing as it is with super.method name
. This is also true for fields, so you can also bring in superclass values with super.field
.
By the way, if you don't want to override the superclass method, you can add ** final
likepublic final void show ()
and you can't override it **.
This can also be done with the class name and field, so if it's a final class
, it's ** a subclass that can't be extended , and if it's a final field
, the value of the ** field doesn't change except for initialization at declaration. constant) `.
I found the word ** covariant return value ** in the Silver problem book, but I'm going to examine the class library soon, so I'd like to clarify the relationship between the classes before dealing with it.
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