Home  >  Article  >  类库下载  >  Java Basics Learning--A Brief Summary of Inheritance

Java Basics Learning--A Brief Summary of Inheritance

高洛峰
高洛峰Original
2016-10-08 13:43:231753browse

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 directly accessed 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 Access

super();

Member variables that have been 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/>


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