• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

instance variables - polymorphism(overriding)

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please advice why instance variables do not participate in polymorphism(overriding) but instance methods can participate..
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please advice why instance variables do not participate in polymorphism(overriding) but instance methods can participate..



Why Line 25 is printing 10 instead of 20 ?
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since instance variables are binded up with the type of reference variables, fields are not involved in Polymorphism...
 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
always remember
overriding-depends upon object type(run-time type).
overloading-depends upon reference type(compile type).
which instance variable invoked determines by the reference type(compile type).
 
Ram Narayan.M
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Polymorphism does not apply to:
1. static methods
2. private methods.
3. variables.
4. overloaded methods.
5. Generics

Above lists are done in Compile time called Compile time binding or static binding

Dynamic Binding or Runtime binding is done in Method Overriding
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arjun Srivastava wrote:always remember
overriding-depends upon object type(run-time type).
overloading-depends upon reference type(compile type).
which instance variable invoked determines by the reference type(compile type).



Hi Arjun,

I want to get insight why "instance variable invoked determines by the reference type(compile type)." ?? whereas "instance methods invoked determines by the object type(run-time type)."
 
Ram Narayan.M
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please visit this link

https://coderanch.com/t/508371/java-programmer-SCJP/certification/Why-Properties-cannot-Overridden#2297158
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Narayan.M wrote:Please visit this link

https://coderanch.com/t/508371/java-programmer-SCJP/certification/Why-Properties-cannot-Overridden#2297158



Thanks Ram..This link resolved my question. Earlier you mentioned that, Polymorphism doesn't apply to point#4 i.e., overloaded methods..The basic purpose of polymorphism is overloading and overriding know ?? That's why for overloading we are using same name different parameters.. Could you please advice on that ?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:
That's why for overloading we are using same name different parameters.. Could you please advice on that ?


when you are overloading a method (by only changing the no of arguments),you are creating a fresh new different method.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arjun Srivastava wrote:

Harikrishna Gorrepati wrote:
That's why for overloading we are using same name different parameters.. Could you please advice on that ?


when you are overloading a method (by only changing the no of arguments),you are creating a fresh new different method.



So, Overloading doesn't apply to Polymorphism. Only Overriding applies to Polymorphism.. Is that right ?? People have different opinions on this concept.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have observed some experts saying that Method Overloading does not fall under Polymorphism....

According to Polymorphism, an entity can be of any form...

E.g.,

Car carObj = new FiatCar();

carObj = new VolkswagonCar();

That is, Car can be FiatCar or VolkswagonCar(Any form)... So dynamically carObj can refer to any subclass instance... Such a flexibility given by Polymorphism...

Polymorphism leads to the building of Generic method (not the Generics) which can work for any type...


And in Method Overloading, once the method pattern is binded at compile time... thats all... cannot be changed...So its not Polymorphism...
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent Ram. I will stick to Polymorphism is only for overriding..
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ram, You mentioned Polymorphism doesn't apply to Generics. I am not clear on that. Can you please give simple code for that. Thanks
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:You mentioned Polymorphism doesn't apply to Generics. I am not clear on that. simple code for that.


polymorshism doesn't apply to generics until and unless you are using wildcards(?) within the parametrized type.

because polymorshism doesn't apply to generics

unless you are using wildcards(?) within the parametrized type.
 
Seriously? That's what you're going with? I prefer this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic