-3

Any body can tell me

when to use the abstract class and when to use interface?

So many websites having only differences. I am not able to get these terms

"when to use the abstract class and when to use interface"

Thanks in Advance

  • 2
    possible duplicate of [Use of Java \[Interfaces / Abstract classes\]](http://stackoverflow.com/questions/2869222/use-of-java-interfaces-abstract-classes) – Pradeep Simha Aug 23 '15 at 07:18
  • 2
    http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo – Codebender Aug 23 '15 at 07:20

2 Answers2

1

Interfaces

An interface is kinda* like a template. Say for example that you want to make a 'Shape' class. Not all shapes use the same formulas for calculating area, so you just establish that there has to be a "getArea" method, but you don't define it.

A simple example:

public interface Shape
{

    public int getArea();

}

Then you can have a class that implements the Shape interface:

public class Rectangle implements Shape
{
    //this works for rectangles but not for circles or triangles
    public int getArea()
    {
       return this.getLength() * this.getHeight();
    }

}

Abstract Classes

Abstract methods can be extended by subclasses.* They differ from interfaces in that they can also contain defined methods.

You can still leave undefined methods, but you must label them abstract.

An example:

public abstract class Vegetable 
{
    public String vegName;
    public boolean edible = true;

    public Vegetable(final String vegName, final boolean edible)
    {
        this.vegName = vegName;
        this.edible = edible;
    }

    public void printName()
    {
        System.out.println(this.vegName);
    }

    //to be determined later when implemented
    public abstract void drawOnScreen();

}

Then we can extend this abstract class.

public class Carrot extends Vegetable
{
    //we must define the abstract methods
    public void drawOnScreen()
    {
        //we can still use our other methods
        this.printName();

        //do some other thing that is specific to this class
    }
}
0

Interfaces cannot contain implementation (at least prior to Java 8) so if you need "common" method implementation, you have to have it in a super class (be it abstract or concrete)

However, any class can have only one super class (but many interfaces). so an interface is the solution for polymorphism when you have a class that already got a super class

Sharon Ben Asher
  • 12,476
  • 5
  • 25
  • 42
  • Does your answer anyhow reflects when to use interface/abstract class and when not to. You just give the definition of that :-O !!! – royki Aug 23 '15 at 07:53
  • @Altius: when to use super class: "if you need "common" method implementation", when to use interface: "when you have a class that already got a super class" – Sharon Ben Asher Aug 23 '15 at 08:47
  • I got your point but your answer certainly doesn't reflect that I think you need to modify and elaborate your answer – royki Aug 23 '15 at 10:47
  • what do you mean "your answer certainly doesn't reflect that". I was qouting my answer. it is possible that it can benefit from elaboration, but it DOES answer the question (even better than the longer other answer, imo) and i think the downvote is uncalled for. – Sharon Ben Asher Aug 23 '15 at 11:03