aspose file tools*
The moose likes Beginning Java and the fly likes references and objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "references and objects" Watch "references and objects" New topic
Author

references and objects

hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
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

Joined: Aug 04, 2008
Posts: 57
line '1' is creating a Base reference named obj which is refering to Derived object. Any confusion!
Maurizio Nagni
Ranch Hand

Joined: May 29, 2004
Posts: 75
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 !!


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
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

Joined: Feb 19, 2008
Posts: 2902
    
    1



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

Joined: Aug 07, 2008
Posts: 54
Thanks sagar.. your hope comes true.
I have understood on your reply.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: references and objects