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

Inheritance + Polymorphism

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey I just joined the forum

While preparing for OCA 8 exam I bumped into some "problems" which I can't  explain

Lets say you have this piece of code:


And if you compileand run it you get following output:


I can't wrap my head around this. Why do we once have value from parents class and then execution of method in child class?
Is some kind of shadowing happening for the id instance variable?
 
Greenhorn
Posts: 12
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) When both super and sub classes have same signature methods, a subclass reference holding a subclass object invokes subclass version of the method and a super class reference holding a super class object invokes super class version of the method. This is straightforward. But when a super class reference holding a sub class object, invokes the method, the sub class version of the method is run by JVM. This is called Dynamic method dispatch or Polymorphic method invocation in simple words Polymorphism.

2) This concept is NOT applicable for variables. Meaning if both super and sub classes have variable with same name, super class reference prints super class version of the variable (irrespective of whether it holds super class object or sub class object) and sub class reference prints sub class version of the variable.

Now if you apply these two concepts, the program and output you posted will make sense.

P.S. You may have to read two or three times the two points above to avoid confusion.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chriss Pujc wrote:I can't wrap my head around this. Why do we once have value from parents class and then execution of method in child class?


Here is a very, very, very important rule: The compiler doesn't execute any code! So every compiler error you get, is because the compiler knows something is wrong without executing any line of code. So the compiler doesn't know and care) about the actual objects, the compiler only knows about the types of the reference variables.

Here are two very simple (and hopefully easy to remember) but very, very, very important rules (which you must know by heart):
  • Which instance variables you can access is determined at compile time based on the reference variable type.
  • Which instance methods you can call/invoke is determined at compile time based on the reference variable type. Which instance method is actually executed is decided at runtime based on the type of the actual object (= polymorphism).


  • Hope it helps!
    Kind regards,
    Roel
     
    reply
      Bookmark Topic Watch Topic
    • New Topic