Java abstract class fields override

28,224

Solution 1

It isn't overriding anything, you're just hiding the original field at the current class scope. If you use a variable with the subtype you will still be able to access the original property. Example:

abstract class GenericContainer {
    public GenericChild child;       
}

abstract class GenericChild {
    public int prop1=1 ;
}

class SpecialChild extends GenericChild {
    public int prop1=2;
}

class SpecialContainer extends GenericContainer {
    public SpecialChild child;
}

public class Main {

    public static void main( String ... args ) {

        GenericContainer container = new SpecialContainer();
        container.child = new SpecialChild();

        System.out.println( container.child.prop1 );

        SpecialChild child = (SpecialChild) container.child;        
        System.out.println( child.prop1 );
    }

}

This prints 1 and then 2.

From SpecialChild you would also be able to go up one level using super:

class SpecialChild extends GenericChild {
    public int prop1=2;

    public int getOriginalProp1() {
        return super.prop1;
    }

}

Solution 2

In Java, data members/attributes are not polymorphic. Overloading means that a field will have a different value depending from which class it's accessed. The field in the subclass will hide the field in the super-class, but both exists. The fields are invoked based on reference types, while methods are used of actual object. You can try it yourself.

It's called, variable hiding/shadowing, for more details look on here

Solution 3

Regarding

....and i IMAGINE that the field "child" in SpecialContainer is automatically overloading the field 'child' of the GenericContainer...

No. Fields don't get overridden, only methods do.

This is one reason why use of (overridable) getter and setter methods are preferred to direct access to fields. Your fields should almost all be private.

As for your design, there's no need for your SpecialContainer class to have a SpecialChild field, but instead the SpecialChild object should be placed in the GenericChild field.

Solution 4

Why nobody is observing that program will throw NullPointerException.
subclass's field with same name will hide super class's field. There is no overriding with field. Overriding is only possible with methods.

Original Code by Author:

public abstract class GenericContainer {
    public GenericChild child;
}

public abstract class GenericChild {
    public int prop1=1;
}

public abstract class SpecialChild extend GenericChild {
    public int prop1=2;
}

public abstract class SpecialContainer extends GenericContainer {
    public SpecialChild child=new SpecialChild(); //PAY ATTENTION HERE!
}

public class ExternalClass {
    public GenericContainer container=new SpecialContainer();
    public int test() {
         return container.child.prop1
    }
}

Solution 5

Java allow me to compile this, and i IMAGINE that the field "child" in SpecialContainer is automatically overloading the field 'child' of the GenericContainer...

Firstly, Inheritence doesn't apply to variables. Fields(Insatnce variables) are not overridden in your sub-class.they are only visible in your subclass if they are marked with either public, protected or default.

Share:
28,224
Alex
Author by

Alex

Updated on July 05, 2022

Comments

  • Alex
    Alex almost 2 years

    I have an abstract class that should implement a public field, this field is an interface or another abstract classe.

    something like this:

    public abstract class GenericContainer {
        public GenericChild child;
    }
    
    public abstract class GenericChild {
        public int prop1=1;
    }
    
    public abstract class SpecialChild extends GenericChild {
        public int prop1=2;
    }
    

    Now i have another specialized class Container:

    public abstract class SpecialContainer extends GenericContainer {
        public SpecialChild child=new SpecialChild(); //PAY ATTENTION HERE!
    }
    

    Java allow me to compile this, and i IMAGINE that the field child in SpecialContainer is automatically overloading the field child of the GenericContainer... The questions are: Am i right on this? The automatic 'overloading' of child will happen?

    And, more important question, if i have another class like this:

    public class ExternalClass {
        public GenericContainer container=new SpecialContainer();
        public int test() {
             return container.child.prop1
        }
    }
    

    test() will return 1 or 2? i mean the GenericContainer container field what prop1 will call, the generic or the special? And what if the special prop1 was declared as String (yes java allow me to compile also in this case)?

    Thanks!

  • Miguel Ruiz
    Miguel Ruiz almost 2 years
    Spot on! Accessing the abstract super field from the specific child is the key concept behind this approach.