anulando una variable en java

public class Foo {
      public int a = 3;
    public void addFive(){
        a += 5; System.out.print("f ");
    }
}

public class Bar extends Foo {
    public int a = 8;
    public void addFive(){ 
        this.a += 5; 
        System.out.print("b " ); 
    }
}

public class Test {
    public static void main(String args[]){
        Foo f = new Bar(); 
        f.addFive();
        System.out.println(f.a);
    }
 }

I am getting output b 3 .why it is not giving b13 as output.Can anyone please explain.

preguntado el 01 de febrero de 12 a las 14:02

Please use proper spacing on your code. -

Variables cannot be overridden in Java. Your code does not compile because in class Foo La variable a is not defined. Please correct your code. -

This code doesn't even compile. -

As written, your code doesn't compile. a does not exist in Foo. -

@prerna I suppose you are studying for the Java certification :) If so, there's a mistrake in the code you post. You forgot to put public int a=3; en el objeto Foo clase. -

4 Respuestas

Assuming class Foo is declared as below

class Foo 
{

    public int a = 3;

    public void addFive()
    { 
        a += 5; 
        System.out.print("f ");
    }
}
  1. Variables have no concept of overriding. They are just masked.
  2. It is printing 3 because, when you use a superclass reference to access a variable, it accesses the variable declared in superclass only. Remember that superclass doesn't know anything about subclass.

Respondido 02 Feb 12, 16:02

class Foo {
        public void addFive() { 
            a += 5; System.out.print("f "); 
        }
}

you don't have 'a' variable defined, so this example doesn't even compile.

correct code:

class Foo {

       public int a;

       public void addFive() { 
          a += 5; System.out.print("f "); 
       }
}

and see link https://stackoverflow.com/a/2464254/1025312

contestado el 23 de mayo de 17 a las 15:05

I assume that you meant to declare an integer field a en la clase Foo.

The answer to your question has to do with concepts of 'overriding' and 'hiding', as others have pointed out. Another way to explain it is that for member variables, there is no such thing as 'dynamic dispatch'. What that means is that, if you access a member of a certain object, the system checks en tiempo de ejecución which member you mean, by looking at the class hierarchy.

So, when calling the method f.addFive, at run time, the system will see that your object is actually a Bar y no un Foo, and so take the addFive function that you defined in the Bar clase.

That does not happen for member variables: you access f.a in your print statement, and at tiempo de compilación it is decided that right there you want to access the field a declarado en clase Foo there -- and so, that is what will happen at run time.

Ahora, la razón that there is no dynamic dispatch for member variable access is performance: it would be very expensive to go through the whole 'see what object this really is' logic every time you just want to add some value to a member variable.

Respondido 01 Feb 12, 18:02

Declarando public int a = 8 in Foo clase en lugar de Bar class it should work... printing B 3.

But I suppose you are talking about a question included in the Java certification exam, so you have to correct the code of the Foo class adding public int a = 3.

You cannot override a variable in Java, but declaring in as public (o protected) En la super-class you can use it also in all inherited classes.

In this case the right output is B 13 because in the test class you are using a Bar objeto como Foo objeto, por lo que el valor de a is 3 y no 8.

Respondido 01 Feb 12, 19:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.