Abstract class and Interface problem!

Dear all,

I would like to ask what the difference between abstract class and interface.

Which situation I should define a interface and which situation should define a abstract class?

Please give me an example.

Thanks a lot!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Abestrct class is a class which contain at least one abstract method

which extends a abstract class and allows derived classes to implement the abstract methods and it won't provide implementation.

Abstract classes may contain non abstract methods

Interface will contain will contain pure abstract methods.
Interface won't contain non abstact methods.

IF your class contain all abstract methods, use interfaces.
IF your class contain at least one non abstract method and at least one abstract method, use abstract class

Best of luck
I consider interface more of a tool to share the same method signatures for different classes.  
with interfaces you can  simulate multiple inheritances. If you think you are defining the same kind of methods ( methods which do simlar stuff except they work on instance variables defined at that level ) create an interface with a meaningful method signature and try to make sure anybody implementing types of your classes also implements the interface.
interfaces can't have method bodies in.
abstract classes can, but must still have some abstract declarations.

i use interfaces when i want to provide a definetion of a class, but all the direct subclasses will have different enough implementations so that no method modies should be written.

i use abstract classes when some implementation details will be shared amongst all or pretty much all of the direct subclasses, so that you will need to override a method instead of implement it.

interfaces are also useful as marker classes to group collections of classes together so that you can store them in arrays or other data structrures.
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  As all the guys told,both abstract class and interface will delcare the methods which all the derived classes has to override.
   In abstract class,
  it contains atleast one abstract method(ie method prototype,but no implementation) and all the derived classes extending that abstract classes has to override that abstract method.Otherwise the derived class will become abstract.Abstract classes can contain normal methods(ie mehtods with implementations).That normal derived class dont need to override.
   In Interface it can contain only method prototype,no implementation at all.By default all the methods in interface becomes public and abstract.
Thats why if you implementing the interface and that while overriding the interface methods you have define that methods as public,since in jave you can not reduce visibility.
   In interface all the methods are be default public and abstract and all the variables are by default public,static and final.So you can only use the variables and you can not change the value.
   In abstract normally,it should contain atlest one abstract method.That time compilier enforce you to declare the class as abstract.Even the abstract class can exist without any abstract methods.That time the compiler wont enforce you to declare the class as abstract.That time if you declare the class as abstract,it wont give any error.The only thing is you cannot create instances of that abstract classes.
   So both abstract class and interface are used to define the methods which can be overriden by all the derived classes.But in abstract class you can define normal methods also.But interface,by default all the methds are abstract.So for that reason you can say interface is a pure abstract class,otherwise one step ahead of abstract class.
   Refer Thinking in Java book for more information about Abstract class and interfaces in the context of polymorphism.
abstract class Exam
  public void abstract display();
public class DerExam1 extends Exam
  public void display()
    System.out.println("inside DerExam1");
public class DerExam2 extends Exam
  public void display()
    System.out.println("inside DerExam2");
public class DerExam
   public static void main(String []args)
  Exam e=new DerExam1();
  e=new DerExam2();
   By having the handle of the base class,you can call methods in derived class using polymorphism.
   In the same example instead of Exam class define Exam interface and implement that,It will work out.
   I hope u got some idea about Interface and polymorphism.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
[Hi benson - your other question has got rather technical so I'll try to keep it simple here :)]

A good basic principle to use is this:


Use an interface to group a number of classes into one parent type (such as sheep and wolves into animals for example).

Use inheritance and possibly an abstract base class when you are refining the behaviour of the same type of object. For example if you have a sheep object and you want to divide your sheep into two subclasses such as sheep with horns and sheep without horns.

One way to look at it is how much of the code I inherit from the base class is actually useful to me.

If you inherit lots of code and are only changing one method for example then it is practical to inherit from super class or abstract base class.


If you abstract class has no implementations or you find that you always need to override the base class implementations in most cases then your code will be more flexible by using an interface.


Another point is, if your classes need to inherit from more than one thing then you should use an interface.

For example, if you need to express information about animals your subclasses (such as sheep) might have the need to inherit from two things:


In this case if you use an abstract base class you cannot make your sheep class be of type animal and of type herbivore.

You may want to do this, because you might want to deal with animals that eat grass differently to animals that eat other animals.

If you use interfaces in Java, then you can do this.

Does this help?

>> [Hi benson - your other question has got rather technical so I'll try to keep it simple here

Sorry, ignore this line of my above comment - got it confused in with another answer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.