Inheritance of classes (2)

Inheritance keywords (extends and implements)

Inheritance can be implemented with two keywords: extends and implements. If there are no such keywords, the Object class is inherited by default.

  • extends
    class A{
    
    }
    Class B extends A{//A is the parent class, B is the child class
    
    } 

    Java, inheritance can be multi-layered inheritance, can not be multiple inheritance, can only be a single inheritance, that is, a subclass can only have one parent class, and extends can only inherit one class. The use of

  • implements

  1. implements can be disguised to make Java have multiple inheritance features, which complements Java's lack of single inheritance flexibility. The class implementation interface syntax is as follows:

  2. public interface A{
        Public void eat();
    }
    Public interface B{
        Public void sleep();
    }
    Public class C implements A,B{//Class C inherits interface A,B
       
    }

    Implementation interface is similar to inheriting the parent class. It can also obtain the variables and methods in the interface. The interface can be considered as a special inheritance (equivalent to the implementation class inheriting a thorough abstract class, except the default method. , all methods are abstract method classes);

  3. implementation interface, must implement all methods in the interface; (that is, rewrite these methods)

  4. interface can be implemented multiple times, the interface only defines the definition of abstract methods These methods default to public abstarct, and the member variables in the interface default to public static final, which must be explicitly initialized.

  • Inheritance should be noted (in the inheritance relationship, the subclass can directly access all the methods and member variables of the parent class, thus destroying the encapsulation of the parent class)

  1. Try to hide the internal data of the parent class, the parent All member variables of the class are set to the private access type, so that the subclass can not be directly accessed;

  2. try not to let the subclass arbitrarily access the method of modifying the parent class, for the basic non-use method, you can use private decoration; The method called by the outer class, modified with public, if the method does not want to be rewritten, use final modification; for the method to be rewritten, and do not want to be arbitrarily accessed, use protected;

  3. try not to Call the method to be overridden by the subclass in the parent class constructor; see the following example:

    class Parent{
    Public void Parent(){
    Eat();
    }
    Public void eat(){
    System.out.println("method to be rewritten by subclass");
    }
    }
    Public class Sub {
    Private String name;
    Public void eat(){
    System.out.println("Subclass rewriting method" +name.length());
    }
    
    Public static void main(String[] args) {
    // A null pointer exception occurred
    Sub s = new Sub();
    S.eat();
    }
    }

    Result: A null pointer exception occurred. Analysis: When creating a Sub object, the constructor in the parent class is executed first, and the eat() method is called in the parent class constructor. However, the call to the eat() method in the parent class is not a subclass. In the eat() method, the name instance variable of the Sub object is null, which causes a null pointer exception.

  • When do I need to derive subclasses from the parent class? The

  1. subclass needs to add additional attributes, not just the change of the attribute value; for example, the Student subclass is derived from the People class, and the Student ID is required in the Student class, but not in the People class.

  2. Subclass needs to increase its own behavior (new method or rewrite method); derive Student subclass from People class, need test method test() in Student class, and there is no such attribute in People class. This attribute is used to describe the behavior of the Student class.