This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Very interesting point in inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Very interesting point in inheritance" Watch "Very interesting point in inheritance" New topic
Author

Very interesting point in inheritance

Sunil Phani Manne
Greenhorn

Joined: Jun 20, 2005
Posts: 10
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!
Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
Java doesn't use multiple inheritance mainly because of the possibility that two parent classes could have members with the same names and signatures, not for simplicity.

Regarding your ignore keyword: Couldn't you just literally ignore the members you don't need (by not accessing them)? What if you keyword-ignore a member that is crucial for the superclass? You can't completely remove it (you don't know if the superclass needs it), so there would be no performance gain. You could only hide it, gaining space for own variable definitions which would then easily be confused with the respective superclass member.
Another point is that you can use a subclass wherever a superclass is required (polymorphism). What if an ignored method is invoked?

The ignore keyword wouldn't make multiple inheritance possible, either. A variable in two parentclasses can, although it may have the same name, represent completely different things. The system is guaranteed to break if classes manipulate each others members while thinking they are their own.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Sunil Phani Manne:
we have created one more extra class "Parent2" which we don�t actually wanted to.


Well, first, why isn't "c" simply declared in Child1?

Assuming that there is a good reason, I'd say that the code actually is telling us that "Parent2" probably is an abstraction that makes sense. If we can't make sense of it, there probably is something else fishy about our design. From my experience I'd bet a lot that if we think just a little bit harder about the design, we will find a better way of structuring our classes that doesn't involve any "ignore" mechanism.

And Michael is of course right that an "ignore" keyword would open a whole can of worms - just think about the implications on Liskov's Substitution Principle...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
If you tightly encapsulate Parent, Child2 won't have visibility for c and can sort of ignore this variable by not using Parent's accessor methods. I say "sort of ignore" because some future programmer can always get access to c from Child2.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Sunil Phani Manne
Greenhorn

Joined: Jun 20, 2005
Posts: 10
I agree with you guys... I realized that it was a crazy thought
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Very interesting point in inheritance