Thursday, January 20, 2011

Difference between abstract class and interace

It is my problem, I always forget the when to use interface and abstract classes. I used to develop so large applications, but I seem to be unaware of when is the time to use interface. Here are some notes to be clear about interface and abstract classes:

Creation of abstract classes:
1) The abstract class should have abstract keyword before the class name e.g.
public abstract class Animal
{
public void eat()
{
//method description
}
public void sleep(int miliseconds)
{
//do implementation
}
public abstract void makeNoise();
}
2) There should be at least one abstract method in the abstract class which is to be defined in the subclass. There are other methods also and we can implement those methods inside the abstract class. The subclass may implement the abstract classes according to their need.
Note: we can not create instances of abstract classes. Actually, it is not appropriate to give provision of creating instances of abstract classes because it contains unimplemented abstract methods and JVM will confused what to do if those methods were called by the instances.

Creation of Interfaces
1) The keyword interface is used to declare interfaces. The interfaces are created similar ways as classes are created.
public interface Player
{
public String name();
public int numberToken();
public void takeTurn(Pile pile, int maxOnATurn);
}
Note: the interface methods are public and abstract. So, we can omit the public and abstract keywords also i.e. the below code is equivalent to above code:
public interface Player
{
public abstract String Name();
int numberToken();
abstract takeTurn(Pile pile, int maxOnATurn);
}
2) The method implementation is not allowed. The implementation is carried out in implementing class.
Note: there is no meaning of creating private members in interface because they are inaccessible in the implementing class. And it is compulsory for the implementing class to implement all the methods in the interface.

The implementation is carried out in the following way
class lazyPlayer implements Player
{
public string name()
{
//implement here
}
public int numberToken()
{
//
}
public void takeTurn(Pile pile, int maxOnATurn)
{
//
}

}

From above, we got the main differences on how the abstract classes and interfaces are created.

Abstract Class and Interface

1) Abstract class is similar to class except we cant create instances, but interface is just the declaration of methods which to be implemented by implementing class.

2) Since, Java does not support multiple inheritance, but we can implement multiple interfaces.

3) We select abstract class in case when we need to define only a few methods in subclass (specialisation) while using all other methods defined by superclass. But, in interface, we can't use any method from interface because no methods are implemented in interface, we need to implement in the implementing class.

I will add more notes here if I get something new regarding interface and abstract class.
Your Comments are Welcome!!!

No comments:

Post a Comment