Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/articles/java-tutorials/)
-   -   Interface Vs Abstract Class (http://www.go4expert.com/articles/interface-vs-abstract-class-t28024/)

Debasmita 15Mar2012 14:08

Interface Vs Abstract Class

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,-

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:-

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.

Ajay12 8May2012 17:37

Re: Interface Vs Abstract Class
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 !!!!!!!!!!!

mfred90 29May2012 18:11

Re: Interface Vs Abstract Class
An interface in the Java programming language is an abstract type that is used to specify an interface.

Healthcare 14Jun2012 09:38

Re: Interface Vs Abstract Class
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.

All times are GMT +5.5. The time now is 05:46.