• 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

Query on Mock test @ Whizlabs

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

Please find the below code



My question is e.getObject() method would return object of class B . Then accessing x on object b should display 6, but why is it displaying 5. Can any one explain the fundamental rule here.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your class gave me compile error:
EPractice$1$5.java:13: getObject() in SubClass cannot override getObject() in EP
ractice$1$5; attempting to use incompatible return type
found : B
required: A
public B getObject(){return new B();}
^
1 error
......................................................
After change the return type to A, and add two override functions
class A
{
int x=5;
public void foo(){
System.out.println("A foo");
}
}

class B extends A
{
int x=6;
public void foo(){
System.out.println("B foo");
}
}

class SubClass extends EPractice$1$5
{
public A getObject(){return new B();}
}

public class EPractice$1$5
{
public A getObject(){return new A();}
public static void main(String[] args)
{
EPractice$1$5 e=new SubClass();
e.getObject().foo();
A a = new B();
System.out.println("x1="+a.x);
B b = new B();
System.out.println("x2="+b.x);
}
}

And here is the output
java EPractice$1$5
B foo
x1=5
x2=6
so I guess for method override, it will call the real object instance's method, but for instance variable, it's different story.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi El Zhang,

I'm not sure why that code didn't work for you. It worked for me absolutely with no errors and theoritically speaking the error you are mentioning can't occur as in method overriding the return type can be of IS-A type. In our case B IS-A A so, that error can never rise.

The way you have designed your code has actually deviated my question. My question was, when we invoke e.getObject().x why the x value of A is displayed when e.getObjet() is actually getting a B object.

But some where I read that, instance variables do not participate in polymorphism. They are determined at compile time rather than at run time as happening with methods. So , if that is true then since e being the reference of EPractice$1$5, compiler might be seeing e.getObject().x , would actually invoke the object A and the value is substituted at compile time.

I'm not sure, If I had really understood this concept or not, but would certainly appreciate if any one can explain how polymorphism behaves with instance variables.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the code to run and I have no freaking clue why the output is 5. You sure it's from whizlab? I have done all 5 exams with exception of diagnostic. I don't recall this problem.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think method was choose to be run at runtime is decided by real object's method(when oberride),this method will choose it's own variable if both super and base have same variable.
if you want access the variable directly,that will depends on the reference type.
because e is type of EPractice$1$5,so getObject() suppose to return a A object whatever actual object is B,it can only be assigned to a A reference,so when you access it's variable from this reference,it is 5(variable depends reference type).
look at this:
A a = new B();
System.out.println(a.x ); //will print 5,not 6

hope it make a point.
 
Tony Smith
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I was going to dissagree until I spend half more hour looking at the thing. wei is right. Polymorphism does not play any part at all when finding the value of x. There are basically two parts to it, the polymorphism part will sucessfully return the new B() when we do e.getObject().

But e.getObject() has nothing to do with e.getObject().x.
e.getObject().x gets translated to A.x even though A getObject() method is never run at all.

I am used to see just Class.x and not Class.method().x that's what confused the heck out of me.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion the logic is as follows:

The thumb rule is that only instance methods are overriden.

In the line "EPractice$1$5 e=new SubClass();" we have reference e of type EPractice$1$5 pointing to an object of SubClass.
Calling any method that has been overridden,through this reference, will call the method of class SubClass.

The method getObject() of class SubClass returns an instance of class B. Note here that the constructor of class B is called in return statement (return new B() . The implicit super() of B's constructor will be called which shall then create an object of class A.

Now as the rule says only instance methods are overridden and not instance variables, the variable x of class A shall be printed.

Hope this makes sense.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you all,

I shall agree with you all regarding this concept.

By the way,

Tony, how did you fee about whizlabs. I failed in my very first test terribly. I thought I made it until I saw the results. I'm planning to take the final test during this month ending. I read K&B very well, did couple of mock examples and cleared them. But whizlabs papers are seemed to be very tough. What was your plan when preparing using whizlabs. Please do share any Tips N Tricks
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was baffled by the code when I first saw it...Then it made perfect sense.
When U say new B() in getObject(), the super class constructor is called(obvious reasons). Since we are only getting the instance of the object but not calling it by its name the super class has the control with itself...
So the result.

For better understanding use eclipse and run it in DEBUG mode...
 
Tony Smith
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Tony, how did you fee about whizlabs. I failed in my very first test terribly." Actually I didn't pass any of the 5 whizlabs exam when I first take it. Highest I got was 57% which is the final exam. However I do feel almost ready. I am still going through other mock exams. I think I will take it this coming saturday.
 
Greenhorn
Posts: 14
  • 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 SubClass extends EPractice$1$5
{
public B getObject(){return new B();}
}

public class EPractice$1$5
{
public A getObject(){return new A();}
public static void main(String[] args)
{
EPractice$1$5 e=new SubClass();
System.out.println(e.getObject().x);
}
}


If you look carefully, you can see that this is not overriding, it is overloading. So, when you say e.getObject(), it is calling EPractice$1$5(superclass)'s method, and it returns and object A.
Then it is calling A.x, and prints 5.
I hope it would help you.
Thanks
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a kind of overriden new in Java 5.0 called Covariant return that you can override a method with a return type that is of a subtype of the overriden method.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra:

I've spent some time on this code and this is what I think is happening:
The statement EPractice$1$5 e=new SubClass(); returns an object of type SubClass but it is being assigned to a EPractice$1$5 object, so the new object is treated as EPractice$1$5 object. So when you call the getObject() method it is the one defined in the EPractice$1$5 class that you are calling, not the one in the SubClass.
At the begining I tought too that the value 5 should be printed, but getObject() in SubClass does not override the one in EPractice$1$5 because e is of type EPractice$1$5.
I did this test: I added a new method in SubClass and called it like from e, but it does not compile, which confirms that e being treated as of type EPractice$1$5.



I hope that helps.


Originally posted by Chandra Kota:
Hello All,

Please find the below code



My question is e.getObject() method would return object of class B . Then accessing x on object b should display 6, but why is it displaying 5. Can any one explain the fundamental rule here.

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

It was quite some time I came back to this thread. Thanks to you all for your responses and now I understood more on this.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic