Inheritance in Java

Source: Internet
Author: User

We in the previous study, we will have C # inheritance, today we come to understand the Java inheritance, in fact, are very similar!

1. Syntax

Modifier Subclass extends Superclass () {

Class Definition Section

}

Eg:public Dog extends Pet () {

}

We say dog has inherited pet.

2. So what can dog inherit from the pet class?

01. When these two classes are in the same package, the dog class inherits the member variables and member methods of the public,protected and default (friendly) access levels in the pet class.

02. When these two classes are in a different package, the dog inherits the member variables and member methods of the public and protected access levels in the pet class. (cannot inherit the default access level)

03. The properties and methods of the private adornment cannot be inherited.

04. You cannot inherit the parent class's constructor method.

Only multiple inheritance is supported in 3.Java, that is, each class can have only one direct parent class, but it can have multiple indirect parent classes

4. Overriding the method

A method that inherits from a parent class can be rewritten in a subclass, called a method override or method override, as required

Demand:

01. The overriding method and the overridden method must have the same method name.

02. The overriding method and the overridden method must have the same argument list.

03. The return value type of the overridden method must be the same as the return value type of the overridden method or its subclass.

04. The override method cannot narrow the access rights of the overridden method.

5.super keywords

Super represents the default reference to the previous direct parent class object for the current object. Members of a taxonomy can be accessed through the Super keyword in subclasses.

Note the point:

01.super must appear in subclasses (methods and construction methods of subclasses), not elsewhere.

02. You can access the members of the parent class, such as the parent class's properties, methods, and construction methods. ‘

03. Note The restrictions on access permissions, such as inability to access private members through super.

6. Construction methods in inheritance relationships

The construction method of Eg:dog class

Public Dog (String name,string strain) {

Super. (name);

This.strain=strain;

}

The calling rules for constructing methods under inheritance conditions are as follows:

01. If the constructor method of a subclass is not called by the super display to call the parent class's parameter constructor method, and does not explicitly call itself through this method of the other constructor, then the system will default to call the parent class of the non-parametric constructor first. In this case, there is no "super ();" Statement, the effect is said the same.

02. If the constructor method of the child class is called by the super display to invoke the parent class's constructor method, the corresponding construction method of the parent class will be executed instead of the parent class without the parameter construction method.

03. If the subclass's constructor method explicitly calls itself through this method, the above two rules are applied in the corresponding construction method.

04. In particular, if there is a multiple-inheritance relationship, when a subclass object is created, the above rules are applied more than once to the higher-level parent class until the non-parametric construction method of the parent class object class is executed.

Inheritance in Java

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.