In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Java abstract classes". In daily operation, I believe many people have doubts about how to use Java abstract classes. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Java abstract classes". Next, please follow the editor to study!
1. Abstract classes: you cannot create objects manually (JVM can create objects of abstract classes), but you can declare references to abstract types.
Public class TestAbstract1 {
Public static void main (String [] args) {
/ / Animal a=new Animal (); / / error
Animal a=null
A=new Dog ()
}
}
Abstract class Animal {
}
Class Dog extends Animal {
}
two。 Abstract method: there is a definition of the method name, no implementation, (only defines what can be done, not what to do)
The advantage of abstract methods is that they allow the definition and implementation of methods to be separated.
Abstract class Animal {
Public abstract void eat ()
}
The relationship between abstract class and abstract method: the class containing abstract method must be abstract class, and the abstract class does not necessarily contain abstract method.
The meaning of the existence of abstract classes is used to be inherited. A class inherits an abstract class and must implement all abstract methods in the abstract class, otherwise, this class is also an abstract class.
Abstract class Animal {
Public void run () {}
Public abstract void sleep ()
Public abstract void eat ()
}
Class Dog extends Animal {
Public void sleep () {
System.out.println ("Dog sleep.")
}
Public void eat () {
System.out.println ("Dog eat.")
}
}
Abstract class Cat extends Animal {
Public void eat () {
System.out.println ("Cat eat.")
}
}
You can declare parent class type subclass objects and use polymorphism to call abstract methods
Public class TestAbstract1 {
Public static void main (String [] args) {
Animal a=null
A=new Dog ()
A.sleep ()
A.eat ()
}
}
Abstract classes have construction methods, have parent classes, and follow the law of single inheritance.
Class E {}
Abstract class Animal extends E {
Public Animal () {
Super ()
}
}
3. Problems with the use of multiple modifiers:
Public protected default private static final abstract
Can be used in conjunction with:
Public static
Private static
Public final
Public static final
Cannot be used concurrently: abstract final void eat ()
Private abstract void eat ()
Static abstract void eat ()
Abstract cannot be used with final,private,static
At this point, the study on "how to use Java abstract classes" 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.