wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Overriding of Member variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overriding of Member variables" Watch "Overriding of Member variables" New topic
Author

Overriding of Member variables

Ads Nct
Greenhorn

Joined: Apr 13, 2010
Posts: 5
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

Joined: Apr 10, 2010
Posts: 308
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


Bye,
Nicola
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

What does the following code tell you about this behavior...?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917



I modified your program a bit...
Let me know if you still have doubts.


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Ads Nct
Greenhorn

Joined: Apr 13, 2010
Posts: 5


What will be the Output of the above Program ? Please explain
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

"Ads Nct",
Did you check your private messages ?


[My Blog]
All roads lead to JavaRanch
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

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
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

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
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

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

Joined: Feb 04, 2010
Posts: 20
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

Joined: Oct 13, 2005
Posts: 37940
    
  22
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
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

Wow !
what an explanation Campbell Ritchie !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37940
    
  22
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding of Member variables
 
Similar Threads
a question about super command
explain why the following will compile
webservice
a bug with a constructor
inheritance