• Post Reply Bookmark Topic Watch Topic
  • New Topic

Super class-Sub class  RSS feed

 
kumaraswamy adurthi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
 
Chris Stehno
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • 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.
 
mohana konakanchi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!