Monday, 23 April 2018



what is the difference between interface/abstract class and concrete class..........

===============================================================================================
interface:-
if we dont know anything about implementation just we have requirement specification
 (100% abstraction)  then we should go for interface.
example:- Servlet(Interface)

abstract class:-
if we are  talking about implementation but not completely
(partial implementation) then we should go for abstract class.
example:-GenericServlet/HttpServlet class.

Concrete class:-
if we are  talking about implementation  completely and ready to provide service
 then we should go for concrete  class.

example:-MyOwnServlet

================================================================================================
//NameAbstract.java//abstract class
//NameInterface.java//interface
//NameConcrete.java//concrete class
//NameMain.java
========================================================================

public abstract class NameAbstract {

public abstract String firstName();
public String lastname()
{
return "khan";
}

}

==============================================================================


public interface NameInterface {

public String middlename();

}


===============================================================================


public class NameConcrete extends NameAbstract implements NameInterface {

@Override
public String middlename() {
return "ballabh";
}

@Override
public String firstName() {
return "govind";
}


}


===============================================================================


public class NameMain {
public static void main(String[] args) {
NameConcrete nc=new NameConcrete();
String s1=nc.firstName();
String s2=nc.middlename();
String s3=nc.lastname();
System.out.println("Full name="+s1+" "+s2+" "+s3);
}

}


===============================================================================

o/p:-
Full name=govind ballabh khan


note:-

Abstract class :-----------
You cannot instantiate an abstract class.
An abstract class may contain abstract methods.
You need to inherit an abstract class to use it.
Concrete class:-----------
You can instantiate a concrete class.
A concrete class doesn’t have any abstract methods.
It is not mandatory to inherit a concrete class to use it.

No comments:

Post a Comment