• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compile-time polymorphism?  RSS feed

 
Harinath Reddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is java supports compile-time polymorphysm? how?
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Java Ranch. Use appropriate subject line, so it is easy to understand subject.

Yes Java supports compile time polymorphism. It is also called as method overloading. Method overloading means you are using same method name with different argument list.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

There is no such thing as compile-time polymorphism.

At least I don't think there is. I think polymorphism means you can call methods on objects of different types, and the methods behave as in the target. For examplebehaves according to the type of myFoo. That depends on the type of the object called. You get whatever myFoo has as its toString() method. Even if you declared myFoo to be of type Object, you get the overridden version of toString().
Overloading is different. . . all call the out object, which is actually a PrintStream. You are getting different methods called, but all on the same object (therefore all on the same type of object). So the difference does not depend on the "target" object; it depends on the calling method, passing an int, a long, a double, a BigInteger, and a BigDecimal in turn. So, if "polymorphism" means different behaviour depending on the called object, then overloading isn't polymorphism. Overloading depends on the type of argument passed by the calling method.
Click on the word polymorphism where it is underlined, and you get some more useful information
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please UseAMeaningfulSubjectLine. What does "Harinath-mca" mean?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, polymorphism generally refers to overridden methods being evoked based on runtime type of an object. This is sometimes called runtime polymorphism (or dynamic polymorphism). Note that this behavior is rooted in the object-oriented concept of subtyping.

Some people refer to method overloading as compile-time polymorphism (or static polymorphism) -- but there are plenty of arguments (no pun intended) against using this term.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!