• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Dynamic dispatch

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


1. Here in dynamic dispatch I am assigning Object of subclass assigned to super class reference variable. What is the advantages of doing this.?

 
Greenhorn
Posts: 12
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To see the benefit of dynamic dispatch in your example, re-name the functions in both the classes to "show()"

Then use this code in your main :

 
Prakash Rai
Ranch Hand
Posts: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That super class method will be called....

Funkaj Kohli wrote:To see the benefit of dynamic dispatch in your example, re-name the functions in both the classes to "show()"

Then use this code in your main :

 
Prakash Rai
Ranch Hand
Posts: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1. Here I am overriding Static method eating() in sub class even it is calling super classs eating().
2. Here it calling subclass Overridden sleeping(). why?
3. Will it differ from static method and instance method in case of dynamic dispatch?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prakash Rai ,
please start a new thread for a new/each question. so that you can get more response due to the clear subject line
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Coming to your question, search first here in javaranch.
Anyway, i did for you this time .
1.https://coderanch.com/t/409544/java/java/we-override-static-method

2.google link
 
Marshal
Posts: 80735
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prakash Rai wrote: . . . I am overriding Static method eating() . . .

No you aren't You can only override instance methods. Nothing else can be overridden. With static methods you get static dispatch, ie on the class the object is declared as. You might call that static dispatch, and it's why you call them static.

And never call static members on the name of the object. Call them on the name of the class.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already answered the question about static methods in your other topic.
 
reply
    Bookmark Topic Watch Topic
  • New Topic