Overview of inheritance in the Java language

Source: Internet
Author: User

Benefits of Inheritance:

1, improve the reusability of the code.

2, the relationship between class and class is created, which provides a precondition for the third characteristic polymorphism.

Java supports single inheritance, does not directly support multiple inheritance, because there are many parents in the same member will be poor call uncertainty, but the C + + in the multi-inheritance mechanism has been improved.

Single inheritance: A subclass can have only one direct parent class.

Multiple inheritance: A subclass can have multiple direct parent classes (not allowed in Java, improved-----java through "multi-implementation")

Java supports multi-layered (multiple) inheritance, and there is an inheritance system.

C Inherit b,b inherit a

When you want to use an inheritance system

1, review the top-level classes in the system to understand the basic functions of the system.

2, create the most sub-class object in the system, complete the function use.

When to define inheritance. Affiliation: is a relationship

Inheritance is defined when a relationship exists between a class and a class. XXX is one of the YYY, XXX extends YYY

In the child parent class, the specific embodiment of the member.

1, Member variable

When the member variable and local variable of this class have the same name as this.

When the name of a member variable in a child parent class is distinguished by super.

This and super are very similar in usage.

This: Represents a reference to an object of this class.

Super: Represents a space for a parent class.

2, member functions

A subclass function is run when a member function is identical in a child parent class.

This behavior, called the overwrite operation. This is the attribute of the function in the child parent class.

Two properties of the function:

1) Overloads, in the same class. Overload

2) overlay. Sub-class. Overrides become also rewritten, overwritten. Override

overriding considerations.

1) When a subclass method overrides a parent class method, the subclass permission must be greater than or equal to the permission of the parent class.

2) static can only be overridden by static, or statically overwritten.

When do I use the overwrite operation?

When you extend a subclass to a class, the subclass needs to preserve the function declaration of the parent class.

However, to define the unique content of the feature in the subclass, it is done with the overwrite operation.

3, constructor

When the subclass constructs the object, it is discovered that when the subclass constructor is accessed, the parent class is also running.

Cause: In the constructor of the subclass, there is a default implicit statement in the first row. Super ();

Instantiation of subclasses: All constructors in a subclass access the constructor of the null argument in the parent class by default. (The constructor of the parent class is accessed only once)

Why do you want to access the constructor in the parent class when the subclass is instantiated?

That's because the subclass inherits the parent class and gets the content (attributes) in the parent class, so before using the parent class content,

Start by looking at how the parent class initializes its own content. In order to complete this necessary action, a super () is added to the subclass constructor.

If the null argument constructor is not defined in the parent class, then the constructor of the subclass must explicitly call the constructor in the parent class with super. At the same time, if the class constructor is called with this in the subclass constructor, then super is gone because super and this can only be defined in the first row. So there can only be one, but it is guaranteed that there will be other constructors in the subclass that access the constructor of the parent class.

NOTE: The super statement must be defined in the first row of the subclass constructor. Because the initialization of the parent class is done first.

Class Demo

{

Demo ()

{

super (); Object class, default inheritance Extend

return;

}

}


The disadvantage of inheritance: breaking the encapsulation.

Overview of inheritance in the Java language

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.