• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Diff bw Abstract class and Interface

 
mani senthil
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly tell me the diffrence between Abstract class and Interfac.
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick trip to the FAQ brings up this: http://faq.javaranch.com/view?InterfaceVsAbstractClass
 
Urs Waefler
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

There are several differences between abstract classe and interface.

I think, one main difference is, an abstract class can contain non-abstract methods. In an interface every method is abstract.

You can not instantiate an abstract class. Also an interface you can not instantiate.

If one method is abstract, the class must be abstract. But an abstract class does not have to contain any abstract method.

In a non-abstract class there is no abstract method.

In an interface every method is abstract an public.

Regards
Urs
 
Campbell Ritchie
Sheriff
Posts: 51468
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite true that you can't instantiate an abstract class or an interface. You can't instantiate them directly, but you can use them, to create anonymous classes. Example. Runnable is an interface in java.lang which has one method called . . . void run().
You will see several instances in the Swing part of the Java tutorials where they start their application like this:-To instantiate an interface you have to give it a class body with (){} and implement all its methods inside the {}.
Because you implement its methods that is why you say public class Foo implements BarInterface.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic