Hey Dudes,
I have an interesting point to be discussed in Inheritance.
In Inheritance we reuse the parent's members in the child class rather than redefining the whole thing.
Now, let us say we have a parent class with three members A,B and C. And if requirement is like - I need to define 2 classes, out of which one should inherit all the three members A, B and C from the parent class and add new members D and E. And the other class should inherit only A and B but not C and add new members F and G. Ok?
_____________
Parent
_____________
a: int
b: int
c: int
_____________
_____________
Child1
_____________
a: int
b: int
c: int
d: int
e: int
_____________
_____________
Child2
_____________
a: int
b: int
f: int
g: int
_____________
Now to handle this situation currently in
java we should do in the following way (one of the ways right!):
class Parent1{
int a,b;
}
class Parent2 extends Parent1{
int c;
}
class Child1 extends Parent2{
int d,e;
}
class Child2 extends Parent1{
int f,g;
}
This solves our problem right? But notice here that we have created one more extra class "Parent2" which we don�t actually wanted to. This has come in our design as it in not possible to leave out of some properties of the parent class in the child class when it is inheriting from the parent.
Why don�t java provide a Keyword to leave out some properties while inheriting from the parent class into a child class?
Am I making sense?
I found out one more advantage by providing this facility. In java we dont have multiple inheritance. This is because as one of the buzz words of Java is "Simple, Object oriented", the developers of Java didn�t want the programmers to get confused (very funny right??). Instead they have introduced interfaces as the alternative way (which relatively are more confusing, more funny aint that??)
Ok, now if we have the keyword to cut off some of the unwanted members while inheriting into the child class, this would solve our multiple inheritance problem also. Hip hip hurray!! Is nt it?
Let us invent our own keyword "ignore" to make this scenario more understandable (give me a minute to do this..
and by the way dont ignore the use of ignore keyword ... Hmm... I know you won�t! Suggestions are accepted for keywords which are handsome like me!!
) ).
//Our guy has got many assets a,b,c and d!!!
class Parent{
int a,b,c,d;
}
//We want all the a,b,c,d and as a bonus we want e and f
class Child1 extends Parent{
int e,f;
}
//Oops this Child2 is a very pesimist he cannt live with b,c and d... He asked me to cut the crap and so i am striving hard
class Child2 extends Parent{
ignore b,c,d;
int g,h;
}
By the way dudes, same is the opinion regarding methods.... Ok?? I meant u could also specify which methods u could ignore.
Hey I also have thought of an optimization dudes. If let us say the parent class has got loads and loads of members and methods, and want only one (or might 2 or 3 which is relatively very less than the total) of those to be inherited into the child we could do that by introducing one more keyword (Hmm... Java developers might say What one more keyword? No way!!!) called "inherit".
which could be used like:
//Parent with loads of assets
class Parent{
int a,b,c,d,....z; //Well I guess u know alphabets and also I am a bit lazy
}
//This guy is a hog, he needs all that his parent has. Very Greedy!!
class Child1 extends Parent{
int a1,a2;
}
//Our guy only needs a, m and z - the Good, the Bad and the Ugly hahahaha
class Child2 extends Parent{
inherit a, m, z;
int b1,b2;
}
What do you say? Cool aint that? Atleast I thought so
Sorry I asked for only a minute and took
alot. But that was worth taking. I know u people agree!
now let us say I have a multiple inheritance scenario like:
Parent1 Parent2
| |
-----------------------------
|
Child
Here if the parents Parent1 and Parent2 have similar members. You could cleverly leave one of those using our new keyword.
Class Parent1{
int a,b,c,d;
}
Class Parent2{
int a,e,f,g;
}
Class Child extends Parent1, Parent2{
ignore Parent1:a; //or ignore Parent2:a - what ever u dislike - but one is mandatory
int p,q,r;
}
Did all this look crazy? It looked for me. Just a kinda fun right!!
-Sunil Phani Manne
Programming is fun!