Register now or log in to join your professional community.
(Note: interface is a pure100% abstract class)
Which one is better to use abstract class or interface in java? - interface and abstract classes cannot be used interchangeably. Both of them have separate intentions in the Java programming language. Interfaces do not have any functionalities built into them, where as abstract classes have some of their functionalities (methods) already defined. Now whether you want to make a class as interface or abstract depends on how much of the classes functionality (methods) can be pre-determined and will not change. If at least one of the method that you define in a class can be predetermined (and you can code the same) then you can create an abstract class instead of an interface.
Which one is better to use? this will depend on urobject model. For example let us say that you have a class Animal and two classes Cat and Dog. A cat IS an Animal so here you should implement Animal as an abstract class. Furthermore, the Cat and the dog both eat. so you would want to implement the eat method once in animal and have ur Cat class and Dog class inherit the same eating method. So an abstract class represent an IS a relation due to the inheritance it generates. On the other hand, an interface is only a contract that you have to respect. It is less allienating than an abstract class. if you have a specific case please share it