Lately I was a few times asked a question "what do we need abstract classes for?"
And today I've got one more question, which inspired me to write this.
Let us have a task to write an application, which will perform some simple arithmetic operations on numbers, represented in a different numeric systems.
For example, our program should be able to add numbers in roman, arabic and hexadecimal systems. But later we should be able to add more operations, like division, multiplication and subtraction.
This is where an abstract class comes to help us!
Rather than write an interfaceNumber, which will define the addition operator and then implementing it for each class like RomanNumber and HexadecimalNumber we will better use an abstract class, which will be able to add decimal numbers and will declare abstract method to convert number itself to decimal system.
Take a look:
…and compare it to this:
This is how we can create an abstraction: we can add or perform any other arithmetic operations regardless on which numeric system we use!
Wneh we declare an
interface, we can’t tell how to create an object, implementing that interface. E. g., we can not define
even a default constructor.
Thus, when we need to have at least one constructor of a defined signature, we must use