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

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

Jun 26, 2017 am 09:59 AM
this object inherit rewrite For

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of C++ function inheritance: How to use 'base class pointer' and 'derived class pointer' in inheritance? Detailed explanation of C++ function inheritance: How to use 'base class pointer' and 'derived class pointer' in inheritance? May 01, 2024 pm 10:27 PM

Detailed explanation of C++ function inheritance: How to use 'base class pointer' and 'derived class pointer' in inheritance?

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

What is the Request object in PHP?

How do inheritance and polymorphism affect class coupling in C++? How do inheritance and polymorphism affect class coupling in C++? Jun 05, 2024 pm 02:33 PM

How do inheritance and polymorphism affect class coupling in C++?

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

How to convert MySQL query result array to object?

How to rewrite function in golang? How to rewrite function in golang? Apr 27, 2024 am 11:15 AM

How to rewrite function in golang?

What is the difference between arrays and objects in PHP? What is the difference between arrays and objects in PHP? Apr 29, 2024 pm 02:39 PM

What is the difference between arrays and objects in PHP?

Detailed explanation of C++ function inheritance: How to debug errors in inheritance? Detailed explanation of C++ function inheritance: How to debug errors in inheritance? May 02, 2024 am 09:54 AM

Detailed explanation of C++ function inheritance: How to debug errors in inheritance?

What should I pay attention to when a C++ function returns an object? What should I pay attention to when a C++ function returns an object? Apr 19, 2024 pm 12:15 PM

What should I pay attention to when a C++ function returns an object?

See all articles