In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "the inheritance case analysis of Java". In the daily operation, I believe that many people have doubts about the inheritance case analysis of Java. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "inheritance case analysis of Java". Next, please follow the editor to study!
What is inheritance?
Object-oriented features:
Encapsulation: unnecessary exposure of data members and methods, modified with the private keyword. Meaning: security.
Background
The classes created in the code are mainly for abstracting some things in reality (including properties and methods).
Sometimes there are some relationships between objective things, so there is also a certain relationship when representing classes and objects.
For example, design a class to represent animals
Note: we can create a separate java file for each class. The class name must match the .java file name (case sensitive).
Class Dag {public String name; public int age; public void eat () {System.out.println ("eat ()");} class Bird {public String name; public int age; public String wing; public void eat () {System.out.println ("eat ()");} public void fly () {System.out.println ("fly");}
Two classes are defined, one is the Dag class and the other is the Bird class
Compare the above two classes:
We will find that they have something in common, and we can abstract what they have in common and redefine a class:
Class Animal {public String name; public int age; public void eat () {System.out.println ("eat ()");} class Dag {public String name; public int age; public void eat () {System.out.println ("eat ()");}} class Bird {public String name; public int age; public String wing Public void eat () {System.out.println ("eat ()");} public void fly () {System.out.println ("fly");}}
All three classes have the same eat method and behave exactly the same.
All three classes have the same name property, and the meaning is exactly the same.
Logically speaking, Dag and Bird are both Animal.
At this point, we can let Dag and Bird inherit Animal classes respectively to achieve the effect of code reuse.
At this time, an inherited class like Animal is called a parent class, a base class or a superclass. For classes like Dag and Bird, we call it a subclass. The derived class is similar to the real son inheriting the father's property, and the subclass will also inherit the fields and methods of the parent class to achieve the effect of code reuse.
rule of grammar
Basic syntax:
Class subclass extends parent class {
}
Use extends to specify a parent class
A subclass in Java can inherit only one parent class (while languages such as C++/Python support multiple inheritance)
The subclass inherits all public fields and methods of the parent class
Fields and methods of the private of the parent class are inaccessible in the subclass
Instances of the subclass also contain instances of the parent class. You can use the super keyword to get a reference to the parent class instance
For the above code, you can use inheritance to improve. At this point, we let Dag and Bird inherit from the Animal class, so Dag does not have to write name fields and eat methods when defining it.
Class Animal {public String name; public int age; public void eat () {System.out.println ("eat ()");} class Dag extends Animal {} class Bird extends Animal {public String wing; public void fly () {System.out.println ("fly");}}
Because the subclass inherits the fields and methods of the parent class, we write this in the main function:
Extends originally means "expansion" in English. The inheritance of the class we write can also be understood as an extension of the code based on the parent class.
For example, the Bird class we wrote extends the fly method on the basis of Animal.
If we change age to private, then subclasses will not be able to access
Because age is now private, you cannot use age in subclasses
Super keyword
When constructing a subclass, you should first help the parent class to construct.
When we write in this way, we won't make a mistake. Why?
There are three uses of super: / / cannot appear in static methods, because super is equivalent to a reference to a parent object
1. Super (): call the constructor of the parent class
2. Super.function (): call the normal method of the parent class
3. Super.data: call the member attribute of the parent class
Protected keyword
We just found that if you set the field to private, the subclass cannot access it. But setting it as public violates the original intention of our encapsulation. The best way to get the best of both worlds is the protected keyword
Protected-decorated fields and methods are not accessible to the caller of the class
Protected-decorated fields and methods are accessible to subclasses of the class and other classes of the same package
/ Animal.java public class Animal {protected String name; public Animal (String name) {this.name = name;} public void eat (String food) {System.out.println (this.name + "eating" + food);}} / / Bird.java public class Bird extends Animal {public Bird (String name) {super (name) } public void fly () {/ / for the protected field of the parent class, the subclass can correctly access System.out.println (this.name + "flying in progress");}} / / Test.java and Animal.java are no longer in the same package. Public class Test {public static void main (String [] args) {Animal animal = new Animal ("small animal"); System.out.println (animal.name); / / compilation error at this time, unable to access name}}
There are four kinds of access rights for fields and methods in Java
Private: can be accessed inside the class, but not outside the class
Default (also known as package access): the class can be accessed internally, classes in the same package can be accessed, and other classes cannot be accessed.
Protected: classes can be accessed internally, subclasses and classes in the same package can be accessed, and other classes cannot be accessed.
Public: both within the class and by the caller of the class can access
Final keyword
We used to learn the final keyword to modify a variable or field to represent a constant (which cannot be modified).
Final int a = 10
A = 20; / / compilation error
The final keyword can also modify a class, which means that the modified class cannot be inherited.
Final public class Animal {...} public class Bird extends Animal {...} / / compilation error Error: (3,27) java: unable to inherit from the final com.bit.Animal
The function of the final keyword is to restrict classes from being inherited
The matter of restriction means inflexibility. Flexibility is often not a good thing in programming. Flexibility may mean being more error-prone. When a class modified with final is inherited, it will compile and report an error, which will remind us that such inheritance is contrary to the original intention of the class design.
We usually use the String string class, which is decorated with final and cannot be inherited
At this point, the study of "inheritance case Analysis of Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.