Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having problem with declaration of an object......

 
Deepakk Verma
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Deepakk Verma
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not access any instance variable of superclass..
 
Deepakk Verma
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome deepak
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instance variables are declared with default access, so they are accessible to any Java code in the same package.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic