In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "what java inheritance features", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "what java inheritance features" bar!
What is inheritance?
Inheritance is one of the three object-oriented characteristics of the java object, that is, the subclass inherits the characteristics and behavior of the parent class, so that the subclass object has the same behavior as the parent class. For example, real-life animals, rabbits and sheep belong to herbivores, lions and leopards belong to carnivores, but herbivores and carnivores belong to animals, as shown below:
Inherited syntax
Class parent class {
}
Class subclass extends parent class {
}
The characteristics of inheritance
1. The inheritance keyword extends, after the implementation of inheritance, there is a relationship between classes.
two。 The essence of inheritance is to extract common code, to extract multiple repetitive codes upward, to simplify the code.
3. Class is the abstraction of a batch of objects, inheritance is the abstraction of a batch of classes.
4. The parent class is also called the superclass, or the base class, and the subclass is also called the derived class.
5. Java is an object-oriented language, and everything is an object. In order to meet this design principle, all classes inherit directly or indirectly from the Object class.
Advantages and disadvantages of inheritance
Advantages:
1. Simplified code
two。 Improved scalability and maintainability
Disadvantages:
The degree of coupling affects the whole body.
Inheritance case
Introduction
To develop animals, including penguins and mice, the requirements are as follows:
Penguin: attribute (name, id), method (eat, sleep, introduce yourself)
Mouse: attribute (name, id), method (eat, sleep, introduce yourself)
Usually we would write:
First define a penguin class, then properties, methods, and then mouse classes, attribute methods, as follows:
/ * * Penguins * * / public class Penguin {
Private String name
Private int id
Public Penguin (String myName, int myid) {
Name = myName
Id = myid
}
Public void eat () {
System.out.println (name+ "eating")
}
Public void sleep () {
System.out.println (name+ "sleeping")
}
Public void introduction () {
System.out.println ("Hello, everyone! I am" + id + "number" + name + ".")
}
} / * rat * * / public class Mouse {
Private String name
Private int id
Public Mouse (String myName, int myid) {
Name = myName
Id = myid
}
Public void eat () {
System.out.println (name+ "eating")
}
Public void sleep () {
System.out.println (name+ "sleeping")
}
Public void introduction () {
System.out.println ("Hello, everyone! I am" + id + "number" + name + ".")
}
}
It is found that the code is duplicated through coding, which leads to a large amount of code and bloated code, and the maintainability is not high. So can we use the inheritance solution? Take a look at the following code:
/ * * extract the public part to define the parent class * * / public class Animal {
Private String name
Private int id
Public Animal (String myName, int myid) {
Name = myName
Id = myid
}
Public void eat () {
System.out.println (name+ "eating")
}
Public void sleep () {
System.out.println (name+ "sleeping")
}
Public void introduction () {
System.out.println ("Hello, everyone! I am" + id + "number" + name + ".")
}
} / * Penguin inherits parent class * * / public class Penguin extends Animal {
Public Penguin (String myName, int myid) {
Super (myName, myid)
}
} / * mouse class inherits parent class * * / public class Mouse extends Animal {
Public Mouse (String myName, int myid) {
Super (myName, myid)
}
}
Through inheritance, it simplifies the code and improves the expansibility and maintainability of the code.
Matters needing attention
Understand inheritance and know how l is implemented, but you should also note the following:
1. If the parent class property method is decorated with private, then the subclass cannot be inherited
two。 Only a single inheritance is supported. Generally speaking, a subclass can only have one parent class, but a parent class can have multiple subclasses.
3. Support for multi-tier inheritance
4. When the member variables of the subclass and the parent class have the same name, the subclass takes precedence, that is, the nearest principle.
Thank you for your reading, the above is the content of "what are the java inheritance features". After the study of this article, I believe you have a deeper understanding of what java inheritance features are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.