• 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

method and variable accessibility

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test{
public static void main(String args[])
{
Base b=new Subclass();
System.out.println(b.x);
System.out.println(b.method());
}
}
class Base
{
int x=2;
int method(){return x;}
}

class Subclass
{
int x=3;
int method(){return x;}
}
---------------------
What will be the output.

When this is the statement.
Base b=new Subclass();

Does the rule for accessing methods and variables differ or is it same? Will evrytime the method and variable of Subclass will be accessed?

Pls explain.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if u extends the class u can acces, study inheritance concept, u are trying to create object for two class.this is obsoltely wrong.

if u extends the sub class, then u can call, sub calss mehtod and base class mehtod.
refer.:.herberd shield book-java2.
[ June 29, 2004: Message edited by: giribabu venugopal ]
 
megha kanth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry for typing mistake..

the correct program is as follows.

class Test{
public static void main(String args[])
{
Base b=new Subclass();
System.out.println(b.x);
System.out.println(b.method());
}
}
class Base
{
int x=2;
int method(){return x;}
}

class Subclass extends Base
{
int x=3;
int method(){return x;}
}
------------

pls explain this.

thx.megha
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your question will be valid if u do



At the moment they are completely unrelated classes so u can't make a object of one assign it to another.
 
K Anshul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which variable is called depends on the reference variable used to access it.
Which method is called depends on the actual object.
So when u write


Base class x is accessed and Subclass method() is called.
 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic