wood burning stoves*
The moose likes Java in General and the fly likes Accessing method from the child class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Accessing method from the child class" Watch "Accessing method from the child class" New topic
Author

Accessing method from the child class

Rajesh Navduri
Ranch Hand

Joined: Jun 28, 2001
Posts: 31
Hi,

I have a class A which has 3 methods (m1, m2 & m3) and I have extened class B which has m2 method.
In my test class I am calling the method m1 as
classB.m1 which in turn calls m2.
Now whats happening is, instead of my overloaded method (m2), my test class is calling the super class's (class A) method m2.
Because of this the changes I have made in method m2 of class B are not getting reflected.
Is there any way to access my classB's method than the ClassA's method??
regds
Rajesh
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It will help if you post some of your code. Just the classes with the function headers will probably be enough to tell what is wrong. By default, Java should call the correct function because it can detect the type of the current object at run time.
My first guess at why it isn't working correctly is that your m2 function has a different signature (especially the parameter list) in each of the two classes. I can't tell you how to fix it until I see the code, though.


Java API Documentation
The Java Tutorial
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi Ramesh,
it would certainly help us if you can post ur code.
but i suspect that following is the problem u r facing...
lets say we have the following code,

when we run temp class we get following output,
A.m1
B.m2
This is what you want, right?
now, i guess you are having m2() method in A PRIVATE and so you have the code,

in this case the output is,
A.m1
A.m2
why??? even if you have the same signature for m2() in both the cases..
because- m2() is PRIVATE in class A. it doesn't get inherited at all to class B hence m2() doesn't get overridden. so when you call m1() on class B object it goes there and calls m2() of class A instead of m2() of class B...
you see what i mean?
if you want to overcome this problem then you can override m1() in B to call m2() in B...like,

hope this was helpful..
regards
maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hey Rajesh
i realized i adderessed "wrong" person
sorry for misreading ur name in hurry...
regards
maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing method from the child class