Detailed Explanation of Inheritance Principle and Implementation Method of java

  • 2021-07-26 07:44:50
  • OfStack

This paper describes the inheritance principle and implementation method of java with examples. Share it for your reference, as follows:

Inheritance

1. In java, it is single inheritance. Each subclass has only one parent.

Syntax: Subclass extends parent class

2. In java, even if no parent class is declared, there is an implicit parent class, that is, the Object class

3. You can use super in a subclass to call the method of the parent class

4. The problem of construction method in inheritance

During an instance of an new1 subclass, the default parameterless constructor of the parent class is automatically invoked first, and then the constructor of the subclass is invoked. If the parent class has no default constructor, only constructors with parameters, then an error will occur.

In addition to automatically calling the default constructor of the parent class by jvm, subclasses can also call the constructor of the parent class by super method, especially when the parent class has no default constructor but only constructors with parameters, then the constructor of the parent class with parameters must be called by super in subclasses.

5. Dynamic and Static Binding

Dynamic and Static Binding of java

6. Parent-child type conversion

Upward type conversion: The reference of the parent type points to the instance of the subtype


Child c = new Child();
Parent p = c;

Downward type conversion: In java, the downward type conversion is realized by casting, that is, the variables of the parent type can be assigned to the variables of the subtype, but it is not certain that every downward type conversion will be successful, depending on whether the dynamic type of the parent class variable (that is, the referenced object type) is this subclass or a subclass of this subclass.


public class Bind{
    public static void main(String[] args) {
        // Success 
        Parent p = new Child();
        Child c = (Child)p;
        // Failure 
        Parent p1 = new Parent();
        Child c1 = (Child)p1;
    }
}

Results:

Exception in thread "main" parent Constructor
child Constructor
parent constructor
java.lang.ClassCastException: practice.Parent cannot be cast to practice.Child
at practice.Bind.main(Bind.java:13)

7. Visibility rewriting

When overriding a method, 1 generally does not modify the visibility of the method. However, if the visibility of the method is modified, the method of the subclass cannot reduce the visibility of the method of the parent class when rewritten: if the method in the parent class is public, the method in the subclass must also be public; If the method in the parent class is protected, the method in the subclass can be protected or public.

This is because in java inheritance is a "is-a" relationship, meaning that subclass objects also belong to the parent class. The child class must support all external behaviors of the parent class.

8. Use final decorations to avoid inheritance

final modifies a class to indicate that the class cannot be inherited; final modifies a method in a non-final to indicate that the method cannot be overridden in a subclass.

9. Advantages and disadvantages of inheritance

Benefits: Code reuse, common properties and methods in the parent class, and subclasses only need to pay attention to subclass-specific ones.

Objects of different subclasses can be handled more conveniently by Unification 1 (there is also polymorphism here).

Disadvantages: Inheritance destroys encapsulation

How do I avoid using inheritance?

(1) Use the final keyword

(2) Prefer composition over inheritance

(3) Using interfaces

More readers interested in java related content can check the topics of this site: "Introduction and Advanced Tutorial of Java Object-Oriented Programming", "Tutorial of Java Data Structure and Algorithm", "Summary of Java Node Operation Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"

I hope this article is helpful to everyone's java programming.


Related articles: