0

Heres the code :

class A{
    int data=10;
    void show1(){
        System.out.println("In A="+data);
    }
}

class B extends A{
    int data=20;
    void show2(){
        System.out.println("In B="+data);
    }



}


public class Overriding4 {
    public static void main(String args[]){
        B b=new B();
        System.out.println("Data1="+b.data);
        System.out.println("Data2="+b.data);
        b.show1();
        b.show2();
    }
}

And heres the output :

Data1=20
Data2=20
In A=10
In B=20

The output at Data1=20 should be 10 , not 20...but I think I'm missing something here. Please help me with this

Okay , thanks for the help, but one new doubt : What would happen if I changed the main method to :

 public static void main(String args[]){
            A a=new B();
            System.out.println("Data1="+a.data);
            System.out.println("Data2="+a.data);
            a.show1();
            a.show2();
        }

There you go.

Ajinkya
  • 147
  • 1
  • 2
  • 11
  • Why do you think it should be `10`? – Hunter McMillen Aug 23 '13 at 17:20
  • Why would `Data1` have to be 10? – Jeroen Vannevel Aug 23 '13 at 17:20
  • It is 20 because you are redeclaring the field so it is using the declared field's value as opposed to the value of the field in the superclass. You are actually supposed to try and avoid duplicate field names (field names that exist within the superclass as well as subclass) because it might avoid a lot of confusion. – Josh M Aug 23 '13 at 17:21
  • You overloaded the data variable. Rename it if you want something different like you did with the method names. If you have the same method/variable name in an extended class they become overloaded. – progrenhard Aug 23 '13 at 17:21
  • Why would you think that `b.data` would be one value for the first `println` and a different value for the next one? – Ted Hopp Aug 23 '13 at 17:24
  • I think you have a typo. Did you mean `System.out.println("Data1="+a.data);` – Jim Garrison Aug 23 '13 at 17:38
  • @JimGarrison I hope not since there's no `a` declared anywhere... – ajb Aug 23 '13 at 17:41
  • Good point. Expecting the exact same code invoked twice (with no method calls) to do different things is weird. – Jim Garrison Aug 23 '13 at 17:44
  • Thanks everybody for your help. Thank you very much – Ajinkya Aug 23 '13 at 17:47
  • what would happen if I changed the main method to following code : – Ajinkya Aug 23 '13 at 17:53

7 Answers7

1

Class fields don't get inherited. Note that you have the same int data field in both classes A and B. This is called Hiding.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

In order to "feel" the power of polymorphism we need to change your code a bit:

class A {

    int data=10;
    void show(){
        System.out.println("In A="+data);
    }
}

class B extends A {

    int data=20;

    @Override
    void show(){
        System.out.println("In B="+data);
    }

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

Now, you can see that both a and b are declared to be of type A - but the assignment is what makes the difference. Further, we can create an array of type A and accommodate it with objects of both types (A as well as B):

A[] arr = new A[3];

// let's add some stuff;

arr[0] = new A(); 
arr[1] = new B();
arr[2] = new A();

And now it's easy to see how beautiful polimorphism worksL

for (int i=0; i<arr.length; i++){
   arr[i].show();
}

The last loop will print:

In A 10
In B 20
In A 10

All we know is that the array holds objects which are sub-types of A - and we can count on each object to call the "correct" show() method. That's polymorphism!

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
1
Within a class, a field that has the same name as a field in the superclass hides
the superclass's field, even if their types are different. 

This can be found in Hiding fields

Hence, it prints 20.

Vikas V
  • 3,176
  • 2
  • 37
  • 60
0

Since you have overridden the data variable in class B, it will print the latest value. Since you revalued "data" to be 20 in the B class, that is what it will show.

user2704561
  • 23
  • 1
  • 5
0

An object of class B has two fields, both named data, but as Luiggi implied, one of those is hidden. If you want to get it back, use a cast:

public class Overriding4 {
    public static void main(String args[]){
        B b=new B();
        System.out.println("Data1="+((A)b).data);  // like this  
        System.out.println("Data2="+b.data);
        b.show1();
        b.show2();
    }
}

produces:

Data1=10
Data2=20
In A=10
In B=20

P.S. you can use the cast on either side of an assignment:

public class Overriding4 {
    public static void main(String args[]){
        B b=new B();
        ((A)b).data *= 10;
        System.out.println("Data1="+((A)b).data);  
        System.out.println("Data2="+b.data);
        b.show1();
        b.show2();
    }
}

Data1=100
Data2=20
In A=100
In B=20

PPS. I posted this to show you how the language works, but in general it is not a good idea to have public fields in classes that other classes can read and write directly. See Why use getters and setters?

Community
  • 1
  • 1
ajb
  • 31,309
  • 3
  • 58
  • 84
0

Unless you made a typo, the two line below are basically the same:

System.out.println("Data1="+b.data);
System.out.println("Data2="+b.data);

It may be helpful to visualize the object like this:

A {
  [A.data = 10];  <-- this is overridden and cannot be accessed directly
  show1() => 10;
  B {
    B.data => 20;
    show2() => 20;
  }
}
KidTempo
  • 910
  • 1
  • 6
  • 22
0

You actually did not override the methods....

`System.out.println("Data1="+b.data);

System.out.println("Data2="+b.data);`

for these two lines the "data" value will be fetched by from the data variable of class B. Now, class B has extends class A that means all the members of class A will get inherited in class B.so variable i(of class A) will get inherited and inside class B we have its own local variable i......show1() method of class A will also get inherited in class B. So by using the reference variable(b) of class B we can access the show1() method as well as show2() method.

user2712154
  • 3
  • 1
  • 5