Preface
Java is an object-oriented language. Everyone who has learned Java knows that encapsulation, inheritance, and polymorphism are the three characteristics of object-oriented. When everyone just learned inheritance, they would have more or less the impression that inheritance can help me realize class reuse. Therefore, many developers will naturally use class inheritance when they need to reuse some code, because this is what the book is written (this is what the teacher teaches). However, it is actually wrong to do this. Long-term large-scale use of inheritance will bring high maintenance costs to the code.
Actually, when I first learned Java, I never heard of the term combination, and the teacher never explained it. I always thought that I had lost some knowledge points, but it was not. The term combination should be defined as a thinking thing for the time being. I believe that readers have come into contact with it, but I don’t know that it has this name.
In fact, the so-called combination is to create a new class to call a class that has been created and debugged, so this new class can be called a combination
For example, I create a People
public class People { private String name; private int age; public void setName(String name){ this.name = name; } public String getName(){ return this.name; } public int getAge(){ return this.age; } public void setAge(int age){ this.age = age; }} Then I want to use this class now. I can add some new features to this class. At this time, we can create a new class and then create People's object in this class.
For example, I am now creating a class called Student
class Student { People people = new People();} Then you can add some attributes to this class, such as defining an identity as student, which I will not discuss here.
Next, let’s take a look at inheritance. In fact, inheritance and combination are similar. Let’s first look at how it is inherited.
There is a keyword called extends in Java, which can help us inherit. We call the inherited class parent class, base class, super class, and inheritors are all OK, and we call the inheritor subclass or derived class, etc.
Here we define a class
public class Student extends People{ //doSomething} In this way, the class inherits all member methods and member variables of the parent class, but note that fields or methods declared as private permission will not be inherited.
To prove this, we write a method in the student class
public String re(){ return this.name;}Here the compiler will report an error "People.name is not visible". From here, we can see that the fields or methods declared as private cannot be inherited. If you want to inherit it, you can change private to protected. In this way, we can inherit the name field smoothly.
In this way, we initialize all the fields in People, just add this code block to the code.
{ this.age = 10; this.name = "zhangsan";}Next, declare this student in the main function
Student student = new Student();System.out.println(student.getAge());
Now we were surprised to find that this writing method is feasible. Although we did not declare any fields and methods in the subclass, we can still call getAge(); and we can print out 10 smoothly
This is because we do not overload any method in the subclass, so we call the parent class getAge, so we can easily access the fields declared by the parent class private.
After reading these, I believe that everyone has a preliminary understanding of inheritance. So in the inheritance mechanism, how are the construction methods of each class called? The answer is to call them in sequence from the parent class to the child class.
While demonstrating, I first declare three classes Temp1, Temp2, and Temp3. At the same time, Temp3 inherits from Temp2, and Temp2 inherits from Temp1. In this way, we put a printed statement in the construction methods of each class.
//Temp1System.out.println("i'm temp1");//Temp2System.out.println("i'm temp2");//Temp3System.out.println("i'm temp3"); For the sake of convenience, I wrote this here, but everyone must pay attention to the fact that these are placed in the construction methods of each category.
We create a Temp3 object in the main function
public class Mian{ public static void main(String[] args){ Temp3 temp3 = new Temp3(); }}We looked at the console and printed it
i'm temp1i'm temp2i'm temp3
From this we can see that we need to create the object of temp3 first, find the extends keyword, then follow the inheritance chain, find temp2, and find the extends keyword, then find temp1, then call the constructor of temp1, and then call one by one.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.