| Author |
Having problem with declaration of an object......
|
Deepakk Verma
Ranch Hand
Joined: Sep 09, 2009
Posts: 31
|
|
i have a shadow of doubt about the concept of object declaration
i would illustrate my doubt with an example
The statement
superclass RefVariable = new subclass();
it will create memory space for an object of "subclass" class and would appoint a variable of type superclass named as RefVariable with the reference of the object created .....
Now my questions rare :
what this RefVariable actually contains?
what are its capability?
Is it carrying a pointer to the superclass object?
Or is it carrying some sort of address to a memory location?
I am really confused with this....please help
|
 |
pankaj vijay
Ranch Hand
Joined: Apr 01, 2008
Posts: 75
|
|
Deepak the first thing is that java doesnt have pointer... Now lets see your line
description of this line is :
superclass --> Name of class
RefVariable --> reference variable of class superclass
new--> this is keyword that will create a memory space for object.
subclass()--> calling subclass constructor.
Here RefVariable is denoting object of subclass. But its type is superclass. so its capability will be like this:
1. Using RefVariable you can call a method of subclass but same method must be defined in superclass also.
2. Using RefVariable you can access any instance variable of subclass.
3. Using RefVariable you can access static method of superclass. you cant use this to access static method of subclass.
4. Using RefVariable you can access static variable of superclass.
Now the funda is static method & variable are accessed based on reference While instance method & variable are access based on the object.
hope this will help
|
Pankaj Vijay (SCJP, SCBCD)
Learn Core Java,Learn Servlet Jsp, SCJP Questions,Struts Tutorial
|
 |
Deepakk Verma
Ranch Hand
Joined: Sep 09, 2009
Posts: 31
|
|
4. Using RefVariable you can access static variable of superclass.
Can we also access the private instance variables of the superclass by RefVariable?
|
 |
pankaj vijay
Ranch Hand
Joined: Apr 01, 2008
Posts: 75
|
|
|
You can not access any instance variable of superclass..
|
 |
Deepakk Verma
Ranch Hand
Joined: Sep 09, 2009
Posts: 31
|
|
It took me a little while to grasp the idea but i thing i am able to digest the concept now
Thank you very much Mr Pankaj for your valucble efforts to explain this all....
Thank you again
Deepak Verma
|
 |
pankaj vijay
Ranch Hand
Joined: Apr 01, 2008
Posts: 75
|
|
welcome deepak
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
The instance variables are declared with default access, so they are accessible to any Java code in the same package.
|
 |
 |
|
|
subject: Having problem with declaration of an object......
|
|
|