• 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

overriding

 
Greenhorn
Posts: 18
  • 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;
}

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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a threads topic.... moving to Java in General, Beginners.

Henry
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome, Ritchie.
 
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic