Java中的“抽象类”是什么?
当前回答
来自oracle文档
抽象方法和类:
抽象类是声明为抽象的类——它可以包含也可以不包含抽象方法 抽象类不能被实例化,但可以被子类化
抽象方法是一种声明时没有实现(没有大括号,后面跟着一个分号)的方法,如下所示:
abstract void moveTo(double deltaX, double deltaY);
如果一个类包含抽象方法,那么类本身必须声明为抽象的,如下所示:
public abstract class GraphicObject {
// declare fields
// declare nonabstract methods
abstract void draw();
}
当抽象类被子类化时,子类通常为其父类中的所有抽象方法提供实现。然而,如果不是这样,那么子类也必须声明为抽象的。
由于抽象类和接口是相关的,看看下面的SE问题:
接口和抽象类的区别是什么?
我应该如何解释接口和抽象类之间的区别?
其他回答
它是一个不能被实例化的类,并且强制实现类尽可能地实现它概述的抽象方法。
除了这些帖子之外。
有时你可能想声明一个 类,但不知道如何定义 所有的方法都属于这个 类。例如,你可能想要 声明一个名为Writer和的类 在它中包含一个名为 写()。但是,您不知道如何编写write()代码,因为它就是 每种类型的作者都不一样 设备。当然,你打算处理 通过派生Writer的子类, 如打印机,磁盘,网络和 控制台。
使用abstract关键字声明的类称为抽象类。 抽象是一个隐藏数据实现细节,只向用户显示功能的过程。抽象让您关注对象做了什么,而不是它是如何做的。
抽象类的主要内容
An abstract class may or may not contain abstract methods.There can be non abstract methods. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: ex : abstract void moveTo(double deltaX, double deltaY); If a class has at least one abstract method then that class must be abstract Abstract classes may not be instantiated (You are not allowed to create object of Abstract class) To use an abstract class, you have to inherit it from another class. Provide implementations to all the abstract methods in it. If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
声明抽象类 在声明期间在类之前指定abstract关键字将使其抽象。看看下面的代码:
abstract class AbstractDemo{ }
声明抽象方法 在声明过程中在方法之前指定abstract关键字将使方法抽象。看看下面的代码,
abstract void moveTo();//no body
为什么我们需要抽象类
In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for ex -: position, orientation, line color, fill color) and behaviors (for ex -: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects (for ex : fill color, position, and moveTo). Others require different implementation(for ex: resize or draw). All graphic objects must be able to draw or resize themselves, they just differ in how they do it.
对于抽象超类来说,这是一个完美的情况。您可以利用这些相似性,并将所有图形对象声明为继承自相同的抽象父对象(例如:GraphicObject),如下图所示。
首先,声明一个抽象类GraphicObject,以提供所有子类完全共享的成员变量和方法,例如当前位置和moveTo方法。GraphicObject还声明了抽象方法,如draw或resize,这些方法需要由所有子类实现,但必须以不同的方式实现。GraphicObject类看起来像这样:
abstract class GraphicObject {
void moveTo(int x, int y) {
// Inside this method we have to change the position of the graphic
// object according to x,y
// This is the same in every GraphicObject. Then we can implement here.
}
abstract void draw(); // But every GraphicObject drawing case is
// unique, not common. Then we have to create that
// case inside each class. Then create these
// methods as abstract
abstract void resize();
}
在子类中使用抽象方法 GraphicObject的每个非抽象子类,如Circle和Rectangle,必须为draw和resize方法提供实现。
class Circle extends GraphicObject {
void draw() {
//Add to some implementation here
}
void resize() {
//Add to some implementation here
}
}
class Rectangle extends GraphicObject {
void draw() {
//Add to some implementation here
}
void resize() {
//Add to some implementation here
}
}
在main方法中,你可以像这样调用所有的方法:
public static void main(String args[]){
GraphicObject c = new Circle();
c.draw();
c.resize();
c.moveTo(4,5);
}
在Java中实现抽象的方法
在java中有两种实现抽象的方法
抽象类(0到100%) 接口(100%)
具有构造函数、数据成员、方法等的抽象类
abstract class GraphicObject {
GraphicObject (){
System.out.println("GraphicObject is created");
}
void moveTo(int y, int x) {
System.out.println("Change position according to "+ x+ " and " + y);
}
abstract void draw();
}
class Circle extends GraphicObject {
void draw() {
System.out.println("Draw the Circle");
}
}
class TestAbstract {
public static void main(String args[]){
GraphicObject grObj = new Circle ();
grObj.draw();
grObj.moveTo(4,6);
}
}
输出:
GraphicObject is created
Draw the Circle
Change position according to 6 and 4
记住两条规则:
如果类中抽象方法和具体方法都很少, 将其声明为抽象类。 如果类只有抽象方法,则将其声明为接口。
引用:
TutorialsPoint - Java抽象 Java抽象类方法 Java文档-抽象方法和类 JavaPoint - Java中的抽象类
抽象类是不能被实例化的类。抽象类是通过创建可实例化的继承子类来使用的。抽象类为继承的子类做了一些事情:
定义可由继承子类使用的方法。 定义继承子类必须实现的抽象方法。 提供一个公共接口,允许子类与所有其他子类交换。
这里有一个例子:
abstract public class AbstractClass
{
abstract public void abstractMethod();
public void implementedMethod() { System.out.print("implementedMethod()"); }
final public void finalMethod() { System.out.print("finalMethod()"); }
}
注意,“abstractMethod()”没有任何方法主体。因此,你不能做以下事情:
public class ImplementingClass extends AbstractClass
{
// ERROR!
}
没有实现abstractMethod()的方法!因此,当JVM获得像new ImplementingClass(). abstractmethod()这样的东西时,它无法知道应该做什么。
这是一个正确的实现类。
public class ImplementingClass extends AbstractClass
{
public void abstractMethod() { System.out.print("abstractMethod()"); }
}
注意,您不必定义implementedMethod()或finalMethod()。它们已经由AbstractClass定义。
这是另一个正确的ImplementingClass。
public class ImplementingClass extends AbstractClass
{
public void abstractMethod() { System.out.print("abstractMethod()"); }
public void implementedMethod() { System.out.print("Overridden!"); }
}
在本例中,您重写了implementedMethod()。
但是,由于final关键字,不能执行以下操作。
public class ImplementingClass extends AbstractClass
{
public void abstractMethod() { System.out.print("abstractMethod()"); }
public void implementedMethod() { System.out.print("Overridden!"); }
public void finalMethod() { System.out.print("ERROR!"); }
}
您不能这样做,因为AbstractClass中finalMethod()的实现被标记为finalMethod()的最终实现:不允许任何其他实现。
现在你还可以实现一个抽象类两次:
public class ImplementingClass extends AbstractClass
{
public void abstractMethod() { System.out.print("abstractMethod()"); }
public void implementedMethod() { System.out.print("Overridden!"); }
}
// In a separate file.
public class SecondImplementingClass extends AbstractClass
{
public void abstractMethod() { System.out.print("second abstractMethod()"); }
}
现在你可以在某处写另一个方法。
public tryItOut()
{
ImplementingClass a = new ImplementingClass();
AbstractClass b = new ImplementingClass();
a.abstractMethod(); // prints "abstractMethod()"
a.implementedMethod(); // prints "Overridden!" <-- same
a.finalMethod(); // prints "finalMethod()"
b.abstractMethod(); // prints "abstractMethod()"
b.implementedMethod(); // prints "Overridden!" <-- same
b.finalMethod(); // prints "finalMethod()"
SecondImplementingClass c = new SecondImplementingClass();
AbstractClass d = new SecondImplementingClass();
c.abstractMethod(); // prints "second abstractMethod()"
c.implementedMethod(); // prints "implementedMethod()"
c.finalMethod(); // prints "finalMethod()"
d.abstractMethod(); // prints "second abstractMethod()"
d.implementedMethod(); // prints "implementedMethod()"
d.finalMethod(); // prints "finalMethod()"
}
注意,尽管我们将b声明为AbstractClass类型,但它显示为“override !”。这是因为我们实例化的对象实际上是一个ImplementingClass,它的implementedMethod()当然被重写了。(您可能已经看到这被称为多态。)
如果希望访问特定于特定子类的成员,必须先向下转换到该子类:
// Say ImplementingClass also contains uniqueMethod()
// To access it, we use a cast to tell the runtime which type the object is
AbstractClass b = new ImplementingClass();
((ImplementingClass)b).uniqueMethod();
最后,你不能做以下事情:
public class ImplementingClass extends AbstractClass, SomeOtherAbstractClass
{
... // implementation
}
一次只能扩展一个类。如果需要扩展多个类,则它们必须是接口。你可以这样做:
public class ImplementingClass extends AbstractClass implements InterfaceA, InterfaceB
{
... // implementation
}
下面是一个接口示例:
interface InterfaceA
{
void interfaceMethod();
}
这基本等同于:
abstract public class InterfaceA
{
abstract public void interfaceMethod();
}
唯一的区别是第二种方法不会让编译器知道它实际上是一个接口。如果你想让人们只实现你的接口而不实现其他接口,这可能很有用。然而,作为初学者的经验法则,如果你的抽象类只有抽象方法,你可能应该让它成为一个接口。
以下行为是非法的:
interface InterfaceB
{
void interfaceMethod() { System.out.print("ERROR!"); }
}
不能在接口中实现方法。这意味着如果实现两个不同的接口,这些接口中的不同方法不能冲突。由于接口中的所有方法都是抽象的,因此必须实现该方法,并且由于您的方法是继承树中唯一的实现,因此编译器知道它必须使用您的方法。
简单地说,您可以将抽象类看作是具有更多功能的接口。
你不能实例化一个接口,这也适用于一个抽象类。
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();
}
推荐文章
- 如何使一个Java通用方法静态?
- for-each循环和迭代器,哪个更有效?
- 泛型类中的静态方法?
- 如何在JPA中持久化类型列表<字符串>的属性?
- 什么是依赖倒置原则?为什么它很重要?
- 考虑在配置中定义一个'package'类型的bean [Spring-Boot]
- Java注释中的/**和/*
- java8 LocalDate Jackson格式
- Android Studio谷歌JAR文件导致GC开销限制超过错误
- 如何在Intellij生成串行版本UID
- “比较法违反其总合同!”
- 从Java项目生成UML类图
- 正确地从一个<Integer>的列表中移除一个整数
- Java开关语句:需要常量表达式,但它是常量
- Java的assertEquals方法可靠吗?