• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
welcome deepak
 
Marshal
Posts: 80282
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic