Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Whether abstract classes can be instantiated

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "whether abstract classes can be instantiated". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Abstract classes cannot be instantiated

An abstract class cannot instantiate an object directly through new, so it cannot be instantiated. To get the object of an abstract class, you need to inherit the abstract class with a class, and then instantiate the subclass.

You can also use anonymous inner classes to create an anonymous subclass in the abstract class, inherit the abstract class, and instantiate the objects of the subclass through a special syntax (this will be explained in more detail later).

Now the point is, to study this problem, the premise is that you have to understand the abstract class, which remains the same. Let's start with the root of the abstract class and deepen the understanding of the abstract class.

First of all, look at this example:

Package com.my.animal; / / Animal public class Animal {String name;// name String color;// color public Animal (String name,String color) {this.name = name; this.color = color;} public void run () {System.out.println (name+ "four legs run fast!") ;}} / / class Dog extends Animal {public Dog (String name,String color) {super (name,color);} / Fish inherits class Fish extends Animal {public Fish (String name,String color) {super (name,color);}} class Test {public static void main (String [] args) {Dog dog = new Dog ("pug", "white"); dog.run () Fish fish = new Fish ("koi", "red"); fish.run ();}}

Running result:

The pug runs fast on four legs!

Koi runs fast on four legs!

Did you find a problem? how can a fish run with its legs? is it a primitive fish?

Haha, just kidding, how to solve this problem? It probably occurred to you right away that it would be fine to override the run method of the parent class in the subclass.

Yes, it can be solved in this way, but have you ever thought about how we found this problem?

Is it only seen after compiling and running? of course, there are also bosses who can see it without compiling and running.

It means that there is a risk that we will not be able to find this problem, and it may not matter to others, but for us programmers, it is better not to make such low-level mistakes. Programmers should pursue higher goals. How can we trip here? if we want to reduce this risk to zero, what should we do?

Don't worry, you can't eat hot tofu in a hurry, just watch me analyze it slowly:

The current problems are:

1. The run method of animals is incorrectly described.

two。

When we describe a kind of thing, we find that there is some kind of behavior, but this kind of behavior is not specific at present, so we can extract the declaration of this kind of behavior, but do not implement this kind of behavior. at this time, this kind of behavior we call abstract behavior, we need to use abstract classes.

Let's take a look at the following example:

Package com.my.animal; / / Animal class (abstract class) public abstract class Animal {String name;// name String color;// color / / constructor public Animal (String name,String color) {this.name = name; this.color = color;} / / non-abstract method public void eat () {System.out.println (name+ "eat!") } / / Abstract method public abstract void run ();} class Dog extends Animal {public Dog (String name,String color) {super (name,color);} @ Override public void run () {System.out.println (name+ "four legs run fast!") ;}} class Fish extends Animal {public Fish (String name, String color) {super (name, color);} @ Override public void run () {System.out.println (name+ "wagging tail swim!") ;} class Test {public static void main (String [] args) {Dog dog = new Dog ("pug", "white"); dog.run (); Fish fish = new Fish ("koi", "red"); fish.run ();}}

Running result:

The pug runs fast on four legs!

Koi wagging tail swim!

When this problem is solved, the next question is: can abstract classes instantiate objects?

Just look at this example:

{String name; String color; public Animal (String name,String color) {this.name = name; this.color = color;} public abstract void run ();} class Test {public static void main (String [] args) {Animal a = new Animal (); a.run ();}}

Running result:

Error: (455.20) java:com.my.animal.Animal is abstract; cannot be instantiated

Details that abstract classes pay attention to:

1. If a function does not have a method body, then the function must be decorated with abstract to make the function abstract.

two。

3. If a non-abstract class inherits an abstract class, you must implement all the abstract methods of the abstract class.

4. Abstract classes can have abstract methods, non-abstract methods, or no abstract methods, but this doesn't make any sense. Java doesn't write nonsense.

5. Abstract classes cannot instantiate objects

6. An abstract class has a constructor, whose constructor is provided to the subclass to initialize the properties of the parent class when the object is created.

Question: why can't abstract classes instantiate objects?

Because abstract classes have abstract methods, it makes no sense to call abstract methods using objects of abstract classes if you can let them create objects.

The question is solved, the story is over? No, the sea is bottomless. Let's extend it a little bit, which is how to instantiate subclass objects with anonymous inner classes at the beginning.

Look at the following example:

Public class Java_Abstract2 {public void a () {System.out.println ("I am a non-abstract method in an abstract class");} public static Java_Abstract2 newIntences () {return new Java_Abstract2 () {};}} class Test2 {public static void main (String [] args) {Java_Abstract2 java_abstract2 = Java_Abstract2.newIntences (); java_abstract2.a ();}

Running result:

I am a non-abstract method in an abstract class

This is the end of the content of "whether abstract classes can be instantiated". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report