Click to See Complete Forum and Search --> : How to access parent's method


Leon945
08-10-2010, 11:46 PM
Hi there..

im wondering if its possible to access the parent's class method.

This is how i have my classes

C extends from B
B extends from A

B is abstract

method "hello" is implemented in A
method "hello" is overriden in B
method "hello" is overriden in C

method "hello" in B makes a super.hello() call.

I want to make a super.hello() call in my class C
but i want to access A's method, not B's method.

is that possible?

i hope i was clear..

thanks in advance.

sohguanh
08-11-2010, 03:55 AM
Hi there..

im wondering if its possible to access the parent's class method.

This is how i have my classes

C extends from B
B extends from A

B is abstract

method "hello" is implemented in A
method "hello" is overriden in B
method "hello" is overriden in C

method "hello" in B makes a super.hello() call.

I want to make a super.hello() call in my class C
but i want to access A's method, not B's method.

is that possible?

i hope i was clear..

thanks in advance.

Why don't you write some simple Java program to test it out ?

AFAIK, Java only support single inheritance. By right, your class C should be able to access A's method but because B also have the same method so your class C is calling B method as B has "over-ridden" A method instead.

What I can think of is below.

Define a new method say hello2 in class B. The inside implementation will call class A hello method.

Then in your class C you will call B.hello2() which in turn call A.hello() and viola you have make your class C access class A.hello() method.

criterion9
08-11-2010, 06:41 AM
I think any classes provided in ancestors are automatically available in grandchildren unless they have been overridden at some point (in which case you'll have to manage the connection similar to as suggested above). Write some simple stubs to verify.