12

Why are Instance variables of a superclass not overidden in Inheritance ?

0

3 Answers 3

29

You can hide a field, but not override it.

Hiding 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.

That's an extremely bad practice to hide field, but works:

public class HideField {

    public static class A
    {   
        String name = "a";

        public void doIt1() { System.out.println( name ); };
        public void doIt2() { System.out.println( name ); };   
    }

    public static class B extends A
    {
        String name = "b";

        public void doIt2() { System.out.println( name ); };
    }

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

        a.doIt1(); // print a
        b.doIt1(); // print a
        a.doIt2(); // print a
        b.doIt2(); // print b <-- B.name hides A.name
    }
}

Depending on whether the method was overriden, the field in A or B is accessed.

Never do that! That's never the solution to your problem and creates very subtle bugs related to inheritance.

Sign up to request clarification or add additional context in comments.

5 Comments

Oh for multiple upvotes for the imperative advice to DO NOT DO THIS. It's much like an evil overlord transforming into a snake.... It never helps.
There are subtle reasons why you might like to : especially with static class fields which you access through specifically using the super or subclass name. "Never" is never a good thing to say ;)
Using static is totally another thing, Oracle has the same suggestion with @ewernli: docs.oracle.com/javase/tutorial/java/IandI/hidevariables.html
This answer is old and has upvotes but it uses terminology in an incorrect way. Fields cannot be overloaded, rather, this is called 'hiding'. Overloading refers only to methods. See for example 8.3 "If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class."
@Radiodef Fair enough. I fixed the terminology.
26

Because inheritance is intended to modify behaviour. Behaviour is exposed through methods, and that's why they can be overridden.

Fields are not behaviour but state. You don't need to modify that, nor the private methods employed by the superclass. They are intended to allow the superclass to do it's job.

Comments

1

Because:

  • It may break the parent class code. For example consider following code(What would be behavior of line obB.getInt(); in following code if, instance variables overriding is allowed):

    class A
    {
        int aInt;
    
        public int getInt()
        {
            return aInt;
        }
    }
    
    class B extends A
    {
        int aInt;
    
        public int getInt2()
        {
            return aInt;
        }
    
        public static void main(String[] args)
        {
             B obB = new B();
    
             //What would be behavior in following line if, 
             //instance variables overriding is allowed
             obB.getInt(); 
        }
    }
    
  • It is not logical because child class should have/reflect all behaviours of parent class.

So, you can only hide the inherited methods/variables in child class but can't override.

Following is an extract from Java doc from Oracle specifying what operation you can perform/expect from child class:

You can use the inherited members as is, replace them, hide them, or supplement them with new members:

  • The inherited fields can be used directly, just like any other fields.
  • You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.