Ii. Java Object-oriented (8) _ Inheritance thought--inheritance relationship

Source: Internet
Author: User

2018-05-01

Labor is to enjoy life better.

Thoughts of succession

I. The concept of succession

Inheritance is the most significant feature of object-oriented. Inheritance is the derivation of new classes from existing classes, which can absorb some of the data properties and behaviors of existing classes, and can extend new capabilities.

Inheritance is a subclass that inherits the characteristics and behavior of the parent class so that the child class object (instance) has the instance domain and method of the parent class.

Inheritance is a subclass of public/protected variables and methods that inherit the parent class by inheriting the parent class.

Inherited classes: Super Class (Parent/superclass/base class/Extended Class)

Sub class: Subclass/Extension Class

Parent class: Used to store commonalities (common attributes (State and behavior))

Subclass: Used to store attributes (their own unique characteristics (state and behavior))

Inheritance solves the problem of code duplication, and the real function is to express a system.

Ii. The relationship of succession

1. What is an inheritance relationship?

The definition of an object based on a parent class (where the definition includes fields, methods, and inner classes) is expanded to create a new class definition, subclasses can inherit the parent class's original some definition, you can either increase the definition that the parent does not originally have, or overwrite (modify) Some of the attributes in the parent class.

  from an object-oriented perspective, inheritance is a kind of relationship from general to special , logically its essence is the is-a relationship. That is, the child class is the extension of the parent class, which is a special kind of parent class.

For example, a dog is a special case of an animal, a dog belongs to an animal. For example, the Red Rabbit Horse is both a horse and an animal.

--------------------------------------------------------------------------------------------------------------- --------------

2. Class inheritance Format

in Java, through the extends keywords can declare a class to inherit another class, in the general form as follows:

Class- parent class name {   }    < Span class= "hl-brackets" >   class subclass class name   Span class= "hl-reserved" > extends parent class name  { //write your own unique state and behavior }   look at the picture:< Span class= "hl-brackets" >   

--------------------------------------------------------------------------------------------------------------- ------------

3. Classification of inheritance

in Java, the inheritance relationship between classes and classes only single inheritance is allowed, and multiple inheritance is not allowed. A single inheritance is a subclass that inherits only one parent class. Multiple inheritance is a subclass that can have two or more parent classes. In other words, Class A, can only have a direct parent class, Class A cannot inherit both class B and Class C. And the interface supports multiple inheritance.

Multiple inheritance is allowed in Java. Multiple inheritance is, for example, Class A inherits Class B, Class B inherits Class C, so by relationship, Class C is the parent class of Class B, and Class B is the parent class of Class A.

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------

4. Object class

Java.lang.Object

The Java.lang package does not need to display the import when it is used, and is automatically imported by the compiler at compile time.

The object class is the root of the class hierarchy, and all classes in Java are fundamentally inherited from the object class, which is The object class is either a direct parent of a class or an indirect parent class of a class .

  The object class is the only class in Java that does not have a parent class (it cannot inherit itself).

  All other classes, including standard container classes, such as arrays, inherit the methods in the object class.

--------------------------------------------------------------------------------------------------------------- --

Does the subclass inherit the members of the parent class? ( judging by the access modifier )

    • The public members of the parent class are inherited as public members of the subclass, as if they were directly defined in the subclass.
    • The protected member of the parent class is inherited as a protected member of the child class. As if they were directly defined in the subclass.
    • The parent class's package access member (the parent class and subclass in the same package, the parent class uses the default modifier) inherits the package access member as a subclass. As if they were directly defined in the subclass.
    • Private members of the parent class cannot inherit from the quilt class because private can only be accessed in this class and the subclass cannot access it. But the subclass object does contain a private member of the parent class.
    • The constructor of the parent class cannot inherit from the quilt class because the constructor must be the same as the current class name.

Note: Do not recite text, you should write code to practice.

Ii. Java Object-oriented (8) _ Inheritance thought--inheritance relationship

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.