What is an Abstract Class? A class defined in such a way such that it defines the basic structure of an object but does not provide any implementation details about at least one of its method. Any method in an abstract class which is devoid of any implementation details is called abstract methods. The specific implementation of each of those abstract methods is provided by its subclasses. An example of abstract class is as follows,- Code: abstract class A { abstract void call( ); } class B extends class A { void call( ) { system.out.println("Inside class B"); } } What is an Interface? An interface in Java defines the set of methods and properties but does not define the specific functionality of each of those properties or methods. The class which inherits an interface defines them. Java class cannot inherit from multiple classes but can inherit more than one interface. Note: In interface all the methods are by default public and abstract whereas variables are final and static. An example:- Code: interface A { void test(int p); } interface B { void test1(int q); } class C implements interfaceA,interface B { public void test(int p) { system.out.println("Interface A called with" +p); } public void test1( int q) { system.out.println("Interface A called with" +q); } } Similarities between abstract class and interface We cannot create object for both abstract class and interface. We can create object references for both interface and abstract class. Both provide blue print of an object without implementation details. Both forces subclasses to define the behavioral aspect of each of the abstract methods. Difference between abstract class and interface An abstract class only need to have one method as abstract and can contain other implemented methods like like any other non abstract class whereas interface has all of its methods as abstract. An abstract class can contain variables and methods which are public, private, protected where as interface methods are always public and variables are always static and final which implies that you should never put any variables inside Interfaces. When to use what? Ideally abstract class is used when you can have a default implementation of the methods whereas interface defines a contract which can be implemented in various ways. To explain this let us consider an example: Shape is an interface where area() is a behavior. Triangle and circle are two classes which have their own way of calculating area. Thus each of those class has its way of own implementing the area but we need to make sure each of our such class has the area implementation. Interface acts as a contract that forces class to implement the area functionality. Now consider content as an abstract class where print( ) is a behavior. Here print() from the base class can act as a default behavior to all the subclasses and there is no need of an extra specialized behavior but if needed extra behavior can be added. Abstract class is preferred in this scenario.
Hai Debasmtia !!!!!!!!!!!!!!!! This is Ajay. Very nicely framed. I also read the article on Java access specifier. That was good. But this one is too good. Please tell me how to post an article ? please !!!!!!!!!!!
An interface in the Java programming language is an abstract type that is used to specify an interface.
hey dear, I am newbie in Java. Can you explain the above concept in more simplified thing. I understood the thing, but i need some more clarification. Please help me out.