• 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

Super class-Sub class

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

i have two classes(say 1 and 2) both have sampe parent, i have first class object with me , i need to call second class methods using this object ,is this possible can any one help me out

thanks in advance

Devi
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes and no... it depends on how the classes are defined. If the methods you want to access from ClassA are only defined in ClassB (and are not static), then no you cannot access them directly.

You could push the methods up into the common parent class (if appropriate) so that both classes could use them. Or, you could make the methods you need to access static.

That said, a lot of this depends on what you are doing and what you are doing it with... and it is hard to really help without a greater understanding of the real system.

Hope this helps.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any subclass can act as parent class. Let's say if you have parent class P and you have two classes extending parent class(C1,C2).

You can typecast back to C1 or C2 to P and call methods belong to P.

In your case, you can typecast from parent to child or child to parent.
But not from C1 to C2 or viceversa.

If change desing as C2 extending C1, C1 extending P, You can call C1 methods from C2. Or

You can use reference of C2 in C1 to call methods on C2.
 
I once met a man from Nantucket. He had a 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