• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Very interesting point in inheritance

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sunil Phani Manne
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you guys... I realized that it was a crazy thought
 
reply
    Bookmark Topic Watch Topic
  • New Topic