Home > Archive > Java Help > February 2007 > hi i have a query regarding inheritance
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
hi i have a query regarding inheritance
|
|
| vaibhav 2007-02-19, 8:06 am |
| hi all,
me a complete newbie to java
would appreciate help from you all on inheritance
my query is like this
consider a class
class abc
{
public int a,b,c;
......
}
its extension
class bcd extends abc
{
public int d;
...
}
class abc has three public integer variables a,b and c
class bcd is class abc`s extension with its own public integer d
what i want is although 'a' is public i dont want it to be inherited
in class bcd
so at the end when i create an object of class bcd i should be left
with only b,c,d variables
how can this be achieved in java ??? some masking of variable etc
help
regards
vaibhav.y.netkar
| |
| Boaz.Jan@gmail.com 2007-02-19, 8:06 am |
| On Feb 19, 1:43 pm, "vaibhav" <vaibhav.net...@gmail.com> wrote:
> hi all,
> me a complete newbie to java
> would appreciate help from you all on inheritance
> my query is like this
>
> consider a class
>
> class abc
> {
> public int a,b,c;
> .....
>
> }
>
> its extension
>
> class bcd extends abc
> {
> public int d;
> ..
>
> }
>
> class abc has three public integer variables a,b and c
> class bcd is class abc`s extension with its own public integer d
> what i want is although 'a' is public i dont want it to be inherited
> in class bcd
> so at the end when i create an object of class bcd i should be left
> with only b,c,d variables
> how can this be achieved in java ??? some masking of variable etc
> help
>
> regards
>
> vaibhav.y.netkar
cant
only masking
try to think again about your desgin...
if you have a RoundObject class which have a member
public int radius = 5;
and a Ball class that extends RoundObject
you cant not-inharite radius...
the thing you can do is encapsulate (for your concern - "Hide") the
member "a" by making it private
and by that you wont be able to access it from the child class... but
it will still be there and might be used within all the inhereted
methods from the father class
| |
| vaibhav 2007-02-19, 10:06 pm |
| hey thanks a lot for the reply
making it private would not only make it inaccsseble directly in the
said class but also in all the other classes where it might be needed
but by doing so dont you think that it might take up a lot of memory
in some bigger programs and not inheriting it would also strenghten
the security concept in java
similarly also suppose
class abc
{
int a,b,c
public void display()
{......
}
}
class abcd extends abc
{
int d
public void display()
{......
}
}
class abcde extends abcd
{
int e
public void display()
{......
}
}
(base) class abc->(child) class abcd->(grandchild) class->abcde
all of them have public void display() method with same signature
through the object of class abcde(the grandchild class) can i invoke
display() method in class abc(base class) without invoking it in class
abcd(the child class) ???
regards
vaibhav.y.netkar
| |
| a249@mailinator.com 2007-02-19, 10:06 pm |
| On 19 Feb., 12:43, "vaibhav" <vaibhav.net...@gmail.com> wrote:
> hi all,
> me a complete newbie to java
Read a book.
> class abc has three public integer variables a,b and c
> class bcd is class abc`s extension with its own public integer d
> what i want is although 'a' is public i dont want it to be inherited
> in class bcd
You can't. Make "a" private. That doesn't prevent the inheritance
(space is still reserved for it, etc.), but it can't be accessed from
code outside abc.
| |
| Oliver Wong 2007-02-20, 7:07 pm |
| "vaibhav" <vaibhav.netkar@gmail.com> wrote in message
news:1171900504.902638.85730@m58g2000cwm.googlegroups.com...
> hey thanks a lot for the reply
>
> making it private would not only make it inaccsseble directly in the
> said class but also in all the other classes where it might be needed
> but by doing so dont you think that it might take up a lot of memory
> in some bigger programs and not inheriting it would also strenghten
> the security concept in java
The inability to "un-inherit" members is pretty central to every OO
language I've ever seen. It may be an interesting idea, but I don't think
it's one that Sun wishes to explore with Java.
>
> similarly also suppose
>
> class abc
> {
> int a,b,c
> public void display()
> {......
> }
> }
>
> class abcd extends abc
> {
> int d
> public void display()
> {......
> }
> }
>
> class abcde extends abcd
> {
> int e
> public void display()
> {......
> }
> }
>
> (base) class abc->(child) class abcd->(grandchild) class->abcde
> all of them have public void display() method with same signature
> through the object of class abcde(the grandchild class) can i invoke
> display() method in class abc(base class) without invoking it in class
> abcd(the child class) ???
Not really. You could add a protected method to abcd which simply
delegates to abc, and have abcde call that one, so that the code in
abcd.display() never gets invoked. But I can't think of a way to do it
without the cooperation of abcd. Maybe there's some crazy reflection-based
hack that I'm unaware of that'll let you do this.
- Oliver
| |
| Jesu81Rebix 2007-02-21, 4:10 am |
| On Feb 19, 4:43 pm, "vaibhav" <vaibhav.net...@gmail.com> wrote:
> hi all,
> me a complete newbie to java
> would appreciate help from you all on inheritance
> my query is like this
>
> consider a class
>
> class abc
> {
> public int a,b,c;
> .....
>
> }
>
> its extension
>
> class bcd extends abc
> {
> public int d;
> ..
>
> }
>
> class abc has three public integer variables a,b and c
> class bcd is class abc`s extension with its own public integer d
> what i want is although 'a' is public i dont want it to be inherited
> in class bcd
> so at the end when i create an object of class bcd i should be left
> with only b,c,d variables
> how can this be achieved in java ??? some masking of variable etc
> help
>
> regards
>
> vaibhav.y.netkar
If we declare public variable can access for all classes. if u want to
protect that variable u declare as private. Or if u want to access the
base and the derived classes u use protected
|
|
|
|
|