What does it mean to say “Instance variables are not over-rided” in java?

StackOverflow https://stackoverflow.com/questions/13008346

  •  13-07-2021
  •  | 
  •  

سؤال

I am aware of the concept called field hiding in java. But still I am having a confusion in relation to instance variable being not over-ridden.

According to my present knowledge, overriding a method of super-class means that the JVM will call the sub-class's over-ridden method though the super-class's method is available to the sub-class.

And I read the similar thing for field hiding via the link:- Hiding Fields

So, in any case we are over-ridding the instance if we change the values of the inherited instance variable in the sub-class.

I am confused please help.


I am using the following super-class:-

public class Animal{
File picture;
String food;
int hunger;
int width, height;
int xcoord, ycoord;

public void makeNoise(){
.........
}

public void eat(){
.............
}

public void sleep(){
..........
}

public void roam(){
.............
}

}

It has sub-classes like Tiger, cat, dog,hippo etc. The sub-classes over-ride the makeNoise(), eat and roam() method.

But each sub-class also uses a different set of values for instance variables.

So as per my confusion, I am kind-of overriding all the instance variables and 3 methods of the super-class Animal; and I still have the super-class instance variables available to the sub-class with the use of the super keyword.

هل كانت مفيدة؟

المحلول

It means if you call a method in a superclass it will resolve to the overriden version in a subclass (if your instance is a subclass). However a reference to a member variable will be bound to the declaration of that variable in the class from which the call is made.

نصائح أخرى

Well, overloading generally refers to function/method overloading that

allows creating several methods with the same name which differ from each other in the type of the input and the output of the function. It is simply defined as the ability of one function to perform different tasks.

You see, the term relates to functions/methods - not instance variables (fields). The latter cannot be overloaded nor overridden. See e.g. this question.

With respect to your example: actually, you are not overriding the ancestor's fields but hiding them.

An example:

class Human {
    String name;
    int age;

    public Human(String n, int a) {
        this.name = n;
        this.age = a;
    }

    public void tell_age() {
        System.out.println("I am "+this.age);
    }
}

class Female extends Human {
    // We want to HIDE our real age ^^
    String age = 'not too old';

    public Female(String n, int a) {
        this.name = n;
        super.age = a;
    }

    // We override the parent's method to cloak our real age,
    // without this, we would have to tell our true age
    public void tell_age() {
        System.out.println("I am "+this.age);
    }

    // Ok, we give in, if you really need to know
    public void tell_true_age() {
        System.out.println("I am "+super.age);
    }
}
public static void main(String[] args) {
    Female Jenna = new Female('Jenna', 39);

    Jenna.tell_age(); // => I am not too old
    Jenna.tell_true_age(); // I am 39
}

In general, we use override to describe the methods but not fields. But you would say they have similar behaviors.

  • when they have the same field name/method signature
  • the subclass one will always override the superclass one unless you use super.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top