user1080519
user1080519

Reputation: 55

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.

Upvotes: 2

Views: 8104

Answers (5)

Nathan Hughes
Nathan Hughes

Reputation: 96385

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

Upvotes: 3

Tudor
Tudor

Reputation: 62439

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
}

Upvotes: 4

Leonardo
Leonardo

Reputation: 3191

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 !

Upvotes: 0

Egor
Egor

Reputation: 40193

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.

Upvotes: 2

Acidic
Acidic

Reputation: 6282

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

Upvotes: 0

Related Questions