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
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.
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,