Hands-on brain (10.22)

package org.yuan.my_project;

class Grandparent 
{
    public Grandparent()
     {
            System.out.println("GrandParent Created.");
}
    public Grandparent(String string) 
    {
            System.out.println("GrandParent Created.String:" + string);
 }
}
class Parent extends Grandparent
{
    public Parent()
     {
            //super("Hello.Grandparent.");
            System.out.println("Parent Created");    
            //super("Hello.Grandparent.");
      }
}
class Child extends Parent 
{
    public Child()
     {    
        System.out.println("Child Created");
      }
}
public class TestInherits {
    public static void main(String args[])
     {
        Child c=new Child();
        //System.out.println(c);
 }
}

 

 

 

 

 

1, the constructor call inherited conditions: by calling the base class constructor super, must be the first statement in the sub-class constructor.

2. The method covers:

package org.yuan.my_project;
class Base {
    void method() { 
            System.out.println("base");
    }
   
}
class Sub extends Base {
    void method() { 
            //super.method();
            System.out.println("Sub");
    }
   
}
public class Test {
    public static void main(String[] args) {
            Base sub1 = new Sub(); 
            sub1.method();
           
            Sub sub2 = new Sub();
            sub2.method(); 
    }
}

 

 

 

 Covering syntax rules: (1) a method of covering a range not less than allow access to the original method; (2) coating method thrown exception is not more than the original method; (3) a method for the final statement can not be overwritten;. (4) can not override a static method.

3、

public class ParentChildTest {
    public static void main(String[] args) {
        Parent parent=new Parent();
        parent.printValue();
        Child child=new Child();
        child.printValue();
        
        parent=child;
        parent.printValue();
        
        parent.myValue++;
        parent.printValue();
        
        ((Child)parent).myValue++;
        parent.printValue();
        
    }
}
class Parent{
    public int myValue=100;
    public void printValue() {
        System.out.println("Parent.printValue(),myValue="+myValue);
    }
}
class Child extends Parent{
    public int myValue=200;
    public void printValue() {
        System.out.println("Child.printValue(),myValue="+myValue);
    }
}

operation result:

 

 Summary: (1) when the child and parent class has the same methods, and let a parent class variable references a subclass object, which in the end the method call, their "real" is determined by the type of the object, that is to say: Object is a sub-type, sub-type of method is called, is the parent type, you call the parent types. (2) If the child and parent class have the same fields, subclass or hidden fields in place of the parent class field, a subclass of a subclass access method field (field instead of the parent class). If you really want to visit sub-class method parent class is hidden in the field, you can use super keyword to access it.

 

Guess you like

Origin www.cnblogs.com/tianwenjing123-456/p/11728792.html