Java中的“抽象类”是什么?


当前回答

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.

其他回答

来自oracle文档

抽象方法和类:

抽象类是声明为抽象的类——它可以包含也可以不包含抽象方法 抽象类不能被实例化,但可以被子类化

抽象方法是一种声明时没有实现(没有大括号,后面跟着一个分号)的方法,如下所示:

abstract void moveTo(double deltaX, double deltaY);

如果一个类包含抽象方法,那么类本身必须声明为抽象的,如下所示:

public abstract class GraphicObject {
   // declare fields
   // declare nonabstract methods
   abstract void draw();
}

当抽象类被子类化时,子类通常为其父类中的所有抽象方法提供实现。然而,如果不是这样,那么子类也必须声明为抽象的。

由于抽象类和接口是相关的,看看下面的SE问题:

接口和抽象类的区别是什么?

我应该如何解释接口和抽象类之间的区别?

简单地说,您可以将抽象类看作是具有更多功能的接口。

你不能实例化一个接口,这也适用于一个抽象类。

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();
}

解决方案——基类(抽象)

public abstract class Place {

String Name;
String Postcode;
String County;
String Area;

Place () {

        }

public static Place make(String Incoming) {
        if (Incoming.length() < 61) return (null);

        String Name = (Incoming.substring(4,26)).trim();
        String County = (Incoming.substring(27,48)).trim();
        String Postcode = (Incoming.substring(48,61)).trim();
        String Area = (Incoming.substring(61)).trim();

        Place created;
        if (Name.equalsIgnoreCase(Area)) {
                created = new Area(Area,County,Postcode);
        } else {
                created = new District(Name,County,Postcode,Area);
        }
        return (created);
        }

public String getName() {
        return (Name);
        }

public String getPostcode() {
        return (Postcode);
        }

public String getCounty() {
        return (County);
        }

public abstract String getArea();

}

类,它可以有具体方法和非具体方法,即有主体和没有主体。

没有实现的方法必须包含'abstract'关键字。 抽象类不能被实例化。

抽象类是声明为抽象的类——它可以包含也可以不包含抽象方法。抽象类不能被实例化,但可以被子类化。

换句话说,用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();  
    }  
}