About inheritance and polymorphism

 Several summary of inheritance and polymorphism

1, all the methods of the parent class can be inherited it? You can rewrite it? Polymorphisms can show it?

1.1 Non-static method

  1.1.1 are public, default, protected modified non-static method

  Subclasses can inherit, if not final modification, then be rewritten when the parent class reference to sub-class object, showing polymorphism.

  1.1.2 modified by private non-static method

  Not inherited by subclasses, but can not be rewritten, no polymorphism (some people to understand all the parent classes, including private members can be inherited, but not visible in the subclass, I prefer the former). When the same sub-class appearance with the parent class name private methods and parameters when what happens?

class the Parent {
     Private  void F () { 
        System.out.println ( "parent" ); 
    } 
    public  static  void main (String [] args) { 
        the Parent P = new new Child ();    
        PF (); 
    } 
} 
class Child the extends the Parent {
     public  void f () {          // private methods of the parent class is not visible to the subclass, the subclass of f () method is a new method, the compiler that f () method is not overwritten
    System.out.println("child"); 
  }
}

Print Results:

parent

1.2 static method

  Static methods can be inherited, can not be rewritten, it can not show polymorphism

the Parent {class 
    public static void F () { 
        System.out.println ( "parent"); 
    } 
} 
class the extends the Parent Child { 
    public static void F () { 
        System.out.println ( "Child"); 
    } 
    public static void main (String [] args) { 
        the Parent Child new new P = (); // static methods can be inherited, but can not be rewritten 
        PF (); 
        Child Child new new C = (); 
        CF2 (); 
    } 
}

Print Results:

parent
child

 1.3 Constructor

  Constructor can not be inherited, can not be rewritten, no polymorphism.

  Constructor method is neither static nor non-static method, there will be a constructor this object as a parameter passed in, so we can initialize the object attributes in the internal structure of the method, non-static method can also be invoked in the constructor.

  If the non-static method is overridden, then the internal structure will not exist polymorphic behavior? Reference Java programming ideas in an example:

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);
    }
}

 

  Non-static method call quilt class in the parent class constructor rewritten, polymorphic behavior occurs, but this is not the result we want, for the following reasons:

  1. Before anything else happens, the storage space allocated to a binary object is initialized to zero;
  2. As previously described calls the base class constructor. In this case, call draw () method (to be called before the constructor RoundGlyph) after being covered, due to the step 1, we will find the radius at this time is 0;
  3. Initialization method is called a member of the order of declaration;
  4. Call the constructor of the derived class body.

  Therefore, there is a valid criterion in the preparation of the constructor: " with the simplest possible way to make the object into the normal state; if possible, avoid calling other methods ." In the constructor, the only security call is final method in the base class (including private method), because these methods can not be overridden by a subclass, the above problem will not occur.

2, all of the properties of the parent class can be inherited it? You can rewrite it? Polymorphisms can show it?

2.1 are public, default, protected modified property

  Can be inherited (and regardless of whether it is static), can not be rewritten, no polymorphism. When a subclass is defined with the same properties as the parent class, subclasses and their properties while retaining the parent class in a different memory space

the extends the Parent Child {class 
    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) { 
        the Parent Child new new P = (); 
        System.out.println (PA); // any domain access operation by the compiler to parse the 
        Child c = Child new new (); 
        c.getA (); // default will get direct access to field its own domain 
        c.ParentA (); // domain by super.field can get the parent class 
    } 
}

2.1 modified by private property

  Personal understanding as can be inherited, but can not be accessed directly by the parent class public, default, protected or indirect access (it was also understood to not 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());  //结果为1
    }
}

   When the presence of the same private properties of the parent class and subclass:

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
    }
}

   About inheritance and polymorphism, and object initialization process, there are many places do not quite understand, first recorded, so in the future have the time to look at the principles of java virtual machine again perfect!

Guess you like

Origin www.cnblogs.com/xiaohang123/p/12070385.html