Heritage

Sep 10, 2024 am 06:48 AM

  • Inheritance is one of the three basic principles of object-oriented programming.

  • Allows the creation of hierarchical classifications.

  • With inheritance, you can create a general class that defines characteristics common to a set of related items.

  • This general class can be inherited by other more specific classes, which add exclusive characteristics.

  • In Java jargon, the inherited class is called a superclass.

  • The class that inherits is called a subclass.

  • The subclass is a specialized version of the superclass.

  • The subclass inherits all variables and methods from the superclass and adds its own unique elements.

Basic aspects of inheritance

  • Java supports inheritance, allowing one class to incorporate another in its declaration.

  • This is done with the extends keyword.

  • The subclass adds functionality (extends) to the superclass.

  • An example of this is the creation of a superclass called TwoDShape, which stores the width and height of a two-dimensional object.

  • A subclass called Triangle is created to extend TwoDShape.

  • The extends keyword is used to create the subclass.

Heritage

Heritage

  • TwoDShape defines attributes of a generic two-dimensional shape, such as square, rectangle, triangle, etc.

  • The Triangle class is a subclass of TwoDShape and creates a specific type: the triangle.

  • Triangle inherits everything from TwoDShape and adds the style field, the area() method, and the showStyle() method.

  • style stores the style of the triangle, which can be any description such as "full", "contour", or "isosceles".

  • The area() method calculates and returns the area of ​​the triangle.

  • The showStyle() method displays the style of the triangle.

  • Triangle can access the width and height members of TwoDShape, both in area() and directly in the t1 and t2 objects.

  • TwoDShape, despite being a superclass of Triangle, is an independent class and can be used separately.

TwoDShape shape = new TwoDShape();
shape.width = 10;
shape.height = 20;
shape.showDim();

  • An object of the TwoDShape class does not know about or access any subclass of TwoDShape.

Heritage

  • Declaration of a class that inherits a superclass:

class subclass-name extends superclass-name {
// class body
}

  • In Java, you can only specify a single superclass for any subclass you create.

  • Java does not support multiple inheritance (inheritance from multiple superclasses in the same subclass), unlike C++.

  • It is possible to create an inheritance hierarchy where a subclass becomes the superclass of another subclass.

  • No class can be a superclass of itself.

  • An advantage of inheritance is that a superclass can define attributes common to multiple objects, allowing the creation of numerous specific subclasses.

  • Each subclass can define its own specific classification based on the superclass.

  • An example of this is creating a subclass of TwoDShape to encapsulate rectangles.

Heritage

  • The Rectangle class is a subclass of TwoDShape.

  • Adds the isSquare() method, which determines whether the rectangle is a square (when the width is equal to the height).

  • Also adds the area() method, which calculates the area of ​​the rectangle.

The above is the detailed content of Heritage. 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 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles