Abstract Class Vs Interface
1) Abstract classes are used for Modelling a class hierarchy of similar looking classes
For example :- Animal can be abstract class and Human , Lion, Tiger can be concrete derived classes.
Interface is used for Communication between 2 similar / non similar classes which does not care about type of the class implementing Interface
For example:- Height can be interface property and it can be implemented by Human , Building , Tree. It does not matter they are living things or not.it matters only a thing that you need to have Height (implementation in you class).
2) By implementing interfaces you are achieving composition ("has-a" relationships) where as by abstract class you are achieving inheritance ("is-a" relationships).
3) methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior. i.e.abstract class may contain both abstract and concrete methods but You can not declare any concrete methods inside interface.
4) Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
5) Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc.
6) Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
7) A Java class can implement multiple interfaces but it can extend only one abstract class.
Which should you use, abstract classes or interfaces?
Consider using abstract classes if any of these statements apply to your situation
Consider using interfaces if any of these statements apply to your situation:
1) Abstract classes are used for Modelling a class hierarchy of similar looking classes
For example :- Animal can be abstract class and Human , Lion, Tiger can be concrete derived classes.
Interface is used for Communication between 2 similar / non similar classes which does not care about type of the class implementing Interface
For example:- Height can be interface property and it can be implemented by Human , Building , Tree. It does not matter they are living things or not.it matters only a thing that you need to have Height (implementation in you class).
2) By implementing interfaces you are achieving composition ("has-a" relationships) where as by abstract class you are achieving inheritance ("is-a" relationships).
3) methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior. i.e.abstract class may contain both abstract and concrete methods but You can not declare any concrete methods inside interface.
4) Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
5) Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc.
6) Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
7) A Java class can implement multiple interfaces but it can extend only one abstract class.
Which should you use, abstract classes or interfaces?
Consider using abstract classes if any of these statements apply to your situation
- You want to share code among several closely related classes.
- You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
- You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to your situation:
- You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
- You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
- You want to take advantage of multiple inheritance of type.
No comments:
Post a Comment