Java中的“抽象类”是什么?
当前回答
简单地说,您可以将抽象类看作是具有更多功能的接口。
你不能实例化一个接口,这也适用于一个抽象类。
On your interface you can just define the method headers and ALL of the implementers are forced to implement all of them. On an abstract class you can also define your method headers but here - to the difference of the interface - you can also define the body (usually a default implementation) of the method. Moreover when other classes extend (note, not implement and therefore you can also have just one abstract class per child class) your abstract class, they are not forced to implement all of your methods of your abstract class, unless you specified an abstract method (in such case it works like for interfaces, you cannot define the method body).
public abstract class MyAbstractClass{
public abstract void DoSomething();
}
否则,对于抽象类的普通方法,“继承者”可以像往常一样只使用默认行为或重写它。
例子:
public abstract class MyAbstractClass{
public int CalculateCost(int amount){
//do some default calculations
//this can be overriden by subclasses if needed
}
//this MUST be implemented by subclasses
public abstract void DoSomething();
}
其他回答
简单地说,您可以将抽象类看作是具有更多功能的接口。
你不能实例化一个接口,这也适用于一个抽象类。
On your interface you can just define the method headers and ALL of the implementers are forced to implement all of them. On an abstract class you can also define your method headers but here - to the difference of the interface - you can also define the body (usually a default implementation) of the method. Moreover when other classes extend (note, not implement and therefore you can also have just one abstract class per child class) your abstract class, they are not forced to implement all of your methods of your abstract class, unless you specified an abstract method (in such case it works like for interfaces, you cannot define the method body).
public abstract class MyAbstractClass{
public abstract void DoSomething();
}
否则,对于抽象类的普通方法,“继承者”可以像往常一样只使用默认行为或重写它。
例子:
public abstract class MyAbstractClass{
public int CalculateCost(int amount){
//do some default calculations
//this can be overriden by subclasses if needed
}
//this MUST be implemented by subclasses
public abstract void DoSomething();
}
除了这些帖子之外。
有时你可能想声明一个 类,但不知道如何定义 所有的方法都属于这个 类。例如,你可能想要 声明一个名为Writer和的类 在它中包含一个名为 写()。但是,您不知道如何编写write()代码,因为它就是 每种类型的作者都不一样 设备。当然,你打算处理 通过派生Writer的子类, 如打印机,磁盘,网络和 控制台。
An abstract class is one that isn't fully implemented but provides something of a blueprint for subclasses. It may be partially implemented in that it contains fully-defined concrete methods, but it can also hold abstract methods. These are methods with a signature but no method body. Any subclass must define a body for each abstract method, otherwise it too must be declared abstract. Because abstract classes cannot be instantiated, they must be extended by at least one subclass in order to be utilized. Think of the abstract class as the generic class, and the subclasses are there to fill in the missing information.
在这里找到你的答案:
Java中的抽象类与接口
抽象类可以有final方法吗?
顺便说一句,这是你最近问的问题。思考一个建立声誉的新问题……
编辑:
刚刚意识到,这个和参考问题的海报有相同或至少相似的名称,但用户id总是不同的。所以,要么,有一个技术问题,键盘有问题再次登录,并找到他的问题的答案,或者这是一种娱乐So社区的游戏;)
抽象类是声明为抽象的类——它可以包含也可以不包含抽象方法。抽象类不能被实例化,但可以被子类化。
换句话说,用abstract关键字声明的类在java中称为抽象类。它可以有抽象方法(没有主体的方法)和非抽象方法(有主体的方法)。
重要提示: 抽象类不能用于实例化对象,它们可以用于创建对象引用,因为Java的运行时多态性方法是通过使用超类引用实现的。因此,必须能够创建对抽象类的引用,以便可以使用它指向子类对象。您将在下面的示例中看到该特性
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){
System.out.println("running safely..");
}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
推荐文章
- 如何使一个Java通用方法静态?
- for-each循环和迭代器,哪个更有效?
- 泛型类中的静态方法?
- 如何在JPA中持久化类型列表<字符串>的属性?
- 什么是依赖倒置原则?为什么它很重要?
- 考虑在配置中定义一个'package'类型的bean [Spring-Boot]
- Java注释中的/**和/*
- java8 LocalDate Jackson格式
- Android Studio谷歌JAR文件导致GC开销限制超过错误
- 如何在Intellij生成串行版本UID
- “比较法违反其总合同!”
- 从Java项目生成UML类图
- 正确地从一个<Integer>的列表中移除一个整数
- Java开关语句:需要常量表达式,但它是常量
- Java的assertEquals方法可靠吗?