Concrete Class Vs Abstract class Vs Interface

Posted By : Nanda Ram | 24-May-2018

We don't have any idea about the implementation just we have requirements like we have to do this but do not know how to do then we should go for an interface.  
Example: Servlet

We know about implementation like we can do this task in this way but not completely ( just partially implementation ) then we should go for abstract class.
Example: Generic Servlet, Http-Servlet

We know about implementation completely & ready to provide service, then we should go for concrete class
Example: our own servlet

 

Difference between interface and abstract class

Interface
1. Every method present inside interface is by default public & abstract
2. The following modifiers are not allowed for interface methods-
    Strictfp, protected, static, native, private, final and synchronized
3. Every variable present inside interface is public, static final, by default whether we are declaring ( or ) not.
4. For variables of an interface, we can not declare these modifiers 
   volatile, transient, private, protected.
5. Initialization for the interface variables should perform at the time of declaration.
6. We can not take instance & static blocks into the interface. 
7. If we don't know anything about implementation just we have requirements specification then we should go for interface
8. Inside interface, we can't take constructor

 

Abstract Class
1. In the abstract class, variables need not be final, public & static.
2. In the abstract class, Every method need not be public & abstract. we can take concrete method also.
3. There is no restriction for abstract class method modifiers i.e, we can use any modifier.
4. The abstract class variable modifier can be anyone there is no boundation for that.
5. Inside the abstract class, we can take constructor.
6. Inside the abstract class, we can take static block & instance blocks.
7. Variables of the abstract class, there is no restriction like performing initialization at the time of declaration.  
8. We should go for abstract class when we talking about implementation but not completely or fully then we have to go for abstract class.

 interface A{  
void m1();//bydefault, public and abstract  
void m2();  
void m3();  
void m4();  
}  

abstract class B implements A{  
public void m3(){System.out.println("I am m3");}  
}  
    
class M extends B{  
public void m1(){System.out.println("I am m1");}  
public void m2(){System.out.println("I am m2");}  
public void m4(){System.out.println("I am m4");}  
}  

class Test5{  
public static void main(String args[]){  
A a=new M();  
a.m1();  
a.m2();  
a.m3();  
a.m4();  
}}  

 

About Author

Author Image
Nanda Ram

Nanda Ram is a Java Developer, he keep conscientious about his task to complete it in a effective and efficient manner .

Request for Proposal

Name is required

Comment is required

Sending message..