Java Abstract Class & Abstract Method

Abstraction is a feature which allows you to hide the implementation details from user and show only functionality. In Java abstraction is achieved through abstract classes and interfaces. Here we will discuss abstract class.

Java Abstract Class

In java the abstract class is declared with abstract keyword. The abstract class can have non-abstract and abstract methods. The abstract class cannot be instantiated. It is inherited by another class to provide the implementation of the abstract methods. When abstract class is inherited you need to provide implementations of every abstract method it contain.

Abstract Class Example

public abstract class Vehicle
{

//non-abstract methods

//abstract methods

}

Abstract Methods

In java abstract methods are declared with abstract keyword and they don’t have implementations. If you want to have a method in particular class but the actual implementation is to be determined by child class you can define it as abstract in parent class. A semi colon ( ; ) is used at the end of abstract method.

Abstract Method Example

abstract class Vehicle
{

abstract void run();

}

class SuzukiBike extends Vehicle{

void run(){

System.out.println(“The suzuki bike is running at 90km/h”);

}

public static void main(String args[]){

SuzukiBike bike = new SuzukiBike();

bike.run();

}

}

Output

The suzuki bike is running at 90km/h

Leave a Reply

Your email address will not be published. Required fields are marked *