This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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?
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.
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
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???
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
posted
0
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.
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
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
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
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.