<-Pre | Home | Next-> |
What is Abstract class?
An abstract class is a class that is declared abstract.It may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
What is Interface?
An interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.
What is the difference between interface and an abstract class?
1> An abstract class is a class that is declared abstract.It may or may not include abstract methods.
A Java Interface can contain only method declarations and public static final constants and doesn’t contain their implementation. The classes which implement the Interface must provide the method definition for all the methods present.
2> Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses. Interfaces are useful in a situation when all its properties need to be implemented by subclasses.
3> All variables in an Interface are by default - public static final while an abstract class can have instance variables.
4> An interface is also used in situations when a class needs to extend an other class apart from the abstract class. In such situations its not possible to have multiple inheritance of classes. An interface on the other hand can be used when it is required to implement one or more interfaces. Abstract class does not support Multiple Inheritance whereas an Interface supports multiple Inheritance.
5> An Interface can only have public members whereas an abstract class can contain private as well as protected members.
6> A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class.
7> The problem with an interface is, if you want to add a new feature (method) in its contract, then you MUST implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass.
Neither Abstract classes nor Interface can be instantiated.
What is an abstract method?
An abstract method is a method whose implementation is deferred to a subclass.
What is meant by “Abstract Interface”?
Firstly, an interface is abstract. That means you cannot have any implementation in an interface.
All the methods declared in an interface are abstract methods or signatures of the methods.
Can Abstract Class have constructors? Can interfaces have constructors?
Abstract class’s can have a constructor, but you cannot access it through the object, since you cannot instantiate abstract class. To access the constructor create a sub class and extend the abstract class which is having the constructor.
Example
public abstract class AbstractExample {
public AbstractExample(){
System.out.println(”In AbstractExample()”);
}
}
public class Test extends AbstractExample{
public static void main(String args[]){
Test obj=new Test();
}
}If interface & abstract class have same methods and those methods contain no implementation, which one would you prefer?
Obviously one should ideally go for an interface, as we can only extend one class. Implementing an interface for a class is very much effective rather than extending an abstract class because we can extend some other useful class for this subclass.
Pre->class-objects | Next->package |
*****Please feel free to give feedback at shrawan.iitg@gmail.com and comments below *****
No comments:
Post a Comment