Exploring understand java class inherits the parent class --Java how private property is inherited?

1, Java concept of inheritance

Inheritance is a cornerstone of the object-oriented programming java technology, as it allows the creation of hierarchical level classes. Inheritance is the subclass inherits the characteristics and behavior of the parent class, so that the subclass object (instance) instance fields and methods having a parent class or subclass inherits methods from parent classes, subclasses that have the same parent class behavior.

2, Java inherited characteristics

(1) Non-parent class has a subclass of private property, method.

(2) subclass can have its own properties and methods, i.e., subclass can extend the parent class.

(3) sub-class parent class may be implemented in their own way.

(4) Java inheritance is single inheritance, but multiple inheritance, a single inheritance is a subclass can inherit the parent class, multiple inheritance is, for example, class B inherits class A, class B inherits Class C, so that C according to the relationship class is the parent class B, class B is the parent class a class, which is different from the C ++ Java inherited inherited a feature.

(4) improve the coupling between classes (inherited shortcomings, it will cause a high degree of coupling between the more closely the code, the code independence worse).

3, Java succession problems

(1) can be obtained according to the above definition, the subclass inherits the parent class A B, the presence of the parent class A private attributes, creating class B when the object b, b does not have a private attribute A.
(2) create a class B class constructor method call objects A, but does not create an object of class A, class object is created in the B B.
Object b (3) Class B may be accessed indirectly by the public methods defined in the class A properties.
(4) In summary, create a sub-class object, the parent of private property will be created, but do not belong to a subclass, without the presence of the parent object. Is not it wonderful?

4, the Java code - a little experiment

public class ClassExtends{
    public static void main(String [] args){
        System.out.println("Hello,World!");
        Animal animal=new Animal();
        animal.eat();
        Dog dog1=new Dog();
        Dog dog2=new Dog("bage",12);
        dog2.getName();

    }
}

class Animal{
    private String name;
    private int age;
    public Animal(){
        System.out.println(this+"\tAnimal对象引用,Animal的构造函数");
    }
    public Animal(String name,int age){
        System.out.println(this+"\tAnimal对象引用,Animal的构造函数(带参数)");
        this.name=name;
        this.age=age;
    }
    public void eat(){
        System.out.println(this+"\t对象吃饭");
    }
    public String getName(){
        System.out.println(this+"\t调用Animal的getName()");
        return this.name;
    }
    public int getAge(){
        System.out.println(this+"\t调用Animal的getAge()");
        return this.age;
    }
}
class Dog extends Animal{
    public Dog(){
        //super();
        System.out.println(this+"\t我是dog对象引用,Dog的构造函数");
    }
    public Dog(String name,int age){
        super(name,age);
    }
}

operation result:

Hello,World!
Animal@15db9742 Animal对象引用,Animal的构造函数
Animal@15db9742 对象吃饭
Dog@6d06d69c    Animal对象引用,Animal的构造函数
Dog@6d06d69c    我是dog对象引用,Dog的构造函数
Dog@7852e922    Animal对象引用,Animal的构造函数(带参数)
Dog@7852e922    调用Animal的getName()

5 Conclusion

(1) create a sub-class object, all attributes are implicitly subclasses inherit all the parent class defined method.
Have (access to) non-private property, scheme (2) sub-class object can only be displayed.
(3) subclass object can access the parent class defined by the public methods of the parent class.

发布了2 篇原创文章 · 获赞 0 · 访问量 53

Guess you like

Origin blog.csdn.net/baguiming456/article/details/104611123