MY mENU


Thursday 9 February 2012

Difference between Interface and Abstract class in Java


1. Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also i.e. abstract class may contain concrete 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 class definition begins with the keyword "abstract" keyword followed by Class definition. An Interface definition begins with the keyword "interface".

3. 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

4. All variables in an Interface are by default - public static final while an abstract class can have instance variables.

5. 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.

6. An Interface can only have public members whereas an abstract class can contain private as well as protected members.

7. 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.

8. 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

9. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast

10.Interfaces are often used to describe the peripheral abilities of a class, and not its central identity, E.g. an Automobile class might
implement the Recyclable interface, which could apply to many otherwise totally unrelated objects.

Note: There is no difference between a fully abstract class (all methods declared as abstract and all fields are public static final) and an interface.

Note: If the various objects are all of-a-kind, and share a common state and behavior, then tend towards a common base class. If all they
share is a set of method signatures, then tend towards an interface.

Similarities:

Neither Abstract classes nor Interface can be instantiated.

Different answers:


Abstraction:
Abstraction is a way of converting real world objects in terms of class. abstract is a keyword. It is not fully defined or implemented. We cannot create object forabstract class. For eg:
abstract class A
{
abstract void show();
}
calss B extends A
{
void show() // Should be overrided
{
c=10; // Must be define a method
System.out.println(c);
}
}

Interface:
In java we cannot use multiple inheritance. Instead of that we are using interface in java.
-It is pure abstraction.
-Method declaration.
-No need to create object.
-Support multiple inheritance.
-here we can using the access specifier is public and default other access specifier we cannot use.
-It is also a keyword.
-Each and every method in the interface is abstract. So no need to declareabstract explicitly.

For eg:
interface A
{
void show();
void disp();
}
class B implements A
{
show()
{
a=10;
System.out.println(a);
}
disp()
{
b=20;
System.out.println(b);
}
public static void main(String s[])
{
A a=new A();
a.show();
a.disp();
}
}
abstract class means its a partially developed class. It should have subclass immediately.It may have declaration of abstract and concrete methods.

Interface is a pure abstract class that means only declarations are allowed.The access specifier must be public.

No comments:

Post a Comment