Java Abstract Class What Is It Good For?

mac2026-01-23  6

Java Abstract Class What Is It Good For?

java 抽象类有什么有点?

The Java abstract class eludes many Java developers. Let’s learn what it does for us and how to use it.

java抽象类使得很多java开发人员都很想逃避,让我们学习它对我们有什么作用。

Abstract art: a product of the untalented sold by the unprincipled to the utterly bewildered. Al Capp

抽象艺术:由无原则的人出售给完全困惑的人的一种产品。阿尔-卡普

I am guessing you have heard of the malady called ADD or Attention Deficit Disorder. On a recent trip to Paris, my son and I discovered we are suffering from another malady with similar initials. Art Deficit Disorder.

We can look at paintings and sculptures and find it uninspiring. Where my daughter enjoyed the d’Orsay we looked for the food court. Where we enjoyed some espresso and fresh-squeezed orange juice.

我猜你听说过一种叫做注意力缺失症的疾病。在最近的一次巴黎之行中,我和儿子发现我们患上了另一种同名疾病。艺术缺陷障碍。

我们可以看看绘画和雕塑,发现它毫无生气。在我女儿喜欢奥赛的地方,我们找了美食广场。我们在那里喝了一些浓咖啡和鲜榨橙汁。

Java Abstract Class

Java抽象类

Java has abstract classes that are incomplete. They cannot be implemented like a regular class. Abstract classes must be subclassed to be used. In these classes, we can declare abstract methods. Abstract classes are similar to interfaces in Java. Let’s dive deeper into this comparison.

Java有不完整的抽象类它们不能像普通类那样实现抽象类必须是子类才能使用。在这些类中,我们可以声明抽象方法抽象类类似于Java中的接口让我们更深入地研究一下这个比较。

Compare

比较

Like interfaces, abstract classes cannot be instantiated. Where the interface just will contain method signatures an abstract class can contain a method body. Abstract classes can declare fields that are not static and final.

与接口一样,抽象类也不能实例化。如果接口只包含方法签名,则抽象类可以包含方法体。抽象类可以声明非静态和最终的字段。

Where interfaces have all fields automatically become public, static, and final. We can implement as many interfaces as we want. Abstract classes are like regular classes and we can only extend one.

其中接口使所有字段自动成为公共、静态和最终字段。我们可以实现任意多个接口。抽象类就像常规类,我们只能扩展一个。

The Java tutorial has some good guidance when to use abstract classes. When we “want to share code among several closely related classes” or “expect that classes that extend your abstract class have many common methods or fields”. Interfaces should be used when “expect that unrelated classes would implement your interface” or “want to specify the behavior of a particular data type”.

Java教程对何时使用抽象类有很好的指导当我们“希望在几个密切相关的类之间共享代码”或“希望扩展抽象类的类有许多公共方法或字段”时。当“期望不相关的类实现您的接口”或“希望指定特定数据类型的行为”时,应使用接口。

Java Abstract Class Example

java抽象类实例

Like all good coders let’s get our hands dirty with some code. First, we can look at an example abstract class to get us started.

像所有优秀的程序员一样,让我们用一些代码弄脏我们的手。首先,我们可以看一个示例抽象类来了解

package com.myitcareercoach; public abstract class Battery { int volt; int amps; void charge(int chargingTime) { // shared code } abstract boolean fullyCharged(); abstract boolean isTooHot(); } view rawBattery.java hosted with ❤ by GitHub

 

This Battery abstract class has one implemented method and two abstract methods. There are also two fields defined as well.

这个Battery抽象类有一个实现方法和两个抽象方法还定义了两个字段。

package com.myitcareercoach; public class ComputerBattery extends Battery { @Override boolean fullyCharged() { // TODO Add some code here! return false; } @Override boolean isTooHot() { // TODO Add some code here! return false; } } view rawComputerBattery.java hosted with ❤ by GitHub

ComputerBattery is a concrete Java class. Therefore, it needs to implement both of the abstract methods that Battery defined.

ComputerBattery是一个具体的Java类因此,它需要实现battery定义的两个抽象方法。

Abstract and Interface?

抽象类和接口?

An abstract class can even implement an interface. This seems like mixing spaghetti and mashed potatoes but, hey it is legal!

一个抽象类甚至能实现一个接口,这好像混合了意大利面条和土豆泥,但是,嘿,这是合法的!

public interface Student { public void setSchedule(); public void registerForClass(String className); } view rawStudent.java hosted with ❤ by GitHub

Let’s take our Student interface and mix it in an abstract class.

让我们把学生界面混合到一个抽象的类中。

public abstract class ProbationaryStudent implements Student { @Override public void setSchedule() { // implemented method } // not going to override registerForClass() method } view rawProbationaryStudent.java hosted with ❤ by GitHub

In our ProbationaryStudent class, we don’t implement all the methods defined in the Student interface. This is possible since the class is marked as abstract.

在我们的试用学生课程中,我们没有实现在学生界面中定义的所有方法。这是可能的,因为类被标记为抽象的。

Main?

主要的?

Would you think if you had the main method in an abstract class that it would run?

你认为如果在抽象类中有main方法,它会运行吗?

public abstract class DoesItRun { public static void main(String[] args) { System.out.println("Does it run?"); } } view rawDoesItRun.java hosted with ❤ by GitHub

I didn’t think it would either but in fact, it does run. I suggest you try it out for yourself. As you can see abstract classes have their place in Java. Similar to interfaces but used in a different way.

Where have you used Java interfaces?

我也不认为会,但事实上,它确实运行我建议你自己试试。正如您所看到的,抽象类在Java中占有一席之地。类似于接口,但使用方式不同。

你在哪里使用过Java接口?

最新回复(0)