Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

references and objects

 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Base
{}
class Derived extends Base
{}
class prg
{
public static void main(String args[])
{
Base obj = new Derived();//1
}
}
can anyone give me whats happenig in '1' in the code above. i know the basic concepts of that. i still want some in-depth details(if any...), using which i could resolve many of my doubts instead posting all of my quesions here..
 
Shahnawaz Shakil
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line '1' is creating a Base reference named obj which is refering to Derived object. Any confusion!
 
Maurizio Nagni
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Supposing I do not know in which extension you mean "I know the basic concepts"



could be also written as



where the super() call the empty constructor of Base.
But



would require



but now if you write


you will receive an error because Base already contains a constructor so the default one is not created (by default), that is you cannot call the "super()" constructor.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing ,
When you hold the derived class object into base class reference type , like you did here,



then object 'obj' can call only those method which are overrides by that referenced object . Its called runtime polymorphism !!
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sagar,you have explained me well but the below line is still confusing me.. if you don mind can you please explain furthur..
'obj' can call only those method which are overrides by that referenced object
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


At line 1 , 2 and 3 , we are simply creating an object same as that of reference type, so there is no problem in calling a method ,

but at line 4 , it throws the error because 'aMethodOR()' is not defined in base class AA (still it is in class BB), this is because the call is happened at runtime , its a polymorphism call , so compiler cant know in advance which objects is gets bind with object 'aa', thats why such calls are resolved at runtime depending upon the object held by base class reference !

Hope I clarify some doubt !
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sagar.. your hope comes true.
I have understood on your reply.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by hari harann:
Thanks sagar.. your hope comes true.
I have understood on your reply.


And still If you didn't get the things right , you can follow this link and practice some polymorphism code !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic