Question: What are the differences between Interface and Abstract class?
Answer: Abstract Class Interfaces An abstract class can provide complete, default code and/or just the details that have to be overridden. An interface cannot provide any code at all,just the signature. In case of abstract class, a class may extend only one abstract class. A Class may implement several interfaces. An abstract class can have non-abstract methods. All methods of an Interface are abstract. An abstract class can have instance variables. An Interface cannot have instance variables. An abstract class can have any visibility: public, private, protected. An Interface visibility must be public (or) none. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. An abstract class can contain constructors . An Interface cannot contain constructors . Abstract classes are fast. Interfaces are slow as it requires extra indirection to find corresponding method in the actual class.
Question:
What are the differences between Interface and Abstract class? Answer:
Abstract Class Interfaces An abstract class can provide complete, default code and/or just the details that have to be overridden. An interface cannot provide any code at all,just the signature. In case of abstract class, a class may extend only one abstract class. A Class may implement several interfaces. An abstract class can have non-abstract methods. All methods of an Interface are abstract. An abstract class can have instance variables. An Interface cannot have instance variables. An abstract class can have any visibility: public, private, protected. An Interface visibility must be public (or) none. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. An abstract class can contain constructors . An Interface cannot contain constructors . Abstract classes are fast. Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Source: CoolInterview.com
Features 1)Methods An interface contains all the methods with empty implementation where as An abstract class must have at least one method with empty implementation. 2)Variables The variables in interfaces are final and static but in Abstract classes may contain both instance as well as static variables. 3)Multiple Inheritance In java multiple inheritance is achieved by using the interface (by implementing more than one interface at a time) Abstract classes does not provide this functionality. 4)Additional Functions If we add a method to an interface then we will have to implement this interface by any class In Abstract classes we can add a method with default implementation and then we can use it by extending the abstract class. Source: CoolInterview.com