• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

What is polymorphic - reference variable or object?

 
Ranch Hand
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused after having read different explanations of polymorphism in Java.

1. The Java Programming Language book:

polymorphism, meaning that an object of a given class can have multiple forms, either as its own class or as any class it extends. The new class is a subclass or extended class of the class it extends; the class that is extended is its superclass.



2. Oracle university course book:

  • Polymorphism is the ability to have many different forms; for example, the Manager class has access to methods from Employee class.
  • An object has only one form.
  • A reference variable can refer to objects of different forms.



  • 3. OCP Java SE 17 Developer Study Guide book:

    The property of an object to take on many different forms. To put this more precisely, a Java object may be accessed using:

  • A reference with the same type as the object
  • A reference that is a superclass of the object
  • A reference that defines an interface the object implements or inherits



  • What is polymorphic in Java - the reference variable or the object?
     
    Saloon Keeper
    Posts: 10929
    87
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Every variable that holds an object does so by reference. Every reference can refer to an object of the declared type or another type that meets the "is-a" test. A "Cat" is-an "Animal" therefore a reference that can hold an Animal can also hold a Cat. This  assumes the code has been written such that Cat  "extends" or "implements" Animal thus implementing the "is-a" relationship.
     
    Faisal Ahmad
    Ranch Hand
    Posts: 363
    Firefox Browser Redhat Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I know how reference variable works. And the relation between an object and a reference variable. Also, I understand inheritance - super-type and sub-types.

    My question is - what should be considered as polymorphic - reference variable or object?

    A book says, an object has only one form - thus, it is not polymorphic. It is the reference variable that is polymorphic.

    Other books say that object is polymorphic, not the reference variables.

    Or, are they both polymorphic?
     
    Carey Brown
    Saloon Keeper
    Posts: 10929
    87
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
     
    Sheriff
    Posts: 28323
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Faisal Ahmad wrote:1. The Java Programming Language book:

    polymorphism, meaning that an object of a given class can have multiple forms, either as its own class or as any class it extends. The new class is a subclass or extended class of the class it extends; the class that is extended is its superclass.

    What is polymorphic in Java - the reference variable or the object?


    Well, according to this, the classes might be said to be polymorphic. But I don't understand the point of your question. Just because there is polymorphism, that's Latin (or Greek?) for "many shapes". It doesn't follow that there is a thing which is polymorphic. And I  certainly wouldn't describe either an object or a variable that way. Nor would I describe any of the classes as polymorphic.
     
    Bartender
    Posts: 303
    12
    IntelliJ IDE Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:Reference.



    This is funny, I would've said the opposite. But I can see arguments for it both ways. I don't even think of a reference as a specific "thing" (other than just a pointer to a thing), it has no functionality or form to be 'polymorphed', so that makes it seem that the object has to be what's polymorphic. But... the object isn't literally changing into a different object when you access it using a superclass reference.

    Seems like almost a philosophical question with a little bit of semantics mixed in.
     
    Enthuware Software Support
    Posts: 4884
    60
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Polymorphism is an illusion.
     
    Carey Brown
    Saloon Keeper
    Posts: 10929
    87
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Polymorphism is-an illusion
    polymorphism extends illusion
     
    Faisal Ahmad
    Ranch Hand
    Posts: 363
    Firefox Browser Redhat Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I am studying various resources to understand how Java implements polymorphism, and what exactly are polymorphic in Java. It is surprising to see that there exist quite opposite views on what are polymorphic in Java.

    In programming language theory and type theory, polymorphism is the use of a single symbol to represent multiple different types. -- Luca Cardelli et. al.

    In object-oriented programming, polymorphism is the provision of a single interface to entities of different types. -- Bjarne Stroustrup




    The reference variable Animal a is polymorphic because it can refer to a Cat instance and a Dog instance, not just an Animal object. This is one aspect of polymorphism. A reference variable can refer to an instance of its own type, and its subtypes. The ability of the reference variable Animal a to refer to an instance of its own type and also its subtypes is polymorphic.



    In the above code block, the instruction was to  a.makeNoise(), but a different form of the  makeNoise() method has been invoked at runtime. At compile-time it was a call to  Animal class's  makeNoise(). But at runtime, it was a call to  Dog class's  makeNoise(). This is another aspect of polymorphism - virtual method invocation.

    Somewhere on the web:

    So polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types).



    Thus, I have concluded that - the reference variable is polymorphic, and not the instance/object. Object/instance has only one form.  Based on all the quotes discussed - it is the super-type reference variable that is polymorphic, not the objects of sub-types. Animal is the interface for underlying forms - Dog and Cat.

    Update:
    An instance can be referred to using references of different types, including its own type:

    So, that makes an instance polymorphic?!
     
    Paul Clapham
    Sheriff
    Posts: 28323
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Faisal Ahmad wrote:I am studying various resources to understand how Java implements polymorphism, and what exactly are polymorphic in Java.


    This is not unlike being told that a system is complicated, and deciding to find which things in it are complex. A complicated system does not need to contain complex things.

    So if you want to understand how polymorphism works, trying to identify polymorphic things in Java is a waste of time in my opinion.
     
    Faisal Ahmad
    Ranch Hand
    Posts: 363
    Firefox Browser Redhat Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Paul, thank you for your reply. When I said "what exactly are polymorphic in Java", I actually meant - "what exactly are polymorphic in Java - references or instances?". I understand what benefits polymorphism brings in Java, and how it works.

    I became curious after I came across different descriptions of polymorphism - and that too from Sun/Oracle. Then I started to dig deeper to understand exactly how polymorphism works in Java.

    So far it seems - both reference variables and objects are polymorphic.
     
    Paul Clapham
    Sheriff
    Posts: 28323
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Faisal Ahmad wrote:So far it seems - both reference variables and objects are polymorphic.

    I wouldn't say either of them have "multiple forms". But like somebody said earlier, we are in the philosophy zone here so I expect "polymorphic" could be made to mean many things.
     
    Saloon Keeper
    Posts: 15727
    368
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    To me, the literal meaning of polymorphism refers to the fact that a reference of a specific type can refer to many different implementing forms of that type.

    So, it's neither the reference that is polymorphic, nor the object. It's the type that has many forms.

    While the type is polymorphic, the use of polymorphic types is implemented through references. It's this use of polymorphic types through references that we call polymorphism.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15727
    368
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    To give a concrete example:

    The type Number is polymorphic: It has different forms such as Integer and Double.

    Polymorphism allows us to refer to an instance of Integer using a reference of type Number.
     
    Author
    Posts: 310
    12
    Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I would say that the language spec is precise and complete, and authoritative on this topic.

    I say that largely because it's written by people with a very precise and thorough understanding of the academic meanings of these things across many languages and history, and was written very carefully with the express purpose of being precise and correct. (Sure, they're still human, but.)

    Oracle training material, and books, however, are written by much more mortal types of being--and I count myself in that category if I'm being kind to myself! We, in that role, sometimes write with a view to creating a pragmatic understanding, rather than abstruse academic precision (which is commonly much less useful). We're also much more likely to make mistakes.

    Finally, I would assert that the language spec explains precisely why an object does have "many forms". If I create a String, it's a String, but it's also a CharSequence, a Serializable, a Comparable (and two more interfaces) and it's also an Object. Because it's all of those things, we can use any of those reference types to refer to it correctly

    This is no different from saying that Ishan *is* a human, but is also a University professor, and a Father, and a sports fan.
     
    them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic