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

Inheretance OCA 1Z0 -808

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys i am currently preparing for OCA 1Z0 -808 exam and i came across the below code which Outputs
Output :
Base
Dervied
Dervied


But what i expected was
Base
Base
Dervied

Could someone explain the logic of this. Thanks in advance guys!!

class Base
{
public void Print()
{System.out.println("Base");
}
}
class Derived extends Base
{
public void Print()
{
System.out.println("Dervied");
}
}
class mainn
{
public static void DoPrint (Base o)
{
o.Print();
}
public static void main(String[] args)
{
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}
 
Saloon Keeper
Posts: 3953
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priyanka !

Welcome to Coderanch !

First off, let me repost your code in formatted way, so it will be more readable:



The behavior you see is called polymorphism, the method will always be called based on real instance of the object, not on the type of varible it referred by.

Some other hints: in Java by convention class names started with uppercase (mainn better name Mainn) and methods are started with lowercase (DoPrint better name doPrint, and Print better print)
 
Mikalai Zaikin
Saloon Keeper
Posts: 3953
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You studying for Java 8 exam, so this may be not in scope, but possibly it helps.

In Java 10 was introduced reserved type name var, so the main method could be rewritten as follows (again, won't work on Java 8):



which also produces output:



So, as you can see all 3 variables are declared identically, but point to different instances, and this is the instance of the class which decides what to print.

Hope it helps. 8-)
 
Marshal
Posts: 80128
417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will actually get slightly different semantics if you use var. In line 3, var will represent Derived, but in the Java8 version that was declared as Base. Of course the declared type won't affect how polymorphism works.

And, again, . . . . welcome to the Ranch
 
Mikalai Zaikin
Saloon Keeper
Posts: 3953
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You will actually get slightly different semantics if you use var. In line 3, var will represent Derived, but in the Java8 version that was declared as Base. Of course the declared type won't affect how polymorphism works.



Yes, fully agree, as the type of variable in case of var will be derived from right hand side assignment type.

Then maybe another code demonstrating polymorphism would be :



resulting the same output:

 
Priyanka Leo
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you guys now i understood the logic!!
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic