• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Accessing method from the child class

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Rajesh
i realized i adderessed "wrong" person
sorry for misreading ur name in hurry...
regards
maulin
 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic