In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how abstract classes are used in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The details are as follows:
I. the basic concept of abstract class
The ordinary class is a perfect functional class, which can generate instantiated objects directly, and can contain constructors, common methods, static methods, constants and variables in the ordinary class. The abstract class refers to adding the components of abstract methods to the structure of the ordinary class.
So what is an abstract method? There is a "{}" on all common methods, which represents the method body, and the method with the method body must be used directly by the object. An abstract method refers to a method without a method body, and the abstract method must be modified with the keyword abstract.
A class with an abstract method is an abstract class, which is declared using the abstract keyword.
Example: define an abstract class
Abstract class A {/ / defines an abstract class public void fun () {/ / ordinary method System.out.println ("method with method body");} public abstract void print (); / / Abstract method, which has no method body and is modified by the abstract keyword}
Second, the use of abstract classes
Let's look at the example first.
Example: directly instantiate an object of an abstract class
Package com.wz.abstractdemo;abstract class A {/ / defines an abstract class public void fun () {/ / ordinary method System.out.println ("method with method body");} public abstract void print (); / / Abstract method, which has no method body and is modified by the abstract keyword} public class TestDemo {public static void main (String [] args) {An a = new A ();}}
Run:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot instantiate the type An at com.wz.abstractdemo.TestDemo.main (TestDemo.java:15)
From the above, we can see that An is abstract and cannot be instantiated directly. Why can't you instantiate it directly? When a class is instantiated, it means that the object can call properties in the class or let it go, but there is an abstract method in the abstract class, and the abstract method has no method body, and it cannot be called without the method body. Since it is impossible to make a method call, how to generate an instantiated object.
The principles for using abstract classes are as follows:
(1) Abstract method must be public or protected (because if it is private, it cannot be inherited by subclass and subclass cannot implement this method). By default, it is public;. (2) Abstract class cannot be instantiated directly and needs to be handled by subclass in the way of upward transformation. (3) Abstract class must have subclass. Using extends inheritance, a subclass can only inherit one abstract class. (4) if the subclass is not an abstract class, all abstract methods in the abstract class must be overridden (if the subclass does not implement the abstract methods of the parent class, the subclass must also be defined as the abstract class. )
Example:
Package com.wz.abstractdemo;abstract class A {/ / defines an abstract class public void fun () {/ / ordinary method System.out.println ("method with method body");} public abstract void print () / / Abstract method, no method body, modified by abstract keyword} / single inheritance class B extends A {/ / B class is a subclass of abstract class, is a common class @ Override public void print () {/ / force to override System.out.println ("Hello World!");}} public class TestDemo {public static void main (String [] args) {AA = new B (); / / upward transformation a.print () / / methods overridden by quilt subclasses}}
Running result:
Hello World!
It can be clearly found now:
(1) there are clear method override requirements in the abstract class inheritance subclass, while the ordinary class can selectively decide whether it needs to be overwritten; (2) the abstract class actually has more abstract methods than the ordinary class. other components are exactly the same as ordinary classes; (3) ordinary class objects can be instantiated directly, but the objects of abstract classes can only be obtained after upward transformation.
Although a subclass of a class can inherit any ordinary class, but from the actual requirements of development, ordinary classes try not to inherit another ordinary class, but to inherit abstract classes.
III. Restrictions on the use of abstract classes
(1) are there constructors in abstract classes?
Because there are some attributes in the abstract class, there must be constructors in the abstract class for the purpose of initializing the attributes.
And when the subclass object is instantiated, it still satisfies the order of executing the parent class construction first and then the subclass construction.
Examples are as follows:
Package com.wz.abstractdemo;abstract class A {/ / defines an abstract class public A () {System.out.println ("* class constructor *");} public abstract void print () / / Abstract method, no method body, modified by abstract keyword} / / single inheritance class B extends A {/ / B class is a subclass of abstract class, is a common class public B () {System.out.println ("* class B constructor *");} @ Override public void print () {/ / force override System.out.println ("Hello World!") }} public class TestDemo {public static void main (String [] args) {An a = new B (); / / upward transition}}
Execution result:
* Class A construction method * Class B construction method *
(2) can abstract classes be declared with final?
No, because abstract classes must have subclasses, while classes defined by final cannot have subclasses
(3) can abstract classes be declared with static?
Let's start with an example of an external abstract class:
Package com.wz.abstractdemo;static abstract class A {/ / defines an abstract class public abstract void print ();} class B extends A {public void print () {System.out.println ("*");}} public class TestDemo {public static void main (String [] args) {AA = new B (); / / upward transition a.print ();}}
Execution result
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Illegal modifier for the class A; only public, abstract & final are permitted at com.wz.abstractdemo.A. (TestDemo.java:3) at com.wz.abstractdemo.B. (TestDemo.java:9) at com.wz.abstractdemo.TestDemo.main (TestDemo.java:18)
Take another look at the inner abstract class:
Package com.wz.abstractdemo;abstract class A {/ / defines an abstract class static abstract class B {/ / static whose inner class belongs to the external class public abstract void print ();}} class C extends A.B {public void print () {System.out.println ("*");}} public class TestDemo {public static void main (String [] args) {A.B ab = new C (); / / upward transition ab.print ();}}
Execution result:
**********
Thus, external abstract classes are not allowed to use static declarations, while internal abstract classes run using static declarations. An internal abstract class declared using static is equivalent to an external abstract class, inheriting in the form of "external class. inner class" to represent the class name.
(4) can you directly call the methods declared with static in the abstract class?
Any time you want to execute the static method in a class, you can call it directly without an object, as well as for abstract classes.
Examples are as follows:
Package com.wz.abstractdemo;abstract class A {/ / defines an abstract class public static void print () {System.out.println ("Hello World!");}} public class TestDemo {public static void main (String [] args) {A.print ();}}
Running result:
Hello World!
(5) sometimes external subclasses can be ignored because only one specific system subclass operation is required in the abstract class. Such designs are common in system class libraries to hide subclasses that users don't need to know.
Examples are as follows:
Package com.wz.abstractdemo;abstract class A {/ / defines an abstract class public abstract void print (); private static class B extends A {/ / internal abstract class subclass public void print () {/ / overrides the method System.out.println ("Hello World!") of the abstract class;}} / / this method is not controlled by the instantiated object public static A getInstance () {return new B () }} public class TestDemo {public static void main (String [] args) {/ / when you get an abstract class object at this time, you don't need to know that class B is a subclass An a = A.getInstance (); a.print ();}}
Running result:
Hello World!
IV. The application of abstract class-template design pattern
For example, there are now three kinds of things:
(1) robots: charging, working; (2) people: eating, working, sleeping; (3) pigs: eating, sleeping.
Now it is required to implement a program that can realize the behavior of three different things.
First define an abstract behavior class:
Package com.wz.abstractdemo;public abstract class Action {public static final int EAT = 1; public static final int SLEEP = 3; public static final int WORK = 5; public abstract void eat (); public abstract void sleep (); public abstract void work (); public void commond (int flags) {switch (flags) {case EAT: this.eat (); break; case SLEEP: this.sleep (); break; case WORK: this.work (); break; case EAT + SLEEP: this.eat (); this.sleep (); break Case SLEEP + WORK: this.sleep (); this.work (); break; default: break;}
Define a class for a robot:
Package com.wz.abstractdemo;public class Robot extends Action {@ Override public void eat () {System.out.println ("robot charging");} @ Override public void sleep () {} @ Override public void work () {System.out.println ("robot working");}}
Define a person's class:
Package com.wz.abstractdemo;public class Human extends Action {@ Override public void eat () {System.out.println ("people eat");} @ Override public void sleep () {System.out.println ("people sleep");} @ Override public void work () {System.out.println ("people work");}}
Define a class of pigs:
Package com.wz.abstractdemo;public class Pig extends Action {@ Override public void eat () {System.out.println ("pig feeding");} @ Override public void sleep () {System.out.println ("pig sleep");} @ Override public void work () {}}
Test the main class:
Package com.wz.abstractdemo;public class AbstractDemo {public static void main (String [] args) {fun (new Robot ()); fun (new Human ()); fun (new Pig ());} public static void fun (Action act) {act.commond (Action.EAT); act.commond (Action.SLEEP); act.commond (Action.WORK);}}
Running result:
Robot charging robot working man eating man sleeping man working pig eating pig sleeping
All subclasses must be overridden according to the specified method if they want to complete the operation normally, and the function of the abstract class is the function of a class definition template.
Thank you for reading! This is the end of this article on "how to use abstract classes in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.