Object-oriented (inheritance, rewriting, this, super, abstract class), thissuper

Source: Internet
Author: User

Object-oriented (inheritance, rewriting, this, super, abstract class), thissuper

Inheritance
When defining A Class B, we find that existing class A is similar to the Class B to be defined, and Class B belongs to one of Class A, we can define class B as A subclass of Class.

When multiple classes have common content, you can extract the common content to a new class. This new class and multiple classes form a relationship called inheritance.
After a subclass inherits the parent class, it automatically has all the attributes and functions that can be inherited by the parent class.

Notes
Java only supports single inheritance, does not support multiple inheritance, and supports multi-level inheritance.
All classes directly or indirectly inherit the Object class. The Object class has no parent class.
Constructor cannot be inherited

Method Rewriting
When a subclass inherits the parent class, it automatically has all the attributes and functions that can be inherited by the parent class. However, if the subclass finds that the parent class method is not powerful enough, the parent class method can follow its own logic.
. It is also called method rewrite, and the method overwrites.
Notes
1. You can use @ Override to check whether the method is overwritten.
2. Subclass method permission must be greater than or equal to the parent class method permission
3. We recommend that you use the same method as the parent class.

Create a parent class object and a subclass object
Parent class objects take precedence over subclass objects
Each time a subclass object is created, the null parameter structure of the parent class is called by default to create a parent class Object (not a new object)
In fact, a parent class object is created in the subclass object because the subclass wants the content of the parent class.
The space for such a parent class is required
The first line of each constructor In the subclass has a default super () that calls the null parameter construction of the parent class.
Super (parameter) calls the constructor of the parent class

This and super
This: reference of this Class Object
Super: reference of the parent class in this class Object

This and super
This. The attribute name accesses the member variables of this class.
This. The method name (parameter) accesses other methods of this class.
This (parameter) accesses other constructor methods of this class (note that this must be used in the first line of the constructor) (understand)

Super. attribute name access parent class member variable (non-private)
Super. Method Name (parameter) access parent class member method (non-private)
Super (parameter) access the parent class Constructor (non-private) (note that it must be used in the first line of the constructor)

Note:
This and super cannot be used in the same constructor when calling the constructor, because both must be defined in the first line.
When calling a constructor, you must ensure that this constructor can be called. Without this constructor, you cannot call it.

Abstract class
Abstract Method: A method without a method body is called an abstract method.
Abstract class: classes with abstract methods must be abstract classes.
Use of abstract classes
1. abstract classes cannot create objects
2. Define subclass to inherit abstract classes
3. Subclass override parent class Method
4. Create a subclass object call Method
Abstract class details:
1. abstract classes can have no abstract methods
2. abstract classes can have specific methods
3. the abstract class must be a parent class
4. The subclass must override all abstract methods. Otherwise, the subclass is also an abstract class.
5. the abstract class has a constructor. The constructor must assign values to member variables.
Meanings of abstract classes
Abstract Classes define the most basic attributes and behaviors of a class of things. Forced subclass must implement its function. Subclass must override its abstract Method
Definition of abstract classes
* Abstract class definition
* Public abstract class name {
*
*
*}

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.