i am learning about the difference between abstract classes and interfaces,
but theoretical explanations are hard to understand when you don’t have practical example.
i have read that:
By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
if can someone please supply me an practical example for this lines i will be very thankful,
Wishing you a pleasant week.
i am learning about the difference between abstract classes and interfaces, but theoretical explanations
Share
Consider this example. We have a drivable interface, automobile abstract, and a Pinto class that uses both.
We can easily add methods to all automobiles by defining them in
Automobile. Any new method added there will be available to all extending classes.However, if we add a method to the Drivable interface, we must seek out and implement said method in every class that implements it to add the new method or else it won’t compile.
For example, say we add a
stop()method to our interface.Pintowill immediately raise errors because it doesn’t have astopmethod.