Object-oriented inheritance of 04 (covering property)

Covering properties
when the subclass if present and defines the name of the same properties as the parent class, referred to as a coverage property
was observed property overrides

class A{
    public String str="Hello";  
}

class B extends A{
    public int str=100;
    public  void print(){ 
        System.out.println(str);
        System.out.println(super.str);
    }
}

public class testDemo{
    public static void main(String args[]){
        B b = new B();
        b.print();
    }
}


Always stressed that in any development, class attribute must be encapsulated (99%), after packaging, makes no sense, because the private property of a subclass of the parent class definition can not be called
comparison: on the difference between super and this is?

No. the difference this super
1 Features This class constructor call, the method of the present class, this class attributes Subclasses call the parent class constructor, the parent class method, the parent class attributes
2 form First find whether there is a call of this type of structure, if there is a direct call, or to locate the parent class Subclass does not look directly call the parent class
3 special This class represents the current object -

Under development, for the operation of the class or parent class is strongly suggested adding "this" or "super" this distinction between good
summary
1. As long as the inheritance occurs, then it will overwrite the existence of the application, the application override in the main method
2. if parent class subclasses specified, but found that the parent class implementation can not be used to override the class satisfy a subclass claim, while retaining the parent class method names
3. subclasses override the method does not own more stringent than the parent class access control rights

Guess you like

Origin www.cnblogs.com/anyux/p/11898953.html