Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is reference variable of Base class in Polymorphism ?  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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 ?

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • 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: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganish Patil wrote:thank you for guidance

    You're most welcome.

    Winston
     
    sal jefferson
    Greenhorn
    Posts: 17
    1
    • Mark post as helpful
    • send pies
    • 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.
     
    Joe Bishara
    Ranch Hand
    Posts: 175
    17
    • Likes 1
    • Mark post as helpful
    • send pies
    • 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
    • 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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!