search
HomeJavajavaTutorialObject-oriented (inheritance, overriding, this, super, abstract class)

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
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
解决PHP报错:继承父类时遇到的问题解决PHP报错:继承父类时遇到的问题Aug 17, 2023 pm 01:33 PM

解决PHP报错:继承父类时遇到的问题在PHP中,继承是一种重要的面向对象编程的特性。通过继承,我们能够重用已有的代码,并且能够在不修改原有代码的情况下,对其进行扩展和改进。尽管继承在开发中应用广泛,但有时候在继承父类时可能会遇到一些报错问题,本文将围绕解决继承父类时遇到的常见问题进行讨论,并提供相应的代码示例。问题一:未找到父类在继承父类的过程中,如果系统无

如何在 golang 中重写函数?如何在 golang 中重写函数?Apr 27, 2024 am 11:15 AM

在Go中,方法重写允许在派生类中重新定义基类中的方法,同时保持相同的方法签名:使用override关键字。重写方法必须与基方法具有相同的签名。重写方法的接收者类型必须是基类型的子类型。

使用继承的Java程序来计算定期存款(FDs)和定期存款(RDs)的利息使用继承的Java程序来计算定期存款(FDs)和定期存款(RDs)的利息Aug 20, 2023 pm 10:49 PM

继承是一个概念,它允许我们从一个类访问另一个类的属性和行为。被继承方法和成员变量的类被称为超类或父类,而继承这些方法和成员变量的类被称为子类或子类。在Java中,我们使用“extends”关键字来继承一个类。在本文中,我们将讨论使用继承来计算定期存款和定期存款的利息的Java程序。首先,在您的本地机器IDE中创建这四个Java文件-Acnt.java−这个文件将包含一个抽象类‘Acnt’,用于存储账户详情,如利率和金额。它还将具有一个带有参数‘amnt’的抽象方法‘calcIntrst’,用于计

PHP中的多重继承PHP中的多重继承Aug 23, 2023 pm 05:53 PM

继承:继承是面向对象编程(OOP)中的一个基本概念,它允许类从其他类继承属性和行为。它是一种基于现有类创建新类的机制,促进代码重用并建立类之间的层次关系。继承基于"父子"或"超类-子类"关系的概念。从中继承的类被称为超类或基类,而继承超类的类被称为子类或派生类。子类继承其超类的所有属性(变量)和方法(函数),还可以添加自己独特的属性和方法或覆盖继承的属性和方法继承的类型在面向对象编程(OOP)中,继承是一个基本概念,它允许类从其他类中继承属性和行为。它促进

如何在PHP中使用多态和继承来处理数据类型如何在PHP中使用多态和继承来处理数据类型Jul 15, 2023 pm 07:41 PM

如何在PHP中使用多态和继承来处理数据类型引言:在PHP中,多态和继承是两个重要的面向对象编程(OOP)概念。通过使用多态和继承,我们可以更加灵活地处理不同的数据类型。本文将介绍如何在PHP中使用多态和继承来处理数据类型,并通过代码示例展示它们的实际应用。一、继承的基本概念继承是面向对象编程中的一种重要概念,它允许我们创建一个类,该类可以继承父类的属性和方法

PHP中的封装技术及应用PHP中的封装技术及应用Oct 12, 2023 pm 01:43 PM

PHP中的封装技术及应用封装是面向对象编程中的一个重要概念,它指的是将数据和对数据的操作封装在一起,以便提供对外部程序的统一访问接口。在PHP中,封装可以通过访问控制修饰符和类的定义来实现。本文将介绍PHP中的封装技术及其应用场景,并提供一些具体的代码示例。一、封装的访问控制修饰符在PHP中,封装主要通过访问控制修饰符来实现。PHP提供了三个访问控制修饰符,

Nginx如何实现基于请求URL的请求重写配置Nginx如何实现基于请求URL的请求重写配置Nov 08, 2023 pm 04:15 PM

Nginx是一款轻量、高性能的Web服务器,它不仅支持反向代理、负载均衡等高级功能,还具备强大的请求重写能力。在实际的Web应用中,很多情况下需要对请求URL进行重写,以达到更好的用户体验和搜索引擎优化效果。本文将介绍Nginx如何实现基于请求URL的请求重写配置,包括具体的代码示例。重写语法在Nginx中,可以使用rewrite指令来进行请求重写。其基本语

如何使用Java强制继承代理final类?如何使用Java强制继承代理final类?Sep 06, 2023 pm 01:27 PM

如何使用Java强制继承代理final类?在Java中,final关键字用于修饰类、方法和变量,表示它们不可被继承、重写和修改。然而,在某些情况下,我们可能需要强制继承一个final类,以实现特定的需求。本文将讨论如何使用代理模式来实现这样的功能。代理模式是一种结构型设计模式,它允许我们创建一个中间对象(代理对象),该对象可以控制对另一个对象(被代理对象)的

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools