Home > Java > javaTutorial > body text

Object-oriented (inheritance, overriding, this, super, abstract class)

巴扎黑
Release: 2017-06-26 09:59:57
Original
2023 people have browsed it

Inheritance
When defining a class B, it is found that the existing class A is similar to the class B to be defined, and class B is a type of class A, class B can be defined as a class A subclass of A.

When multiple classes have common content, the common content can be extracted upwards and extracted into a new class. This new class forms a relationship with multiple classes called inheritance.
After a subclass inherits a parent class, it automatically possesses all the inheritable attributes and functions of the parent class.

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

Method override
When a subclass inherits After being added to a parent class, it automatically has all the inheritable properties and functions of the parent class. However, when the subclass feels that the parent class method is not powerful enough, it can rewrite the parent class method according to its own logic
. Also called method copying and method overwriting.
Notes
1. You can use @Override to check whether the method is overridden
2. The subclass method permissions must be greater than or equal to the parent class method permissions
3. Recommended and parent The class methods are the same

The creation process of parent class objects and subclass objects
The parent class object takes precedence over the subclass object
Each time a subclass object is created When , the empty parameter constructor of the parent class will be called by default to create the parent class object (not the new object)
Actually, a parent class object is created in the child class object because the child class wants the content of the parent class
It is necessary to have such a space support of the parent class
In the first line of each constructor method of the subclass, there is a default super() to call the empty parameter constructor of the parent class
super(parameter) calls the parent Class construction methods

this and super
this: reference to this class object
super: reference to the parent class in this class object

this and super
this.property name to access member variables of this class
this.method name (parameter) to access other methods of this class
this(parameter) to access other constructors of this class (note that it must Used in the first line of the constructor) (just understand it)

super. attribute name accesses parent class member variables (non-private)
super. method name (parameter) accesses parent class member methods (non-private) 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 When calling the constructor, it cannot be used at the same time in the same constructor, because they both need to be defined in the first line.
When calling a constructor, you must ensure that there is such a constructor before it can be called. If there is no such constructor, it cannot be called.

Abstract class
Abstract method: A method without a method body is called an abstract method
Abstract class: A class with an abstract method must be an abstract class
Abstract Usage of classes
1. Abstract classes cannot create objects
2. Define subclasses to inherit abstract classes
3. Subclasses override parent class methods
4. Create subclass objects to call methods
Abstract class details:
1. An abstract class can have no abstract methods
2. An abstract class can have concrete methods
3. The abstract class must be a parent class
4. The subclass must Rewrite all abstract methods, otherwise the subclass is also an abstract class
5. Abstract classes have construction methods, and subclasses need to use the construction methods to assign values ​​to member variables
The meaning of abstract classes
An abstract class defines an The most basic properties and behaviors of class things. Forces subclasses to implement their functionality. Subclasses must override their abstract methods
Definition of abstract class
* Definition of abstract class
* public abstract class class name{
*
*
* }

The above is the detailed content of Object-oriented (inheritance, overriding, this, super, abstract class). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!