A deep understanding of inheritance in Java and a deep understanding of Java inheritance

Source: Internet
Author: User

A deep understanding of inheritance in Java and a deep understanding of Java inheritance

For object-oriented programming, Every programmer should understand the encapsulation, inheritance, and Polymorphism in Java. What I am talking about today is the subject of inheritance.

1. Understanding of inheritance.

Inheritance is one of the three main characteristics of object-oriented, and is one of the important methods to implement code reuse in java. All classes in Java are obtained by directly or indirectly inheriting the java. lang. Object Class. The inherited class is called a subclass, And the inherited class is called a parent class. Subclass cannot inherit the member variables and methods whose access permission is private in the parent class. Subclass can override the parent class method and name the member variables with the same name as the parent class. However, Java does not support multi-inheritance, that is, the ability of a class to derive from multiple superclasses.

II. The inherited syntax in Java

Modifier Dog extends Animal

{

// Class definition

}

Inheritance is implemented by using the extends keyword, where Dog is called a subclass and Animal is called a parent class (base class or super class ).

3. sub-classes can inherit from the parent class.

01. inherit the attributes and Methods Modified by public and protected, regardless of whether the subclass and parent class are in the same package.

02. inherit the attributes and Methods Modified by the default permission modifier (friendly), but the subclass and parent class must be in the same package.

03. attributes and Methods Modified by private cannot be inherited.

04. The constructor of the parent class cannot be inherited.

4. access modifier access permission.

5. Subclass meets the requirements for rewriting the parent class method.

01. The method to be rewritten and the method to be rewritten must have the same method name.

02. The override method and the override method must have the same parameter list.

03. the return value type of the override method must be the same as the return value type of the method to be overwritten or its subclass.

04. The access permission of the method to be rewritten cannot be reduced.

6. What is the difference and connection between the rewrite method and the overload.

Resolution: The overload involves methods of the same name in the same class. The method name must be the same and the parameter list must be different. It is irrelevant to the return value type and access modifier, rewriting involves a method with the same name between the subclass and the parent class. The method name must be the same, and the parameter list must be the same,

The return value has the same type, and the access modifier cannot be strict with the parent class.

VII. usage of the super keyword.

Super indicates the default reference to the parent class Object of the current object. In the subclass, you can use the super keyword to access the members of the category.

Note:

01. super must appear in the subclass (subclass method and constructor), rather than in other places.

02. You can access the members of the parent class, such as the attributes, methods, and constructor of the parent class.

03. Pay attention to access permission restrictions. For example, you cannot access private members through super.

8. abstract class

1. Understanding of abstract classes:

01. in Java, when a class is modified by the abstract keyword, the class is called an abstract class.

02. abstract classes are parent classes abstracted from multiple concrete classes and belong to high-level abstractions.

Note:

01 abstract classes must be modified using abstract keywords.

02 abstract classes cannot be instantiated (object instances cannot be created using the new keyword ).

03 abstract classes can contain attributes, methods, constructor methods, initialization blocks, internal classes, and enumeration classes.

04 classes containing abstract methods must be defined as abstract classes.

05. Subclass must override all abstract methods before instantiation. Otherwise, the subclass is still an abstract class.

Eg: Define an abstract class

publiic abstract Class Dog{}

2. Can there be constructor in the abstract class?

Resolution: constructor can be included in an abstract class. The constructor can be called by other constructor methods of this class. If this constructor is not private-modified, it can also be called by the constructor In the subclass of this class.

 

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.