Wednesday, January 06, 2010

Interface Vs Abstract class Java

Interface :
Interface is a group of related methods with empty bodies(Signatures). It is not a class. One class may implements multiple interfaces thus helps to achieve multiple inheritance. All the methods in interface are by default abstract with out any abstract key word.
Examples for interface :
interface vehicle {
void startVehicle(Sting commandToStart);
void stopVehicle(String commandToStop);
void speedUp(int speed);
to implement the above interface we can have mutiple classes like Bycycle, MotorBike, Car etc.
class MotorB{

Abstract Class :
A class declared as abstract - It may or may not contain abstract methods. It can't  be instantiated but can be subclassed. Means an abstract class can only be subclassed which mean it can be inherited from. In simple words other classes can inherit an abstract class but they can't initiate the abstract class. One class can extend only one abstract class.
Abstract method :

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:Abstract classes are classes that contain one or more abstract methods.

public abstract class vehicle{

//Non abstract method
void int speedUp(int speed){

spedd = speed + 10 ;


abstract void stopVehicle(String commandToStop);


public class thunderBird extends vehicle{

void stopVehicle(String commandToStop)




So in the above abstract class vehicle there are two methods one is speedUp which is non-abstract method and one abstract method stopVehicle . So the classes that extend this abstract class must define the abstract methods declared in abstract class other wise the class itself must declared as abstract class again.

An abstract method is a method that is declared, but contains no implementation.
Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.
An Abstract class without any implementation just looks like an Interface.

Abstract class can implement interface ..

abstract class jumbo implements vehicle {
  void startVehicle(Sting commandToStart){

start ;

void stopVehicle(String commandToStop){

stop ;

void speedUp(int speed){

speed = speed + 20;


Refer some more pages on the same topic here [1][2][3][4]

Technorati Tags:


  1. Pure abstract class is an interface :)

  2. nice blog boss....!

  3. Indeed you have done a great job on differentiating the interface and abstract class in java . just to add when we use abstract class we can not extend another class but with interface we have that space spare with us. just consider if Runnable were an abstract class instead of interface, how inflexible it would be.

    How HashMap works in Java

  4. Thanks Krishna! This helped my understanding a lot. This site also helped me very much with regard to interface vs abstract class:


Who ever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs u can at least use Name/URL option which don’t even require any sign-in, good thing is that it can accept your lovely nick name also and URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")
వీలయితే నాల్గు పోస్టులు ...కుదురితే ఒక కామెంటూ ...