• 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

problem in polymorphism

 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have some doubts in polymorphism....need help?
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sachin,

Do you have concrete example where you are confused?
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes!!
in java polymorphism is of two types compile time and run time
compile time can be achieved by two ways( function overloading and operator loading).In java as only static function shows compile time binding because jvm while invoking instance function it implicitly puts invokevirtual keyword so it gets dynamically binding.

still how are we able to overload instance function.How?

Sorry for putting my question in haphazard way
 
Ranch Hand
Posts: 89
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're confusing Java with C# or C++. Java does not offer operator overloading i.e you cannot define the way operators work on different types.
static functions exist before any objects are created. So you can call a static method before you create any objects of that class as shown below:


However you need to remember that static methods behave differently than instance methods when involved with inheritance and polymorphism:



Now inside main if you write something like this:
It would print "Inside SuperClass" instead of "Inside SubcCass". Static methods are not involved in polymorphism!
Hope this helped!!
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ashwin. I was knowing the fact that java do not support operator overloading and when static method are inherited then it get compile time binded and jvm will look only in superclass for called method
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the thing i wanted to ask
function overloading is a way to achieve compile time binding
compile time is only for static function even though how are we able to achieve function overloading for non static function?
 
Ashwin Rao
Ranch Hand
Posts: 89
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the code below:


Now in main if you write:


Even before you execute the program the compiler knows which method within the class is gonna be called. Hence the JVM does not have anything to decide at run-time. It already knows which method has to be called based on the arguments. I don't think you can call this compile time binding because the TestClass object is not created until and unless you "run" the program. Whenever you think about something related to objects think "run-time" whenever something is related to static stuff think "compile-time".
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:yes!!
in java polymorphism is of two types compile time and run time . . .

I would disagree with that, too. I would say there is only runtime polymorphism.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic