• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding of Member variables

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?



 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the following code tell you about this behavior...?
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What will be the Output of the above Program ? Please explain
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Ads Nct",
Did you check your private messages ?
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow !
what an explanation Campbell Ritchie !
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Hang a left on main. Then read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic