Java inheritance fields

13,243

Solution 1

First, see Hiding Fields (emphasis added)

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

In other words, this isn't "inheritance" since you're actually hiding A's i behind B's i, and you are using a reference object of A, so you are getting its fields. If you did B b = new B(), you would see 20, as expected.


If you expect true overrides, try using methods.

class A {
    public int get() { 
        return 10; 
    }
}

class B extends A {
    @Override 
    public int get() { 
        return 20; 
    }
}

See

A a = new B();
System.out.print(a.get()); // 20

If you really want to see both at once, see this example.

class A {
    int i = 10;
}

class B extends A {
    int i = 20;

    @Override 
    public String toString() { 
        return String.format("super: %d; this: %d", super.i, this.i);
    }
}

And

A a = new B();
System.out.print(a); // super: 10; this: 20

Solution 2

In java you cannot override an instance variable. The output you are getting is expected. In Java you can only override instance methods and not instance variables.

If you want 20 as an output you may use getter methods over those instance variables.

 class A {
    int i = 10;

    int getI() {
        return i;
    }
}

class B extends A {
    int i = 20;

    int getI() {
        return i;
    }
}

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

        System.out.println(a.getI());
    }
}

Solution 3

Polymorphism is not applicable for fields in Java.Evaluating Variables decision is taken at compile time so always base class variables are accessed.

Solution 4

Because you define 2 variables: one in the subclass B, and one with the same name in superclass A.

A a = new B();
a.i; // refers to A.i

If you cast the A to a B, it will access B.i:

System.out.println(((B)a).i);

I think you need to use 1 variable:

class A {
    int i;

    public A() {
        i = 10;
    }
}

class B extends A {
    public B() {
        i = 20;
    }
}

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

        System.out.println(a.i); // will print 20
}

Solution 5

Member variable i is already defined in class A.

In order to achieve what you are looking for, change the class B as shown below:

class B extends A {
    public B() {
      i = 20;
    }
}
Share:
13,243

Related videos on Youtube

dhS
Author by

dhS

Software Developer...work on java, Spring-boot, microservices and other related technologies. Enthusiastic to work on challenging tasks

Updated on July 30, 2022

Comments

  • dhS
    dhS almost 2 years

    I am not able to understand the following output.

    I don't know why the output is 10, I think the line A a = new B() creates a new instance of class B, I think the result should be 20

    class A {
        int i = 10;
    }
    
    class B extends A {
        int i = 20;
    }
    
    public class MainClass {
        public static void main(String[] args) {
            A a = new B();
    
            System.out.println(a.i);
        }
    }
    

    Why this works like this .. please explain.

    • OneCricketeer
      OneCricketeer about 7 years
      You aren't overriding the value of i here, you're shadowing
    • Evin1_
      Evin1_ about 7 years
    • Dawood ibn Kareem
      Dawood ibn Kareem about 7 years
      Polymorphism doesn't work for fields, the way it does for methods. You used a variable of type A to look up field i, so you get the copy of i in type A, not the copy in type B. When you look up fields, it's the type of the variable that matters, not the class of the object.