Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding of Member variables

 
Ads Nct
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class A {
public int i=10;

}


public class B extends A{
public int i=30;


public static void main(String[] args) {
A a = new B();

System.out.println("i"+a.i);

}
}

what will be the output please explain it with proper reasoning ?



 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You override methods not member variables.

After:

A a = new B();

you have an object of class A in hand (its name is a) , with overridden methods by B (none in this case)

The int value i belongs to A then and 10 is what you get on the output.

I hope it's more clear now.
Bye
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the following code tell you about this behavior...?
 
salvin francis
Bartender
Posts: 1339
11
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I modified your program a bit...
Let me know if you still have doubts.
 
Ads Nct
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What will be the Output of the above Program ? Please explain
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Ads Nct",
Did you check your private messages ?
 
salvin francis
Bartender
Posts: 1339
11
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
program output for my code:

Explanation:
In the first instance it behaves like an Object of A
In the second instance it behaves like an Object of B

 
salvin francis
Bartender
Posts: 1339
11
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
program output for the code you posted:

i10


Explanation:
There is nothing to explain here, B is inheriting 'i' from A, there is no 'i' in B.
 
salvin francis
Bartender
Posts: 1339
11
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ads Nct wrote:What will be the Output of the above Program ? Please explain

Ads Nct, did you run your code ?
What output were you expecting?
 
adam smith ii
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicola Garofalo wrote:You override methods not member variables.


I beg to differ, please correct me if I'm wrong.

which when run will yield:
objA's i as an A is: 10
objB's i is : 30
objB's i from its parent is : 10
objAc's i when cast to a B is: 30

Now the definition of variable i in Class B can be deleted. The program still works because the i in B is inherited from A. If run this way it yields:
objA's i as an A is: 10
objB's i is : 10
objB's i from its parent is : 10
objAc's i when cast to a B is: 10
 
Campbell Ritchie
Sheriff
Posts: 51439
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are mistaken. That "B" class only has its own field, and does not express the field from the superclass. Let's override your getI() method in B. You do that by putting an identical method in the A class. Now let's look at the bytecodeLook at B.init; this calls A.init (invokespecial #1) and that sets up the field i to 10 with bipush. Then later on you can see it sets the field i to 30. So you actually have two is. Look at the two getI() methods, which are identical. Now look at the getSuperI() method. You can see it calls a field of another class (A.i) (the :I bit means it's an int). You have got two separate fields in the two classes. Let's add some lines to your main methodand see what it prints out.
java B
objA's i as an A is: 10
objB's i is : 30
objB's i from its parent is : 10
objAc's i when cast to a B is: 30
objAc's i when not cast to a B is: 10
objAc's i when cast to a B is: 30
objAc's i when not cast to a B is: 30
You see you have two values for i depending on whether you cast or not. So this isn't an overridden field at all; there are two fields. Look at the overridden method. This gives 30 whether you cast or not. See the difference in behaviour. The method is overridden; the field is hidden.
 
salvin francis
Bartender
Posts: 1339
11
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow !
what an explanation Campbell Ritchie !
 
Campbell Ritchie
Sheriff
Posts: 51439
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, but it was actually easy. All you have to do is add enough lines until you get a difference, and then print the bytecode with javap!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic