Java basic learning--a brief summary of inheritance
Code reference: Java basic learning notes--polymorphism
Why should inheritance be introduced?
Or make a media library, which can hold CDs and DVDs. If CD and DVD are made into two unrelated classes, then when managing this media library, you need to make a separate function to add CD and a separate function to add DVD. If you want to add to this media library For other media classes, another additional function must be created. We say that such code is not scalable. In addition, CDs and DVDs have many similarities. Their member variables include titles, playback duration, comments, etc. We say that such a program has a lot of code duplication, and code duplication is a manifestation of bad code. So we can create a parent class of them, add their same member variables to the parent class, and they can all inherit these variables. When adding to the media library, we only need to create a function that adds their parent class. , they can be added as well.
What does the subclass inherit from the parent class?
Inheritance gets everything from the parent class, including member variables and member functions. But you may not be able to access everything in the parent class, so look at the access attributes of member variables and member functions inherited from the parent class. If any member variables in the parent class are private, they cannot be accessed directly by the subclass. However, private can only be accessed by the parent class itself, so we can indirectly access these member variables in the subclass by calling the public function of the parent class.
In addition, in addition to inheriting from the parent class, a subclass can also have its own unique member variables and functions written in its own class.
Access properties
Access properties Meaning
public Open to everyone
private Only you can access
protected Only yourself, subclasses and other classes in the package can access
Default Only you and other classes in the package can access To access
super();
member variables constructed in the constructor of the parent class must be stated in the parameter list of the constructor of the subclass, and use super() to construct these in the parent class The member variables are taken from the parent class. For example code, please see the polymorphism blog post.
The parameters inside super(); will determine which constructor in the parent class is called. The parameters inside super(); are the same as the parameters of the called constructor.
In the default mode (if there is no super(); in the subclass constructor), the constructor without parameters in the parent class will be called by default. If there is no such constructor in the parent class, eclipse will report an error.
Override
When there is a function with the same name in the subclass and the parent class, the function in the subclass will override (override) the function with the same name inherited from the parent class, so it comes from the parent class That function will be hidden and will not work. But if you want to call the function with the same name in the parent class in the subclass, you should write it as super.function name(); .
<br/>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
