Understanding the inheritance of Java's three features
Review previous blog examples( Understanding the encapsulation of Java's three features):
public class Husband { private String name; private String sex; private int age; private Wife wife; /*Omit getter(), setter()*/ }
public class Wife { private String name; private int age; private String sex; private Husband husband; /*Omit getter(), setter()*/ }
In this case, the two classes have a lot of common properties, and this kind of repeated code is not tolerated. So it needs to be implemented by inheritance at this time.
Home page, we leave the programming world, starting from the reality, there are not only husband and wife relationships in reality, as human beings have some basic attributes such as name, gender, etc., so it is undoubtedly a good way to extract these human public attributes. Let different subclasses implement these details through inheritance.
public class Person { protected String name; protected int age; protected String sex; }
public class Husband extends Person { private Wife wife; public void setWife(Wife wife) { this.wife = wife; } }
public class Wife extends Person { private Husband husband; public Husband getHusband() { return husband; } public void setHusband(Husband husband) { this.husband = husband; } }
After using inheritance, the amount of code for the two subclasses Husband and Wife is significantly reduced. In addition, the relationship between the three classes can be clearly seen.
In fact, the subclass is the specialization of the parent class. It has its own characteristics besides the characteristics of the parent class. At the same time, in the inheritance relationship, the child class can completely replace the parent class, otherwise it cannot. There are three sentences to remember when using inheritance:
- A subclass has properties and methods that are not private in the parent class
- Subclasses can have their own properties and methods, that is, subclasses can extend the parent class
- Subclass can override the method of the parent class
constructor
Subclasses can inherit the properties and methods of their parents. Except for those private ones, constructors cannot be inherited and can only be called. We can call the constructor in the parent class through the super keyword.
For subclasses, the correct initialization of their constructors is very important, but only if only one method can guarantee this: in the constructor, the parent class constructor is called to complete initialization, while the parent constructor has the ability to initialize the parent class.
public class Person { protected String name; protected int age; protected String sex; public Person(){ System.out.println("Person Constructor..."); } }
public class Husband extends Person { private Wife wife; public void setWife(Wife wife) { this.wife = wife; } public Husband(){ System.out.println("Husband Constructor..."); } }
public static void main(String[] args){ Husband husband = new Husband(); } //output: //Person Constructor... //Husband Construcotr...
It can be seen that the construction process of the subclass is spread out from the parent class. Although we do not call the constructor of the parent class explicitly, the compiler will call the constructor of the parent class for the subclass by default.
For inheritance, the subclass will call the constructor of the parent class by default, but if there is no default constructor of the parent class (null parameter), the subclass must explicitly specify the constructor of the parent class, and must be in the first line of code in the subclass constructor.
While inheritance brings benefits, it also has drawbacks:
- If the parent class changes, the child class needs to change
- Inheritance destroys encapsulation, and its implementation details are transparent to its subclasses
- Inheritance is a strong coupling relationship.
So when we use inheritance, we need to be sure that inheritance is an effective way. Do you want to use inheritance? Think in java provides a solution: ask yourself if you need to transform from a child class to a parent class. If you have to transition up, inheritance is necessary, but if you don't, you should consider whether you need inheritance.