Friday, August 22, 2008

==> Interface vs Abstract Class

Interfaces

Interfaces can be used to implement the Inheritance relationship between the non-related classes that do not belongs to the same hierarchy, i.e. any Class and any where in hierarchy. Using Interface, you can specify what a class must do but not how it does.

-> A class can implement more than one Interface.

-> An Interface can extend one or more interfaces, by using the keyword extends.

-> All the data members in the interface are public, static and Final by default.

-> An Interface method can have only Public, default and Abstract modifiers.

-> An Interface is loaded in memory only when it is needed for the first time.

-> A Class, which implements an Interface, needs to provide the implementation of all the methods in that Interface.

-> If the Implementation for all the methods declared in the Interface are not provided , the class itself has to declare abstract, other wise the Class will not compile.

-> If a class Implements two interface and both the Intfs have identical method declaration, it is totally valid.

-> If a class implements tow interfaces both have identical method name and argument list, but different return types, the code will not compile.

-> An Interface can’t be instantiated. Intf Are designed to support dynamic method resolution at run time.

-> An interface can not be native, static, synchronize, final, protected or private.

-> The Interface fields can’t be Private or Protected.

-> A Transient variables and Volatile variables can not be members of Interface.

-> The extends keyword should not used after the Implements keyword, the Extends must always come before the Implements keyword.

-> A top level Interface can not be declared as static or final.

-> If an Interface species an exception list for a method, then the class implementing the interface need not declare the method with the exception list.

-> If an Interface can’t specify an exception list for a method, the class can’t throw an exception.

-> If an Interface does not specify the exception list for a method, he class can not throw any exception list.

Abstract Class

Abstract classes can be used to implement the inheritance relationship between the classes that belongs same hierarchy.Abstract Class means - Which has more than one abstract method which doesn’t have method body but at least one of its methods need to be implemented in derived Class.

-> Classes and methods can be declared as abstract.

-> Abstract class can extend only one Class.

-> If a Class is declared as abstract , no instance of that class can be created.

-> If a method is declared as abstract, the sub class gives the implementation of that class.

-> Even if a single method is declared as abstract in a Class , the class itself can be declared as abstract.

-> Abstract class have at least one abstract method and others may be concrete.

-> In abstract Class the keyword abstract must be used for method.

-> Abstract classes have sub classes.

-> Combination of modifiers Final and Abstract is illegal in java.

Difference Between Interfaces And Abstract class

-> All the methods declared in the Interface are Abstract, where as abstract class must have atleast one abstract method and others may be concrete.

-> In abstract class keyword abstract must be used for method, where as in Interface we need not use the keyword for methods.

-> Abstract class must have Sub class, where as Interface can’t have sub classes.

-> An abstract class can extend only one class, where as an Interface can extend more than one.

No comments: