File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overriding" Watch "overriding" New topic
Author

overriding

vini singh
Greenhorn

Joined: Dec 04, 2008
Posts: 18
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
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Not a threads topic.... moving to Java in General, Beginners.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

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

Joined: Jul 20, 2008
Posts: 48
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.


Software development is an exercise in thinking not coding.
vini singh
Greenhorn

Joined: Dec 04, 2008
Posts: 18
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

Joined: Feb 14, 2006
Posts: 210
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.


SCJP 1.4 85%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Feb 14, 2006
Posts: 210
You are welcome, Ritchie.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding