What is the purpose of an abstract method?

时间秒杀一切 提交于 2020-01-02 09:29:11

问题


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.


回答1:


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."




回答2:


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
}



回答3:


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.




回答4:


To force any non-abstract class that inherits from it to implement the method, similar to an interface.




回答5:


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 !



来源:https://stackoverflow.com/questions/9556184/what-is-the-purpose-of-an-abstract-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!