• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem in polymorphism  RSS feed

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have some doubts in polymorphism....need help?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sachin,

Do you have concrete example where you are confused?
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • 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
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • 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: 368
3
  • Mark post as helpful
  • send pies
  • 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: 368
3
  • Mark post as helpful
  • send pies
  • 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
C++ Java Python
  • Mark post as helpful
  • send pies
  • 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".
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!