Difference Between Interface and Abstract Class
Interface | Abstract Class |
---|---|
An interface is an unimplemented class. | An abstract class is an incomplete class. |
All methods are by default public abstract. | An abstract class can have both abstract and non abstract methods. |
All variables by default public static final by default | Variables may be static final or may not be. |
They cannot have any implemented methods. | They can have any number of implemented methods. |
An interface must be implemented by the base class. | An abstract class must be extended by the base class. |
You need to override each method of an interface in the sub class. | You need to override abstract methods only. |
An interface cannot have a constructor | An abstract class can have a constructor. |
An interface can extend more than one interfaces. | An abstract class can implement more than one interface. |
Point To Remember
- Object of both an interface and an abstract class can not be made.
- An abstract class may not override all methods of an interface.
- An abstract class may not override any method of another abstract class.
- An abstract method in interface or abstract class can not be static.
Example : Use of Interface and Abstract Class
interface Interface{
int t = 100;
public abstract void show();
}
abstract class Abstract{
public Abstract(){
System.out.println("Constructor - Abstract Class ");
}
public abstract void print();
}
public class Test extends Abstract implements Interface{
public static void main(String args[])
{
Test obj = new Test();
obj.show();
obj.print();
}
@Override
public void show(){
System.out.println("Show()");
}
@Override
public void print(){
System.out.println("Print()");
}
}
Comments
Post a Comment