• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Overiding Doubt

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

I have a doubt regarding overriding.

If i have an object B that extends A and override a function (for example equals, or toString).

In a main function of a C class, if i have a instance of B , can i call the method of parent class?

Example:




 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't afaik. Instance methods are the specific method types that you override, like you just did, and which method is actually executed is dependant on the actualy runtime object type. Hence, even if you put your Hija object in a Padre reference variable, it would still use its Hija print() method. The only way to call it would be to add a call inside Hija's print() method, wuith super.print();

// Andreas
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Supporting you answer.......its the runtime objects on which the methods to be called depends.
Check out your code rewritten.


 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

anirudh jagithyala wrote:Supporting you answer.......its the runtime objects on which the methods to be called depends.
Check out your code rewritten.


[code=java]
public class test {
public static void main(String[]args){
//Here ,How can i call print method of padre?
Padre padre ;
padre = new Hija();
padre.print(); //Override and calls the method from Hija class
padre = new Padre();
padre.print(); //here you call the method from Padre class
}
}

Hi Anirudh first of all thanks for your contribution.
The fact is Alejandro wanted to know how to invoke padre's print() method
ON HIJA'S OBJECT! according to the exact nature of his codes. AND one good way to do that is to create an instance method
in Hija's class that uses the keyword "super" to invoke padre's print method.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic