Welcome to Srini's blog

Thursday, April 15, 2010

Abstract Class Vs Interface -- Java

Interface :
1. Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body.Interfaces are designed to support dynamic method resolution at run time.
2. A class may implement several interfaces.
3. An interface cannot provide any code at all, much less default code.
4. Variables can be declared inside of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class. They must also be initialized with a constant value.
5. If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method.
6. An Interface can only have public members.
7. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class.

Abstarct Classs:
1. A superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass
to fill in the details. Such a class determines the nature of the methods that the subclasses must implement, Called abstract class.
2. A class may extend only one abstract class
3. An abstract class can provide complete code, default code, and/or just stubs that have to be overridden.
4. Both instance and static constants are possible.
5. If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.
6. Abstract class can contain private as well as protected members.
7. Abstract classes are fast.

No comments:

Post a Comment