Interviews2 answers
When do we use interface and when do we use abstract class?
2 Answers
Interface
It is used to achieve total abstraction.
Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .
It is also used to achieve loose coupling.
Interfaces are used to implement abstraction.
Abstract
The purpose of an abstract class is to define some common behavior that can be inherited by multiple subclasses, without implementing the entire class.
An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces
Nisha RegilPosted Sep 9, 2020, 1:35 AM EDT
Basically interface and abstract class both used for abstration.
abstract class
- Acstract class can not be initiated .
When you want to default behaviour to method then in abstract class we can do but in interface not possible
For example we want to provide basic calculation to implementing class then we can do in abstract class.
SwapnilPosted Jul 3, 2020, 8:30 PM EDT
