Postscript 2020/3/16 I corrected it because I was pointed out in the comments that there was a mistake in the way of naming the class.
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 part that was actually mistaken 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 the classes I've written so far have been written independently, but I had to set members (fields and methods) each time. If it's about the Cat
class, you can make it if you do your best, but it's not realistic to write every class with a huge number of members (fields and methods) every time.
Java has a ** class extend ** function to create your own class while inheriting the members of the existing class.
extendedHousecat
class WildCat
{
protected double weight;
protected double height;
protected String name;
public static int sumCats = 0;
public wildCat()
{
weight = 0.0;
height = 0.0;
sumCats++;
System.out.println("Nyago, I found another cat.");
}
public WildCat(double we,double he)
{
weight = we;
height = he;
sumCats++;
System.out.println("Nyago, I found another cat.");
System.out.println("The cat I found is the weight" + weight + "kg, height" + height + "It looks like cm.");
}
public void measureWeightHeight(double w,double h)
{
weight = w;
height = h;
System.out.println("I measured this cat.");
System.out.println("Weight" + weight + "kg, height" + height + "There is cm.");
}
public static void countCats()
{
System.out.println("In all" + sumCats + "I found a cat.");
}
}
//Wildcat(Wildcat)Based on the class of(Housecat)Let's make a class.
class HouseCat extends WildCat
{
private String collorColor;
private String nickname;
public void callNickname(String s)
{
nickname = s;
System.out.printlm("New to this cat" + nickname + "I gave it a nickname.");
}
}
The wildcat (WildCat
) class was extended to create a domestic cat (HouseCat, Cat
... there are various names) class. At this time, Wildcat
and HouseCat
have a parent-child relationship, which is super class
and sub class
, respectively.
The HouseCat
class, which is an extension of the WildCat
class, inherits the members (fields and methods) of the parent WildCat
class.
HouseCat cat1 = new HouseCat();
cat1.measureWeightHeight(5.0,50.5);
By doing so, you can create a HouseCat
object with a weight of 5.0 kg and a height of 50.5 cm.
Of course, it also has a collorColor and nickname fields,
You can also give it a nickname with the public void callNickname ()
method.
The HouseCat
class does not have a constructor, but if you do not provide a constructor in the sub class
, when the ** no-argument constructor ** of the super class
constructor creates a HouseCat
object Is automatically called to. This time it's public WildCat ()
.
However, for example, if you call it as super (8.0,60.8)
in the constructor, it will be called as a domestic cat of 8.0kg, 60.8cm.
Did you notice that the fields in the WildCat
class are different than usual? (Actually, I fixed it after writing so far). protected
is a modifier that means" accessible from the same class or subclasses that are children. "
If you can expect to become a superclass that you want to create and develop more and more subclasses, it will be easier to handle later if you set it to protected
.
As for access restrictions, we are refraining from packaging, but we will omit it here.
What the inherited subclass must have, and where do superclass members allow access? I'm still suffering from these problems, so the current situation is that my understanding here is still weak. The Java 11 Reference that I dealt with before also showed ʻextends` in a mess with just a little peek. There is. When you understand this family tree, you'll probably see the real Java.
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)
Java® Platform, Standard Edition & Java Development Kit Version 11 API Specification
Recommended Posts