aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Any one solve it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Any one solve it?" Watch "Any one solve it?" New topic
Author

Any one solve it?

kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54
class A{
int x=5;
}
class B extends A{
int x=6;
}
public class Main{
public A getObject(){
//System.out.println("Inside A");
return new A();
}
public static void main(String a[]){
Main m = new Sub();
System.out.println(m.getObject().getClass());
System.out.println(m.getObject().x);
}
}
class Sub extends Main{
public B getObject(){
//System.out.println("Inside B");
return new B();
}
}
THis code returns 5 ,class A variable value not class B variable value.why?
Ian Edwards
Ranch Hand

Joined: Aug 14, 2006
Posts: 107
With the line


the compiler looks at the type of the reference and can see that m is of type Main and the type returned by Main.getObject() is A. In class A the value of x is 5.

Its only at runtime that the correct method is invoked. If you wanted the value of x from the B class then the runtime method would have to return it for you.
Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
agreed!

With overriding

Instance variable assignments are done compile time whereas methods are done runtime!

as a little exercise why dont you write a couple of methods (this is what I did) called int getX() { return x } ;
and experiment with calling

System.out.println(m.getObject().x);
System.out.println(m.getObject().getX());

its interesting!!!
Hari Prasad Rao
Greenhorn

Joined: Apr 11, 2006
Posts: 3
When classes are extended only methods are inherited and not the varibles.

When we create a object using the statement

Main m = new Sub();

Now, runtime binding comes into picture and when we try m.getObject() then Object B is returned.

Where as when we say m.getObject().x it still prints the value present in Class A, since variable refrences are resolved during Compile time.

Hope this helps.

Thanks,
Hari.
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Hello Ranchers,

I am not bale to get.

public A getObject()
{
//System.out.println("Inside A");
return new A();
}

here in the code as per my understanding getObject()mtd is ot class type A or it is different?
If i am wrong pls correct .
One mre thing where should i include get method to call m.getbject.x().
If i am declaring in Main method its telling me to declare a local variable named as x.
Can you please shed some light on this


Thanks<br />Dinesh
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Originally posted by Hari Prasad Rao:
[QB]When classes are extended only methods are inherited and not the varibles.


Hi,

I didn't read the whole thread, I hope I'm not out of context..
Member variables that not private are inherited by the subclasses.

bye,
Alex
Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
Originally posted by Alex Belisle Turcot:


Hi,

I didn't read the whole thread, I hope I'm not out of context..
Member variables that not private are inherited by the subclasses.

bye,
Alex


Yes I think we are a bit confused here, lets not get inheritance (off topic) confused with overridden methods, which is discussed here!
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Hi Kish,

I think of it like this:




When the compiler goes through LineB, it just sees the return type of the method- Main's getObject method has the return type A.

So it sees,
System.out.println(m.getObject().x);


as

System.out.println(someReferenceOfA.x);

and finds that it is 5

Regards,
Vishwa


Blog
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
I think the example is of overloading, the name of method is same but the reference/return type is different.
Overloading methods check for reference of object.
so it should print A and 5.
Why B???

Please explain, getting really confused...
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
It can be examined very easily .

Main m = new Sub();

A a = m.getObject();
System.out.println(a.x);

Pay attention that return type is A , however actual object is of type B.\
Member variables are resolved at Compile-time , that is a.x .

If you want to get the value of B , you have to cast it like

System.out.println(((B)m.getObject()).x);

It will give you value of B.x that is 6 .


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
My question, is why its printing B, it should print A and the value of A which 5.
Which its not doing ??
First of all, is this example of overriding or overloading.
I think the example is of overloading because the argument return is different, as per my understanding of overloading, referes to reference of object at runtime which in this case is m which has method A getObject(), so it must print A and value of A which is 5 that is printed.

If we see the output is B and value of A which is 5.

Correct me if i am wrong please..
Please shed some light on this.
Swathi Kota
Ranch Hand

Joined: Jun 04, 2008
Posts: 52


Dinesh,
This is an example of Overriding. The return type is B which IS-A A( B Extends A).Co-variant returns i.e return type can be a subtype of the overriding method's return type which is a feature of Java 5.

I hope I am clear.


SCJP 6, SCWCD 5
Success is how high you bounce when you hit bottom
Rakesh Chaudhary
Ranch Hand

Joined: Feb 19, 2008
Posts: 120
Reference Type of Parent Class creating a Object of Type with a IS-A relationship of Child Class..Delegation of method's always start with the method of Child Class but...in this case the Return type of getObject in Main class is of type A...therefore due to the Magic of Polymorphism...it delegates to the parent class A...Rather than printing x in B...The compiler prints the class Name as B but the default super statement takes it to Class A and prints the value 5..and the call returns to the original call statement....

Correct me if I am Wrong...
Thanks..


Acquire the unexpected.......
Preparing for SCJP 6
Krishnamoorthy Vuyala Muralidharan
Ranch Hand

Joined: Sep 13, 2005
Posts: 52
Hi

This is an example of perfect overriding(Co-variant-return)
In Polymorphism, the actual object's method is executed at runtime.
But as far as an instance variable is concerned, the instance variable of the declared type is returned. In this case the declared type of the reference-type variable is Main and the actual type(at run-time) is Sub as per the code example:
Main m = new Sub();

If you add the getters(getX()) in the classes 'A' and 'B' to access the int variable 'x' then the result will prove that the methods are executed on the actual object at run-time(dynamic-binding) whereas the instance variables are decided at compile-time.

Run-time = Actual type
Compile-time = Declared type

The modified code:

public class Main {
public A getObject() {
System.out.println("Inside Main's getobject() returning A");
return new A();
}

public static void main(String a[]) {
Main m = new Sub();
System.out.println(m.getObject().getClass());
System.out.println("Accessing the public instance variable directly : "+m.getObject().x);
System.out.println("Accessing the instance variable through a method :"+ m.getObject().getX());
}
}

public class Sub extends Main{
public B getObject() {
System.out.println("Sub's getobject() returning B");
return new B();
}
}

class A {
int x = 5;
int getX(){
return x;
}
}

class B extends A {
int x = 6;
int getX(){
return x;
}
}

Hope this is clear.

Kind Regards
Kris
 
 
subject: Any one solve it?