The relationship between parent and child classes

1. What is a parent and child classes?
In simple terms when there is a class called Father, then, if we use the keyword extends, class write the following statement:

class Son extends Father{
.....
}

The above is the configuration of a new class called the Son, and the direct use of a different class Son {}, which inherits the class Father, that Father Son is a subclass of Father Son is the parent class.

2. What subclass inherits the parent class?
We know two important classes of members: variables and methods. The parent class has its own variables and methods, and the subclass inherits the parent class members, not only have their own variables and methods, but also have variables and methods of the parent class. Subclasses can be used directly inherited members. We all know that the class is to allocate memory for the variable itself through its constructor, because the subclass inherits the parent class, the constructor of a subclass will first call the constructor of the parent class, and then use their own method of construction methods. (But this is not inherited, that is not the subclass inherits the parent class constructor's)
example:

public class Father{
    int money = 1000000;
    void fight(){
    System.out.println("I will kill you")
    }
}

class Son extends Father{
     int weight = 10;
}

The above piece of code, Son of the Father of money inherited variables and methods fight. Son equivalent in the presence of:

int money = 1000000void fight(){
    System.out.println("I will kill you")
    }

3. However, inheritance is also limited
(1) in the same package of inheritance and subclasses of the parent class: In addition to private member variables and methods, others are inherited.
(2) the parent class and subclass not in the same package: the subclass inherits only protected and public members of the parent class. (Difference between protected and friendly classes here appeared)

4. The tree structure of the classes
like:
Here Insert Picture Description

Where Object is the default ancestor class, i.e., any class which is the ancestor class. K is a descendant of class B, there are inheritance relationship between them.

Further explanation of 5.protected
If an object is created in its class D in the body, then the object can always be accessed through inheritance. "" Operator or protected their own definition of variables and methods, but if used in other classes class D creates an object, then the rights protected variables and methods of the Object access class D as follows:
(1) for the protected member variables and methods of the subclass D own declaration, as long as other class a and class D in the same package, then the object can access these protected variables and methods.
(2) protected D variables and methods inherited from a parent class, then the protected variables and methods as long as the parent class D and other classes in the same package, then the object can be accessed.

Above learning summary includes personal, such as the existence unreasonable, welcome to point out, learn from each other.

Published 35 original articles · won praise 0 · Views 1305

Guess you like

Origin blog.csdn.net/c1776167012/article/details/102758293