简体   繁体   中英

What is the purpose of an abstract method?

abstract public class car 
{   
    abstract void drive();
}

As in the code snippet above, what exactly is the purpose of an abstract method in Java? From what I can gather, by definition, they aren't allowed to have bodies.

By declaring a method abstract you are not providing an implementation, but you are forcing concrete classes that extend the car class to provide an implementation to the method. Example:

abstract public class Car {
    abstract void drive();
}

public class Audi extends Car {
    void drive() {
        System.out.println("I'm an Audi.");
    }
}

public class Volvo extends Car {
    void drive() {
        System.out.println("I'm a Volvo.");
    }
}

Failure to provide the implementation will cause a compilation error.

Now, from this example you can easily see that, since instances of both Audi and Volvo can be placed where a Car is expected, you can plug in different behaviors at runtime (this is called polymorphism):

void driveCar(Car car) {
    car.drive();
}

void testDrive() {
    driveCar(new Audi()); // prints I'm an Audi
    driveCar(new Volvo()); // prints I'm a Volvo
}

When you make things implement the same abstract class, you are saying, these things are alike at a high level, but they vary in the particulars of how they do things. An abstract method is how you say, "Here's something that all the things that extend this class have to do, but they each get to specify how exactly they will do it."

Abstract methods should be implemented in subclasses of this abstract class. For example, your class is named Shape and it has a draw() method. You can't implement this method for the Shape class cause you don't really know how to draw a Shape , so you make it abstract . And when you're creating, say, Triangle class that extends Shape - you're sure how to draw a Triangle and you can implement the draw() method. Hope this helps.

强制从其继承的任何非抽象类实现该方法,类似于接口。

Abstract works as a "shape" class, where all methods listed (like drive() ) must be implemented, or you will see some errors compiling your program. This kind of class CAN'T be instantiated, so you need to inheritate this class and instatiate all the abstract methods. The advantage of using abstract is, you can have the abstract class as a shape class, which you can inheritate to some classes without the need to rewrite your methods, as they are already there. Don't confuse rewrite with implemente, because you must implement this methods. An example: You have an animal class:

abstract public class Animal{
    abstract void eat();
    abstract void walk();
}

So, if you inherit this, automatically you will need to implement this methods. See below:

public class Horse extends Animal{
        void eat() { //place the eating code }
        void walk() { /// place the walking code }
}

Hope you understand !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM