Tuesday, November 29, 2011

What is the difference between interface and abstract class ?

An Abstract Class can contain default Implementation where as an Interface should not contain any implementation at all. An Interface should contain only definitions but no implementation. where as an abstract class can contain abstract and non-abstract methods. When a class inherits from an abstract the derived class must implement all the abstract methods declared in the base class. an abstract class can inherit from another non-abstract class.:An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.:a) All the methods declared inside an interface are abstract whereas abstract class must have at least one abstract method and others may be concrete or abstract. b) In abstract class, key word abstract must be used for the methods whereas interface we need not use that keyword for the methods. c) Abstract class must have subclasses whereas interface can?t have subclasses. :Abstract class can have methods definitions (declaration and its logic) but Interface can only have declarations. Abstract class can extend a Class or Interfaces but Interface can extend Interfaces only.:Abstract class is partial implementation whereas interface has method declaration only.
Category:Java Interview Questions

No comments:

Post a Comment