1

Possible Duplicate:
When to use interfaces or abstract classes? When to use both?

Consider this

public abstract class Flat
{
    //some properties


    public void livingRoom(){
       //some code
    }

    public void kitchen(){
       //some code
    }

    public abstract void bedRoom();

}
An implementation class would be as follows:

public class Flat101 extends Flat
{
        public void bedRoom() {
            System.out.println("This flat has a customized bedroom");
       }        

}

Alternatively I can use an interface instead of an abstract class to achieve the same purpose like follows:

class Flat
{
  public void livingRoom(){ 
       System.out.println("This flat has a living room");
  }

  public void kitchen(){
     System.out.println("This flat has a kitchen");
 } 

}

interface BedRoomInterface
{
     public abstract void bedRoom();
}

public class Flat101 extends Flat implements BedRoomInterface
{
       public void bedRoom() {
      System.out.println("This flat has a customized bedroom");
       }
}

Now the question is : For this setup why should choose to use an interface (or) why should I choose to use an abstract class?

Community
  • 1
  • 1
sai sindhu
  • 1,155
  • 5
  • 20
  • 30

1 Answers1

3

Generally speaking, use abstract classes when you want a place to put common logic that can be reused between implementations. Otherwise, use an Interface.

Then there are a number of exception, often closely related to design patterns. But keep it simple.

Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148