On Inheritance and polymorphism

On Inheritance and polymorphism

1. Can all methods of the parent class be inherited? Can it be rewritten? Can it show polymorphism?

1.1 non static method

1.1.1 non static methods decorated by public, default and protected

It can be inherited by subclass, and can be rewritten if it is not modified by final. When a parent class reference points to a subclass object, it shows polymorphism.

1.1.2 non static methods decorated by private

Can't be inherited by subclasses, can't be rewritten, and has no polymorphism (some people understand that all members of the parent class, including private members, can be inherited, but they are not visible in subclasses, I prefer the former). What happens when a subclass has the same private method name and parameter as the parent?

class Parent{
    private void f() {
        System.out.println("parent");
    }
    public static void main(String[] args) {
        Parent p = new Child();   
        p.f();
    }
}
class Child extends Parent{
    public void f() {         //The private method of the parent class is not visible in the child class. The f() method of the child class is a brand new method. The compiler thinks that the f() method has not been overridden
    System.out.println("child"); 
  }
}

Print results:

parent

1.2 static method

Static methods can be inherited, cannot be rewritten, and cannot show polymorphism

class Parent{
    public static void f() {
        System.out.println("parent");
    }
}
class Child extends Parent{
    public static void f() {
        System.out.println("child");
    }
    public static void main(String[] args) {
        Parent p = new Child();   //Static methods can be inherited but not overridden
        p.f();
        Child c = new Child();
        c.f();
    }
}

Print results:

parent
child

1.3 construction method

The constructor cannot be inherited, rewritten, or polymorphic.

The constructor is neither a static method nor a non-static method. There will be a this object passed in as a parameter in the constructor, so we can initialize the object properties inside the constructor or call non-static methods inside the constructor.

If the non-static method is overridden, will there be polymorphism within the constructor? Refer to an example in Java programming ideas:

class Glyph {
    void draw() {
        System.out.println("Glyph.draw()");
    }

    Glyph() {
        System.out.println("Glyph() before draw()");
        draw();
        System.out.println("Glyph() after draw()");
    }
}

class RoundGlyph extends Glyph {
    private int radius = 1;
    RoundGlyph(int r) {
        radius = r;
        System.out.println("RoundGlyph.RoundGLyph(), radius = " + radius);
    }
    void draw() {
        System.out.println("RoundGlyph.draw(), radius = " + radius);
    }
}

class RolyConstructors {
    public static void main(String[] args) {
        new RoundGlyph(5);
    }
}

 

Calling the non static method overridden by the subclass in the parent constructor will result in polymorphism, but this is not the result we want, for the following reasons:

  1. Initialize the storage space allocated to the object to zero binary before anything else happens;
  2. Call the base class constructor as described earlier. At this time, call the overwritten draw() method (to be called before calling the RoundGlyph constructor). Because of step 1, we will find that the radius value is 0;
  3. Call the initialization methods of members in the order of declaration;
  4. Call the constructor body of the exported class.

Therefore, there is an effective guideline in writing constructors: "use as simple a method as possible to put an object in a normal state; if possible, avoid calling other methods.". In the constructor, the only one that can be called safely is the final methods (including private methods) in the base class, because these methods cannot be overridden by subclasses, so the above problems will not occur.

2. Can all properties of the parent class be inherited? Can it be rewritten? Can it show polymorphism?

2.1 attributes decorated by public,default,protected

Can be inherited (whether static or not), can not be rewritten, no polymorphism. When the same attributes as the parent are defined in the subclass, the subclass will keep its own and parent attributes in different storage spaces at the same time

class Child extends Parent {
    public static int a = 2;
    public void getA() {
        System.out.println("a = "+a);
    }
    public void ParentA() {
        System.out.println("super.a = " + super.a);
    }
    public static void main(String[] args) {
        Parent p = new Child();
        System.out.println(p.a);  //Any domain access operations are resolved by the compiler
        Child c = new Child();
        c.getA();     //Directly accessing the field will get its own domain by default
        c.ParentA();  //The domain of the parent class can be obtained through super.field
    }
}

2.1 attributes decorated by private

Individuals can be understood as inheritable, but not directly accessible. They can be accessed indirectly through the parent class public, default, or protected methods (some people also understand that they cannot be inherited)

class Parent {
    private int a;
    public Parent(int a) {
        this.a = a;
    }
    public int getA() {
        return a;
    }
}

class Child extends Parent {
    public Child(int a) {
        super(a);
    }
    public static void main(String[] args) {
        Child c = new Child(1);    
        System.out.println(c.getA());  //The result is 1
    }
}

When the parent and child classes have the same private property:

class Parent {
    private int a;
    public Parent(int a) {
        this.a = a;
    }
    public int getA() {
        return a;
    }
}

class Child extends Parent {
    private int a = 2;
    public Child(int a) {
        super(a);
    }
    public static void main(String[] args) {
        Child c = new Child(1);    
        System.out.println(c.getA());  //1
        System.out.println(c.a);       //2
    }
}

There are many incomprehensible points about inheritance, polymorphism and object initialization. First record them, and then study the principle of java virtual machine in the future!

Keywords: Java Programming

Added by Concat on Thu, 19 Dec 2019 19:06:20 +0200