I was going through the concepts of abstraction in Java.
These are my understandings:
These are my questions:
When an abstract class can behave like an interface when all the methods are made abstract, why do we need interface separately? Please explain with an example for better understanding.
Can we likely call Abstract class = Interface + Inheritance on a functionality basis? Because we can achieve the functionality of interface and inheritance together with Abstract class.
Simply saying: interface is a contract, abstract class is skeletal implementation. (Additionally, in Java interfaces are mostly used because it's not possible to extend multiple classes.)
Contract says what, implementation says how.
Example of interface: java.util.List
. It has all methods that any list should have: add()
, size()
, indexOf()
and so on.
Example of abstract class: java.util.AbstractList
. Though it has many abstract methods, some List
methods, that don't depend on the way elements are stored in the concrete list, are implemented there (addAll()
, equals()
, toString()
and others). To create complete implementation, not all List
methods should be implemented, thus making implementor's work easier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With