• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Inheretance OCA 1Z0 -808

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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);
}
}
 
Bartender
Posts: 3659
38
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 3659
38
  • Mark post as helpful
  • send pies
  • 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: 71070
292
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 3659
38
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys now i understood the logic!!
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic