A brief analysis of inheritance and combination in Java

  • 2020-05-26 09:16:09
  • OfStack

preface

Java is an object-oriented language. Everyone who has studied Java knows that encapsulation, inheritance, and polymorphism are the three characteristics of object orientation. Everyone is more or less under the impression that inheritance can help me achieve class reuse. As a result, many developers will naturally use class inheritance when they need to reuse some code, because that's what the books say (and that's what the teachers teach). But it's not the right thing to do. The heavy use of inheritance over a long period of time can impose high maintenance costs on the code.

Actually when I was first learning java didn't heard of combination the term, teachers are also more did not explain, I 1 straight thought is I fall off the knowledge points, in fact not, combination setting aside the noun it is defined as a thinking things, believe that readers are exposed to, but don't know it also has the name.

In fact, the so-called combination is to create a new class to call the already created and debugged class, so the new class can call it a combination

So let's say I create an 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;
 }
}

So now I'm going to use this class, and I can add some new features to this class, and then we can create a new class, and then we can create People objects in this class.

So let's say I now create a class called Student


class Student {
 People people = new People();
}

You can then add some properties to the class, such as defining an identity as student, which I won't discuss here.

Now let's look at inheritance, because inheritance and composition are the same thing, so let's look at how inheritance works.

In java there is a keyword called extends, which helps us inherit, the inherited class we call the parent class, we call it the base class, we call it the superclass, and the successor we call it the subclass or the derived class, and so on

Here we define a class


public class Student extends People{
 //doSomething
}

In this case, the class inherits all of the parent class's member methods and member variables, but note that fields or methods declared as private permissions are not inherited.

To prove this point 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", which shows that the field or method declared as private cannot be inherited. If you want to inherit it, you can change private to protected, so that we can inherit the name field smoothly.

So we initialize all the fields in People by 1, and we just add this block of code to the code


{
 this.age = 10;
 this.name = "zhangsan";
}

Then declare student in the main function


Student student = new Student();
System.out.println(student.getAge());

Now we are surprised to find that this is possible, even though we have not declared any fields or methods in the subclass, we can still call getAge(); And you can print out 10 without a hitch

This is because we have not overloaded any methods in the subclass, so we are calling getAge of the parent class, which makes it easy to access the fields declared by private of the parent class.

After reading this, I believe that you have a preliminary understanding of inheritance, then in the inheritance mechanism, how is the constructor of each class called? The answer is from the parent class to the child class in turn.

At the same time, I declare three classes Temp1, Temp2 and Temp3. Meanwhile, Temp3 inherits from Temp2, and Temp2 inherits from Temp1. In this way, we put a printed sentence in the constructor of each class


//Temp1
System.out.println("i'm temp1");
//Temp2
System.out.println("i'm temp2");
//Temp3
System.out.println("i'm temp3");

I'm going to do that for convenience here, but you have to be careful that these are in the constructor of the class.

We create an Temp3 object in the main function


public class Mian{
 public static void main(String[] args){
 Temp3 temp3 = new Temp3();
 }
}

We look at the console and it prints out


i'm temp1
i'm temp2
i'm temp3

You can see that you want to create an temp3 object, find the extends keyword, then go up the inheritance chain, find temp2, find the extends keyword, then find temp1, then call the temp1 constructor, and then call it one by one.

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: