Java Fifth Zhang Ziyi (class and inheritance

Source: Internet
Author: User

5.1 Subclass and Parent class

1 Java does not support multiple inheritance, that is, a subclass can not inherit from multiple parent classes at the same time, while C + + can.

It is customary to call the relationship between the subclass and the parent class "Is-a"

2 during the declaration of a class, a subclass of a class is defined by the keyword extends

Eg:class sub-class name extends parent class name {

}

3 classes in Java can form a tree structure in terms of inheritance, in which the root node is the object class (The object class is Java). The class in the Lang packet), the object class is the ancestor class of all classes,

Each class has and has only one parent class in addition to the object class, but a class can have 0 or more subclasses.

If a class does not use the Extend declaration at the time of declaration, then this class defaults to the subclass of the Objetc class

Eg:class A and Class A extends Object these two declarations are equivalent

5.2 Subclass • Inheritance

If the child class is in the same package as the parent class, the subclass inherits all the variables and methods in the parent class except the private attribute.

If the subclass is not in the same package as the parent class, the subclass inherits the variables and methods in the parent class except for the private and default attributes;

Access permissions for the protected property

For example, A is B's parent Class B is C's parent class C is the parent of D in the other class using the Class D to create an object, if you use this object is called the Class D Protectde property of the variable or method, if the Class D and other classes in the same package, then you can access,

If the ancestor class of Class D is called with an object, if the ancestor class and the other class are in a package, the variables and methods of the Protectde property can be called if they are traced back to the ancestor classes

5.3 Subclasses and objects

1: When creating objects using subclasses, the system assigns variables to the object and allocates memory for the variables, and the variables defined in all subclasses and their parent classes are allocated memory, but they are not all of the objects, only variables that can be accessed by the object belong to the object-owned variables

Eg: for example, in the same package, a variable of the private type of the parent class will also allocate memory, but these variables are not owned by objects created by subclasses, and similarly, if not in the same package, the variable of the default property is also 2.

The 2:instanceof operator instanceof operator is a binocular operator, the left action Yuanshi object, the right operand is the class, if the left object is the object created by the right class, or the child of the right class is created by the object

Then the returned value is true, otherwise the returned value is False

5.4 Hiding and method rewriting of member variables

The hiding of member variables

As long as the child class has the same name as the new defined variable and the parent class variable, the parent class's variable is hidden, even if the type is different

1 subclass inherited methods can only manipulate subclass inherited and suppressed member variables, subclasses of the newly defined methods may manipulate subclass inheritance and subclass new declared member variables, but cannot manipulate the implied member variables, (if the operation implied member variables, you need to use the Super keyword to decorate, later also described)

Method overrides

1 When overriding a method of a parent class, you cannot reduce access to the parent method, but you can increase access rights

2 JDK1.5 changes to the rewrite,

The type of the subclass override method can be a subtype of the parent method type, i.e., the parent class's method type is the people class, and the subclass's overriding method type can be the student class, provided that the student class is a subclass of the people class

Java Fifth Zhang Ziyi (class and inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.