• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding

 
vini singh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
int x=5;
}
class B extends A
{
int x=6;
}

class CT
{
public CT getObject()
{
return(new CT());
}
}
class SCT extends CT
{
public SCT getObject()
{
return(new SCT());
}
public static void main(String args[])
{
CT c=new SCT();
System.out.println(c.getObject().x);

}
}


output is :5 why is 5 an output i thought it to be 6?

plzz xplain
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a threads topic.... moving to Java in General, Beginners.

Henry
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use CODE-tags!!!









The reason why you get 5 is that you are looking at the CT class, not the SCT. The SCT-class knows about the 6, the CT does not.

You can look at the superclass A as a big box that exposes its methods and attributes on the outside of the box.

The sub class B also exposes its methods and attributes on the outside of its box but since it is "inside" the A-box, you can't see the methods and attributes of the B box.
[ December 05, 2008: Message edited by: Ove Lindstr�m ]
 
Don Solomon
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two issues. 1) the getObject() method is going to have return type issues and 2) class CT and class SCT know nothing about classes A and B and their instance variable x. That's what I can make of what you have written.
 
vini singh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey you are right,

something wrong with the question.

question is:


class A
{
int x=5;
}
class B extends A
{
int x=6;
}

class CT
{
public A getObject()
{
return(new A());
}
public static void main(String args[])
{
CT c=new SCT();
System.out.println(c.getObject().x);

}

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

output is:5 how?
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use Code tags to display your code and use Use Real Words

In this case, 5 is returned because you are creating an SC reference. The variable returned depends on the type of the reference. To get 6, use SCT as the reference type. The fact that you are creating an SCT object makes no difference when accessing variables.

Note that this behavior is unlike the one observed when accessing an overridden method, where the type of instance created will be taken into account.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hadn't noticed the wrong words; thank you, Prateek Parekh, for pointing that out. You are doing well answering all these questions

The thing about whether the subclass or superclass field is seen is similar to what happens with static method: see this FAQ.
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome, Ritchie.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic