Abstract Class And Interface WHAT? WHY?HOW!
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
I’m sure my readers are very familiar with the term abstract class and interface. Most of us learnt it and never used it again. Why did we learn something that we probably never would use? Well, to be honest in the line of programming you can do a lot by just using some variables some if-else and a few loops. That’s probably all there is to programming. Then why did all these other features came to be? To make your code smart.
Smart?
Yes, Smart code. Anyone now days can write code and make some apps. But is your code smart? There are a lot of ways to make your code smart but I’ll be sticking to abstract class only for this article.
Lets start from Object. What’s an object? I’d say object is anything that exists. Maybe more specifically something that acquires physical space like a brick.
We can call a brick an Object. Yes, yes we would go to programming terms in a moment but lets stick with real life for sometime. So the brick here takes some space. As a result you can’t put another brick on the same place. It doesn’t have to be a brick it could be anything that acquires space .
But what if I said, A bird!
Now here’s something to be noted carefully. When I said bird, It’s very likely my readers had different types of bird in their mind. Some probably thought of a Parrot, some thought of a macaw , some thought of a pigeon . Now that you clearly think of it , A bird doesn’t exist. It’s just a generalized form of some animals that exist. Something they all have in common. Now, when I say a bird what information's do you get?
Something that has
- Feathers
- Wings
- 2 Legs
And also it can probably do a few things such as
- Fly
- Chirp
That’s probably all the information you can give about a bird. We don’t get any information about it’s feather color. Or what kind of sound it makes or if it’s a male or a female. But one thing we can all agree on that, a bird doesn’t actually exist. It doesn’t take any space. It’s just a generalized form of some living creature.
Now lets move on to some programming stuff. In programming, Objects too take space. They take memory to exist. See it does match up with real life. Two objects can’t exist on the same memory space.
Okay So lets make some birds in programming. So basically what we should do is make a bird class that has some properties like feathers, wings, legs.
And Two functions like Fly and Chirp.
And then we would probably inherit this class for some other class such as parrot and macaw or dodo . And override how Chirp and Fly functions work as they would be different for each of them.
But answer me this should we let ourselves make a object of Bird class? I mean yeah we can certainly go ahead and make one object just call the constructor and there it is. Should we? That’s the question. In real life we see bird doesn’t exist so we probably shouldn’t let ourselves or anyone else create a bird object. Because we are trying to replicate the real life after all. Now Abstract keyword comes to place. When you add abstract keyword before class it can no longer be instantiated or be made into an object. Ye! we made our code closer to real life.
I also went ahead and made those two function abstract. Now whenever I or anyone make a new class that inherits Bird must write how Fly and Chirp function work. This is how I ensure that each bird has a fly function and a chirp function. Maybe some birds don’t fly or chirp but most of them does.
So you must be thinking.. Yeah that’s all cool and dandy we can now code in a way that is more close to reality but how does that help me?
Well my friend, Here’s how you get benefit. Suppose we have 2 classes that inherits Bird class.
So now you wanna make 2 instance of these classes and wanna hear their chirp. I mean you wanna call the chirp function.
Cool. What if you wanna make 100 different birds chirp. What would you do?Manually add them and call Chirp() ?
That would be stupid wouldn’t it? also you can’t use a loop. because a loop will work on an array or a list that has same kind of objects. right? And crow and parrot are different object so you can’t possibly do a loop. OR CAN YOU?
Now the bird class comes to save ya!
You could make a list of birds and add those in that list as they all are birds!
Now you can incorporate a loop. you can add all the birds you need. You can call the chirp function from bird reference as each bird has an abstract Chirp() function.
Well it wasn’t necessary to have an abstract class for this parent-child up-casting example. But you get the point.
Before I talk about Interface lets go back to real world. So from our previous example of bird we saw it can fly. But can only birds fly? There are Flying Squirrels , Flying Fish , And Most insects can fly. So if I ask you to make a list of things that can fly you could add more than Birds that are on the list right?
Lets get back to programming. We have lists in programming to. What if we wanted to do the same? Make a list of things that can fly that doesn’t necessarily share the same parent class because Bird,Fish,Insect aren’t the same. Maybe if we go way back on something like LivingBeing then we can have a common parent but we only wanna make a list of things that has some specific feature that is it can fly. Can we really reference all objects that fly in a generalized format?
Here’s where Interface comes into play. We need something that references features. Interface gives us the ability to do exactly that.
Interface lets us create method/property signatures. Interface can’t define any method bodies as it shouldn’t and also it can’t have any variables because it shouldn’t store anything. Now lets modify our old Bird abstract class.
We only added the IFlyable interface to the class. Now if it was some normal class we must have to implement Fly() method. Since it’s an abstract class and we really don’t want to implement that method here we are going to add abstract keyword before. But here’s the thing that happens. Now we can make a list of IFlyable and get our list of things that can FLY().
Okay lets assume we have a FlyingFish Class that implements IFlyable and an abstract class Insect that does the same. And a Mosquito class that inherits Insect class.
There we go we were able to make a list of things that can fly in programming too.
There are a lot of amazing things you can do with Interface or Abstract Class which are mostly related to DESIGN PATTERNS. But I won’t go into details in this article.
Always write smart structured code that can be related with real life. Thank me later.