Java inheritance problem

Java inheritance problem

   first, the override is only for methods. The attribute with the same name in the subclass and the parent class is only that the subclass overrides the same attribute of the parent class;
   all attributes and methods in the parent class will be inherited by the subclass (the attribute method modified by private is only owned, but can be used indirectly through the public method in the parent class). When the subclass inherits, the subclass can call the inherited attributes and methods (except private ones). In addition, final methods, private methods and static methods cannot be overridden;
  in Java, subclasses can inherit the methods in the parent class without rewriting the same methods. However, sometimes subclasses do not want to inherit the methods of the parent class intact, but want to make certain modifications, which requires method override. Method override is also called method override.

/**
 * Testing Java inheritance
 * @author liuhongjun
 * @create 2020-11-23 20:34
 */
public class ExtendsTest {
    public static void main(String[] args) {

        Z z = new Z();
        System.out.println(z.defaultField);
        System.out.println(z.finalField);
        System.out.println(z.protectedField);
        System.out.println(z.extendField);
        z.defaultMethod();
        z.protectedMethod();
        z.finalMethod();
        z.extendMethod();
        Z.staticMethod();
        System.out.println(Z.staticField);

        System.out.println("========================");

        //During compilation, call from the left, while during runtime, call from the right
        F f = new Z();
        //shxuing
        System.out.println(f.defaultField);
        System.out.println(f.protectedField);
        System.out.println(f.finalField);
        System.out.println(f.staticField);
        //Method override, the method of the subclass called
        f.protectedMethod();
        F.staticMethod();
        f.defaultMethod();
        f.finalMethod();

        //Transformation is required to use subclass specific method attributes
        System.out.println(((Z) f).extendField);
        ((Z) f).extendMethod();
    }
}


class F {

    public final String finalField = "Parent class final attribute";

    public static String staticField = "Parent class static attribute";

    String defaultField = "Default attribute of parent class";

    protected String protectedField = "Parent class protected attribute";

    private String privateField = "Parent private property";


    public static void staticMethod(){
        System.out.println("Parent class static method");
    }

    //Cannot be overridden
    public final void finalMethod(){
        System.out.println("Parent class final method");
    }

    void defaultMethod(){
        System.out.println("Default method of parent class");
    }

    protected  void protectedMethod(){
        privateMethod();
        System.out.println("Parent class protected method");
    }

    private void privateMethod(){
        System.out.println(privateField);
        System.out.println("Parent class private method");
    }


}

class Z extends F {
    //Redefine the property with the same name inherited from the parent class
    public final String finalField = "Subclass final attribute";

    //public static String staticField = "subclass static attribute";

    String defaultField = "Subclass default properties";

    protected String protectedField = "Subclass protected attribute";

    private String privateField = "Subclass private property";

    public String extendField = "Subclass extended properties";

//    public static void staticMethod(){
//        System.out.println("subclass static method (redefine)");
//    }

    //The return value of the subclass method must be [less than or equal to] the return value range of the parent method (the return value of the parent or its subclass, the parent is void basic data type, and the subclasses need to be consistent)
    //The permission of the subclass method must be greater than or equal to the permission modifier of the parent method
    //The exception of the subclass method must be less than or equal to the exception of the parent method
    void defaultMethod(){
        System.out.println("Subclass default method (override)");
    }

    protected  void protectedMethod(){
        System.out.println("Subclass protected Method (override)");
    }

    private void privateMethod(){
        System.out.println("Subclass private method(redefinition)");
    }

    //Cannot be modified after inheritance
//    public final void finalMethod(){
//        System.out.println("parent class final method");
//    }

    public void extendMethod(){
        System.out.println("Subclass extension method");
    }

}

   in Java, if the parent class contains a static method, and the subclass also contains a static method with the same return type, method name and parameter list, the subclass actually hides the method with the same name in the parent class instead of overriding it. In other words, the parent and child classes contain two unrelated methods, and their behavior is not polymorphic. Just as "Java programming thought" said: "once you understand the mechanism of polymorphism, you may think that everything can happen in polymorphism. However, only ordinary method calls can be polymorphic." This is also a good understanding of why in Java, static methods and final methods (private methods belong to final methods) are pre bound, while all other methods are post bound.

Keywords: Java inheritance

Added by stuartriches on Wed, 09 Mar 2022 13:19:08 +0200