Saturday 21 February 2009

Inheritance

As long as i keep writing this blog, i will say, I'm learning something new. Wow, so now i can count the days too when i'm in hibernation mode!! :D

I have started learning Java just few days back. I had heard that Java is slow. But that doesn't mean you should also be slow in learning, Pranali. :D

So, my topic is Inheritance today.

After reading inheritance from a highly recommended book of C++ (recommended by professors, by almost everyone i had met), I read it in Head First Java. I had never heard about this book, before coming in amdocs. So fast I am!! :D

I was aware of the biological definition of Inheritance.
As many OO concepts can be given analogies of the real world non-technical concepts, I used the same attitude while reading from the highly recommended book, but in rigid manner.
My Perception was -
1)We can inherit some class as children inherit from their parents.
2)There are few types of inheritance. Single , multiple etc.
3)If i want some feature in my class which is already present somewhere in other class, I will extend that class.
4)And yes, i knew one more thing - Inheritance is called Reusabilty too.

So i was aware of things in rigid manner. I never developed an insight for this concept. I don't even remember whether i had tried to develop that too. :(

After reading that highly recommended book around 3 times, I was very confident that i know OO, i know inheritance. But how wrong i was!! (This was the reason why i couldn't think of inheritance when i had done my first assignment in amdocs. )

In the third week of amdocs training , we happened to get a training for Java for single day. That day, i had heard a sentence from him which made me think of something else other than the rigid things i had stamped on my small brain. He had given a simple usual example of inheritance - Animals. Dog inherits Animal. i.e. "Dog is an animal." :)

Any technical reader now will think Its very simple and why the heck is this girl writing a big post for it??? I'd say, yes things are simple when we know them. But they are is inspiring when we come to know about them for the first time. :)

What a simple sentence! And changed my view towards inheritance. Now inheritance is not only above four things to me. Now i can think of the superclass, design it and can inherit it by a subclass. Not only when i'm told to do so. Anytime i will feel this will help me making my code flexible to change.

Now dog is an animal. My subclass is its superclass. But what's this? A child is its mother? its father? :O:O How is it possible??

Child is neither its mother nor its father. It is the entity which comes into picture when two other entities come together. This is multiple inheritance. Different from "IS-A" concept.
But why is it so different that when Inheritance is called "IS-A" relationship so widely? (This may be the reason why Java doesn't support it.)
So is multiple inheritance not a pure inheritance? If it is so, then what about the biological term? Doesn't child INHERIT from its parents? Or we can't give this analogy for OO inheritance? What is that?

This post will be updated when i will get answers to my questions.

No comments: