Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get a real world concept of Abstract class vs Interface in asp.net

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?

like image 444
Frank Avatar asked Nov 03 '11 14:11

Frank


People also ask

Where do we use abstract class and interface in real life scenario?

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.

What is abstract class with real life example?

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".

When would you use an abstract class instead of an interface?

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.

Where exactly we should use abstract class in building a real time applications?

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.


2 Answers

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.

like image 198
Jim D'Angelo Avatar answered Sep 28 '22 09:09

Jim D'Angelo


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!

like image 39
asawyer Avatar answered Sep 28 '22 09:09

asawyer