• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method Overriding

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

Help in understanding the below given code.

class Super{
private void display(){
System.out.println("Super display");
}
public static void main(String arg[]){
Super s = new Sub();
s.display();
}
}

class Sub extends Super{
public void display(){
System.out.println("Sub display");
}
}

I expected the output to be "Sub display", as the object type of S is Sub and hence the method belonging to Sub class should be executed. Is it that the virtual machine will look into the object type only in case if the corresponding method is overridden?

Regards
Yuvi
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add the @Override annotation to your subclass method and you will see you aren't actually overriding anything.
 
M Yuvi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that the method is not been overridden within the sub class in this example.

My doubt is how the virtual machine identifies the method to be executed in case of non-overridden method. If the method is not overridden, will the virtual machine not consider the type of the object the reference variable is referring to, in order to identify which method should be executed, rather would invoke the method based on the reference type?
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand your question correctly your question is not about overriding at all. But why would you title your question "Method overriding" if it is not about overriding methods.

In any case you can test all this by yourself by trying it out.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
funny lol.

if you need more about overriding topics, here take a look...
i think it's nice source to read upon.

http://www.java-samples.com/showtutorial.php?tutorialid=287



wish you luck!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An unoverridden method dwells in its superclass. Since the JVM cannot find an overridden method, it uses the method in the superclass or the type referred to in the reference.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ilari Moilanen wrote: . . . But why would you title your question "Method overriding" if it is not about overriding methods. . . .

He thought the method had been overridden, and that explains the thread title.
 
M Yuvi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Is it that the virtual machine will look into the object type only in case if the corresponding method is overridden?"

I believe this query can be classified under method overriding. If you still don't agree, excuse me !

I was under the impression that irrespective of the whether the method is overridden or not, the virtual machine will look into the object type (and not the reference type) to invoke the method. If it is not so, how and when the virtual machine will know whether a particular method invoked is overridden or not and decide whether to consider the reference type or the object type to invoke the method?

The question could be basics and this could be an inappropriate forum. But I would appreciate if one of you can make me understand the concept.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the question is basic. You have made a basic error which makes your superclass method impossible to override. You can only override a "visible" method. Your method is declared private, which means it is only for internal use in its own class. So it cannot be overridden. It is called as a private method from inside its own class only.

And I still think "overriding" is appropriate for this thread's title.
 
reply
    Bookmark Topic Watch Topic
  • New Topic