Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

multi-level inheritance problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I have a problem here...Let me explain it as simple as possible.
class A{
public void met1(){
System.out.println("A--> met1 is called");
}
}
class B extends A {
public void met1(){
System.out.println("B--> met1 is called");
}
}
class C extends B {
public void met1(){
System.out.println("C--> met1 is called");
}
public void test(){
/* Now my question is , Here i need to call the method of class A with a reference of C. How can i do that? Is it possible?
*/

}

}

Prompt response will be appreciated. Alternatively, u can mail me at sudhakar390@indiatimes.com
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sudhakar
Welcome


/* Now my question is , Here i need to call the method of class A with a reference of C. How can i do that? Is it possible?
*/


It is impossible that the class C method overrides the class A method met1()
If you want to use the same method name, you must use the class A reference and do the type casting from class C to class A.
I hope that I can answer your question
 
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This turned out to be a longer post than I intended......
Typecasting will not solve this little problem. No matter what you do, you cannot call A's met1 from outside the class. And even inside the class this becomes a little bit interesting, since the desired method is in a grandparent class.
The only way around this would to write a method like by adding some methods to the classes:

The reason for this is simple: Your objects are overriding their super class's methods for some reason. As such code outside the class does not have the choice of what mthod to call. Think of it this way:

Now, if you have a DuckBilledPlatypus instance, there should be no way to call the generic Mammal's giveBirth() method; DuckBilledPlatypuses (DuckBilledPlatypii??) just don't do that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic