Can somebody please explain me this output of polymorphism?

493 views Asked by At

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.

7

There are 7 answers

0
KidTempo On

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;
  }
}
0
Luiggi Mendoza On

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.

0
user2704561 On

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.

0
Nir Alfasi On

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!

0
Vikas V On
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.

0
ajb On

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?

0
user2712154 On

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.