Inheritance in Java

Source: Internet
Author: User

1. Inheritance

Inheritance is the child class inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the parent class's instance domain and method, or the subclass inherits the method from the parent class, so that the subclass has the same behavior as the parent class.

Java classes have four access control characters: private, default, protected, and public, (default is not written) and there are four levels of access control altogether.

The property or method of private modification is unique to the class and cannot be accessed directly in any other class;

The property or method of the default adornment has a package access attribute, and other classes in the same package can be accessed;

Protected modified properties or methods can be accessed by other classes in the same package, but also for subclasses that are not in the same package;

Public-decorated properties or methods can be accessed directly in the outer class.

When a subclass inherits from a parent class, the subclass can inherit properties and methods that have non-private adornments in the parent class, which can be called by super when the child class object wants to invoke the original method of the parent class . Subclasses are not inherited for properties and methods that are unique to the private decorated parent class.

Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.

Subclasses can implement methods of the parent class in their own way.

When a subclass needs to change the inherited method, that is, overriding the parent class ( which must have the same parameter return type ) Once overridden, this method of the parent class behaves as hidden for the subclass. After the object of the subclass calls this method, it is called after the subclass overrides the method.

2. This and super in succession:

The This in the method represents the object reference that is currently calling this method

Use this when a method in an object needs to call other methods in this object

When the name of the local variable in the object property and method is the same, sometimes this can handle the case where the member variables and the names of the parameters in the method are identical.

This can be thought of as a variable whose value is a reference to the current object

Super represents calling the corresponding properties and methods in the parent class.

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.