• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Confusion.

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A
{
int x = 5;
}

class B extends A
{
int x = 6;
}

public class CT
{
public A getObject()
{
return new A();
}

public static void main(String[] arg)
{
CT c = new SCT();

System.out.println(c.getObject());//1

System.out.println(c.getObject().x);//2

}
}

class SCT extends CT
{
public B getObject()
{
return new B();
}


In the above question at line //1 i am getting object B, then why at line //2 i am getting output as 5 which which is the value of Object A.

Can any body clarify this puzzle.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prasad Maddipatla:
class A
{
int x = 5;
}

class B extends A
{
int x = 6;
}

public class CT
{
public A getObject()
{
return new A();
}

public static void main(String[] arg)
{
CT c = new SCT();

System.out.println(c.getObject());//1

System.out.println(c.getObject().x);//2

}
}

class SCT extends CT
{
public B getObject()
{
return new B();
}


In the above question at line //1 i am getting object B, then why at line //2 i am getting output as 5 which which is the value of Object A.

Can any body clarify this puzzle.



Hi Prasad, your code and output of your code everything is fine.I like to add one point.
Whenever you try to call like c.getObject().Always extended class is called.And also whenever you try to access instance variables only parent class will be called.To get clear knowledge you pleas try the following code.


In the above program //1 --> class SCT is called and corresponding class B is called.
//2 --> class SCT is called and corresponding sample() in class B is called.
From this you can conclude that only instance variables can be accessable in that way.Hope this help you..
-Thanks & Regards,
Hamsa
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The actual object created is the SCT class object but we are having the reference stored in the CT variable. This is possible because CT is a super-class of SCT.

Now for

This line returns the actual object that is stored in c. That is the SCT class object.

For


The variables and method of only the reference variable can be called when is used.
Here the reference variable is of type CT.


You can verify this by changing the variable x in class B to another name and try to access in line 2 print statement. It returns a compilation error..

Thanks
 
Prasad Maddipatla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks its clear
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic