Results 1 to 17 of 17
- 05-04-2009, 01:41 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
Can anyone tell me how this output?
Hi all,
I am not sure how this output came.
I have numbered the steps through which the code will traverse according to my assumption. Can anyone tell where am i going wrong?Java Code:class Foo { public int a; public Foo() { a = 3;[B]//(2)[/B] System.out.println("foo"); } public void addFive() { a += 5; } } public class Bar extends Foo { public int a; public Bar() { a = 8; System.out.println("bar");[B]//(3)[/B] } public void addFive() { this.a += 5; [B]//(4)[/B] } public static void main(String args[]) { Foo foo = new Bar(); foo.addFive();[B]//(1)[/B] System.out.println("Value: " + foo.a); } }
The output which i am getting is :
foo
bar
Value: 3
The output which i think should be is '13'
- 05-05-2009, 12:29 AM #2
Very good question.
While methods are virtualised, fields are not. The methods you call depend on the type of the object, while fields you refer to depend on the type of the variable.
Your object foo has two fields - Foo.a and Bar.a. Calling foo.addFive() increases Bar.a to 13 because foo is a Bar. Refering to foo.a gives you Foo.a (which is still 3) because foo is a Foo variable.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-05-2009, 02:01 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
I don't think the order indicated in your code is right.
Java Code:class Foo { public int a; public Foo() { System.out.println("a = 3;//(2) starting"); a = 3;//(2) System.out.println(" a = 3;//(2) done"); System.out.println("foo"); } public void addFive() { System.out.println("a += 5; starting"); a += 5; System.out.println(" a += 5; done"); } } public class Bar extends Foo { public int a; public Bar() { a = 8; System.out.println("System.out.println(\"bar\");//(3) starting"); System.out.println("bar");//(3) System.out.println(" System.out.println(\"bar\");//(3) done"); } public void addFive() { System.out.println("this.a += 5; //(4) starting"); this.a += 5; //(4) System.out.println(" this.a += 5; //(4) done"); } public static void main(String args[]) { System.out.println("Foo foo = new Bar() starting"); Foo foo = new Bar(); System.out.println(" Foo foo = new Bar() done"); System.out.println("foo.addFive();//(1) starting"); foo.addFive();//(1) System.out.println(" foo.addFive();//(1) done"); System.out.println("Value: " + foo.a); } }
- 05-05-2009, 02:14 AM #4
Run it though a debugger and the correct annotations are
Java Code:class Foo { public int a; public Foo() { a = 3; //(2) System.out.println("foo"); } public void addFive() { a += 5; } } public class Bar extends Foo { public int a; public Bar() { a = 8; //(3) System.out.println("bar"); } public void addFive() { this.a += 5; //(5) } public static void main(String args[]) { Foo foo = new Bar(); //(1) foo.addFive(); //(4) System.out.println("Value: " + foo.a); } }Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-05-2009, 06:09 PM #5
Member
- Join Date
- Apr 2009
- Location
- China
- Posts
- 8
- Rep Power
- 0
Please remeber foo is Foo reference.
- 05-06-2009, 05:54 PM #6
Member
- Join Date
- Sep 2008
- Posts
- 4
- Rep Power
- 0
Your Bar.a is not the overridden version of Foo.a although it may seem that way. They are independent.
Try adding the method:
public int getA() {
return a;
}
to both Foo and Bar.
And in your main(),
replace "+ foo.a;" by "..+ foo.getA());."
You'll see the difference.
- 05-07-2009, 12:03 AM #7
Or you can use typecasting:
But generally, you should make field private.Java Code:System.out.println("Value: " + ((Bar)foo).a);USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 05-07-2009, 06:08 AM #8
In think the previous posts explained the problem, but I'm going to take a shot at it, too...
Subclasses can override methods, but not fields. What you did is called "hiding" the field. You have two fields with the same name, one in each class.
Since your Bar class extends Foo, it has two fields named "a". When you specified class Foo, even though you were using a subclass, that caused the Foo version of a to be retrieved. You would have to cast the foo *field* to type Bar in order to retrieve the Bar version of a.
Therefore:
Don't hide fields. You can also cause this by specifying method parameters or fields that have the same name as your instance fields. I suggest putting a prefix, such as "i" or "_", on all instance fields, so you won't accidentally hide them. Also, the compiler can generate warnings. The Eclipse IDE provides prefix support, so getter and setter names won't include the prefix. The same applies to parameters.
Declare all your instance fields private, and provide getters and setters for *any* class that accesses them. Don't give in to the temptation to use default or protected scope!
In general, *always* do things the "right" way. Once you get in the habit, it doesn't take any more time, and you will save hours of needless debugging. This is coming from someone who is writing "Habits of the Constitutionally Lazy Programmer". Well, I would be, if I got around to it...
- 05-08-2009, 12:41 PM #9
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
Thanks everyone. So i understand that fields are not overridden. foo.a and bar.a are different. But what doubt i have now is i have called foo.a only after foo.addFive() which should have changed the value of a to 8. I wonder why even this has not happened.
By the way, i had problems in logging in for the past few days. Did anyone had similar problems?
- 05-08-2009, 12:58 PM #10
Hi,
Here no pass by reference right? How can the a value will be 8?
-Regards
RamyaRamya:cool:
- 05-08-2009, 01:31 PM #11
Hi,
Steve clearly mentioned regarding this.
variable cannot be overriden.
Foo foo = new Bar();
1.For the below line if u put SOP then a value will be 13.When method is called ,Bar will be taken into consideration.
foo.addFive();//(1)
2.When the variable is called then Foo will be taken into consideration.Here we cant hide the original parent class variable with child class eventhough, you assign child class object ,so value of a is 3 for the below line.
System.out.println("Value: " + foo.a);
-Regarrds
RamyaLast edited by RamyaSivakanth; 05-08-2009 at 01:35 PM.
Ramya:cool:
- 05-08-2009, 06:21 PM #12
foo.addFive() adds 5 to Bar.a
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-08-2009, 06:29 PM #13
Actually, the method used is the Bar version, which acts on Bar.a
The result is that Foo.a is 3, unchanged, while Bar.a is 13
- 05-08-2009, 06:49 PM #14
Of course, foo.addFive() calls Bar.addFive(), not Foo.addFive().
Hurrah for naming conventions.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-09-2009, 01:11 AM #15
this should clear things up... or maybe confuse you even more...
Java Code:public class FooOrBarTest { public static void main(String[] args){ Foo foobar = new Bar(); // NAME FIELD System.out.println(foobar.NAME); // FOO System.out.println(((Foo)foobar).NAME); // FOO System.out.println(((Bar)foobar).NAME); // BAR // TOSTRING() METHOD System.out.println(foobar); // BAR System.out.println(((Foo)foobar)); // BAR System.out.println(((Bar)foobar)); // BAR } } class Foo { final String NAME = "FOO"; @Override public String toString(){ return this.NAME; } } class Bar extends Foo { final String NAME = "BAR"; @Override public String toString(){ return this.NAME; } }USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 05-09-2009, 11:23 AM #16
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
Thanks everyone. I thought that 'foo.addFive()' will call addFive() method in Foo. But i understood that it actually calls the method in Bar. So foo.a still remains 3.
- 05-10-2009, 08:32 PM #17
Similar Threads
-
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM -
what the outPut
By alksam in forum Advanced JavaReplies: 5Last Post: 12-25-2008, 01:44 PM -
What will be output and why
By huma in forum Threads and SynchronizationReplies: 4Last Post: 06-26-2008, 10:14 PM -
output
By Camden in forum New To JavaReplies: 3Last Post: 12-01-2007, 10:34 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks