I have searched through the numerous S/O postings and have not found an answer that helps me.
I would like to get a mental vision of what is an abstract class and what is an interface. I have read through this post Real world abstract class usage simple samples, but I am still not clear about the concepts.
I am hoping someone can describe a real world in the form of an "Person" object.
So inheritance would be "Person" --> "Employee" --> "Manager"
And Overriding would be "Employee's Salary" would become "Sales employee commission"
How can I describe an abstract class and an interface within a Person object concept?
An abstract class is a good choice if we are using the inheritance concept since it provides a common base class implementation to derived classes. An abstract class is also good if we want to declare non-public members. In an interface, all methods must be public.
A concrete example of an abstract class would be a class called Animal. You see many animals in real life, but there are only kinds of animals. That is, you never look at something purple and furry and say "that is an animal and there is no more specific way of defining it".
Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes. If you are designing small, concise bits of functionality, use interfaces.
When we have the requirement of a class that contains some common properties or methods with some common properties whose implementation is different for different classes, in that situation, it's better to use Abstract Class then Interface.
There are different views on the subject of using an interface versus abstract class. An interface, though, should express the behavior of an object (what it can do) and the abstract class should define what it is. Basically "I can" versus "I am" from the object's persepective.
So, if we have a Person, that is a noun and so we'll use an abstract class to define it. Any thing that "is a" Person will inherit from that class. If we want to define some behaviour that describes some behavior that the Person is capable of that doesn't extend to all Persons, we should put that into an interface.
Using the relationship that you defined (Person -> Employee -> Manager), we can say that the Employee implements IFirable and the Manager implements IFirer. The employee can be fired, and the manager can fire an employee.
I apologize that I am not using your person example, please take this as an extended comment only.
I find it easiest to explain the concepts, to people with no programming background, using an analogy to something most people already understand: Plastic molding!
Lets say we are making plastic fruit for instance:
An interface is like the mold, it has the shape of the final product, but you can fill it with all kinds of different colors and materials. They will all have the same shape in the end, even if they are completely different in color and texture.
An abstract class would be more like something that needs an extra step, like painting. You create the basic plastic fruit, then sent it off to get some painting or fur glued on or something.
The unfinished fruit is like an abstract class in that is has more definition of the final product then the mold does, but it in itself is not complete. It needs more work to be completed, and the end products could be completely different colors, but they are still basically the same.
I hope that helps!
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