• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Polymorphism and Instance Variables?

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

Check out this code:


OK. I understand polymorphism is in effect here when Line 1 executes, resulting in the Sub... class executing and displaying the messge "in B object method" that we see on Line X. I suppose I am expecting poly to also apply to the instance variables and so I thought "6" would display via line X.

Does poly not apply to instance variables in obcets? Or is it simply that the JVM uses the reference variable type (on the left of the assignment operator (Line 2)) in deciding what I want to print?

Thanks to all for your imput
Gary
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java uses reference type to invoke the variable, and actual objects to invoke methods. UseRealWords here!
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember polymorphism doesnot apply to:
1.static methods
2.private methods.
3.variables.
4.overloaded methods.
5Generics(you will study later when you study collections)
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would polymorphism not apply to overloaded methods and generics??

Overloaded methods in combination with polymorphism:


Generics in combination with polymorphism:
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:Why would polymorphism not apply to overloaded methods and generics??

Overloaded methods in combination with polymorphism:




Just to inform you that overloading happens when we change the argument of the methods
but here we are overridding both the test methods in Bar class
public void test(Object o)
public void test(Integer o)

So that why here polymorphism applies here





 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:
Generics in combination with polymorphism:



No,it is a method overridden,because polymorphism applies only to the Base type not to a generic type.
Here you are overriding the test method.

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanky Sohar wrote:Just to inform you that overloading happens when we change the argument of the methods
but here we are overridding both the test methods in Bar class
public void test(Object o)
public void test(Integer o)

So that why here polymorphism applies here


Of course because there is no other way. If the super class only has the test method with an Object argument then the sub-class can only override that method. Then overloading is only useful if you have a sub-class reference. You just said that polymorphism doesn't apply to overloaded methods.

Shanky Sohar wrote:No,it is a method overridden,because polymorphism applies only to the Base type not to a generic type.
Here you are overriding the test method.


The base class is generic. What is your point?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
main point is that polymorphism doesnot determine which overloaded version is called..
polymorphism does come into play when the decision is about which overriddden method is called.

Lets see how the polymorphism work with overloaded method.



lets say
here as the method dostuff() is overridden in the Test class so,it will call the method of Test class at runtime but at compile time it continue to point the method of Testingoverloaded class.so here polymorphish applyies

now see
//cannot be done because subclass refernce cannot point to a superclass object.


and lastly
this will call the method of Test class..


in case of overloaded method reference type determine which method to call..
but in case of overridden method object type determine which method to call..

polymorphish occur at runtime but in case of overloaded method like in

when we do
compiler itself decided which method to call.

so i think now it is clear to you
if not please study polymorphishm
http://www.google.co.in/#hl=en&rlz=1R2ADFA_enIN394&q=polymorphism+in+java&aq=0sx&aqi=g-sx10&aql=&oq=polymorphishm+in+java&gs_rfai=&fp=bc4921db69ff5ed7

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as i said above
polymorphish happen at runtime
but all the generic type information is removed while compiling.
at runtime collections are collections like earlier days.
So you cannot say that polymorphishm apply to Generics also.


Happy Learning

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this scenerio.

l polymorphism has to be at run time. Think of this scenario, Java has exposed JDBC drivers interface and you load the driver class at runtime. Using the same code you can connect to any JDBC compliant database where the actual database to be connected may be supplied by user at runtime. How such a functionality can be supported at compile time?

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanky Sohar wrote:main point is that polymorphism doesn't determine which overloaded version is called..
polymorphism does come into play when the decision is about which overridden method is called.

I agree with that but that is not what you said. You just said that polymorphism doesn't apply to overloaded methods. But what you probably meant was that polymorphism doesn't apply to overloaded methods that don't override methods in the parent class.

And about the second part of your post. Your talking about type erasure but that has little to do with polymorphism. Polymorphism is just a feature that allows you to handle different data-types and methods through a uniform interface. The fact whether or not that interface is generic, is irrelevant. You just said that polymorphism doesn't apply to generics.

And type erasure doesn't remove all the generic information. Yes it's true that you can't know of what type the generic object is (without reflection). However the class still contains the generic restrictions. For instance:
public static <T extends Number> void test(T t) {}
will compile to:
public static void test(Number){}

And sometimes generics does come in to play when determining which method to call:


Happy Learning
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic