• 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

references and objects

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line '1' is creating a Base reference named obj which is refering to Derived object. Any confusion!
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks sagar.. your hope comes true.
I have understood on your reply.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
reply
    Bookmark Topic Watch Topic
  • New Topic