• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

What is reference variable of Base class in Polymorphism ?

 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What is difference between these two object creation of SubClass ?

How these objects get created in memory?
I've drawn diagram, what I understand how objects get created. Below are links.
I tried to add images to this post but don't know how to add.
( Read this link but don't know how to add images from my laptop drives) if yes please tell me how ?
SubClass Object Created Diagram

Only BaseClass Object Created Diagram

BaseClass Ref variable and Object of SubClass Diagram
Is this how object gets created and stored in memory ?

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganish Patil wrote:What is difference between these two object creation of SubClass ?


Nothing. The only difference is the declared type of the variable that stores the reference.

How these objects get created in memory?


Not important. The only thing that's important is what the compiler "knows" about those two variables.
  • In the case of subObj1, it knows it's a SubClass.
  • In the case of baseRefSubObj, it knows it's a BaseClass.

  • So in the latter case, it won't let you call any method that is only defined in SubClass.

    The "polymorphic" part is that when you call baseRefSubObj.getMessage() you will get
      SubClass: Hello
    because baseRefSubObj is actually a SubClass object, so it will call SubClass's version of the method.

    HIH

    Winston
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Confess for being late to reply.

    Winston Gutkowski wrote:The only thing that's important is what the compiler "knows" about those two variables.

    thank you for guidance
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ganish Patil wrote:thank you for guidance


    You're most welcome.

    Winston
     
    Greenhorn
    Posts: 17
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There is a little difference here.

    Hypothetically speaking, say you have a base class Animal, and you have a Cat class and Dog class that expend Animal.

    If you want to later create a collection of Animal objects, Cats, Dogs, Bunnies you will want to declare the object as an Animal.

    So


    This is the essence of Polymorphism, and would not be possible to have a "mixed" array if each Object was declared as a subclass of Animal.
     
    Ranch Hand
    Posts: 175
    17
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sal jefferson wrote:...you will want to declare the object as an Animal.


    You don't have to declare a reference as Animal in order to add it into an Animal array. You can declare a reference as a Dog and add it into an Animal array. For example, you can add these references into an Animal array.

    A polymorphic reference (a reference that refers to a subtype) is useful only if you need to use it to polymorphically call a method overridden in the subtype. For example, when you do this Animal animals = new Dog(), you can use animals to call a method overridden in Dog. You can later do this animals = new Cat() and still be able to use the same reference to call a method overridden in Cat.
     
    sal jefferson
    Greenhorn
    Posts: 17
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the clarification. I have been wrong twice so far today, that I know of. This is good learning for me. I am glad to get these wrong answers out of the way before a test or interview.
     
    An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic