Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
Beginner Question about java superclasses and subclasses

I have a question about instance variables in java classes.

I have the following class Employer.java

public class Employee {
    String name = "";
    public double salary = 0;

    public double getEarnings() {
        System.out.println("Super Earnings: " + salary);
        return salary;
    }
}

Here is my Manager class as a subclass of Employer:

public class Manager extends Employee {
    public double bonus;
    public double salary = 0;

    @Override
    public double getEarnings() {
        return super.getEarnings() + bonus;
    }
}

In the main function I try to set the bonus and I have the following code:

public class App {
    public static void main(String[] args) throws Exception {
        Manager myMan01 = new Manager();
        myMan01.name = "Mike";
        myMan01.salary = 3000;
        myMan01.bonus = 2000;
        System.out.println(myMan01.getEarnings());
        System.out.println(myMan01.salary);
    }
}

I expected the output to be 5000. But it actually is 2000.

I know I have instantiated the salary variable in the superclass and the subclass, which seems to be causing the output. I just don't understand, why myMan01.salary is not called by super.getEarnings(). Can anyone please clarify?

Thanks in advance and sorry for the formatting. Lots to learn, still :)

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

In your getEarnings() method in your Manager class I think you have a typo, you return result + bonus instead of salary + bonus !

And you don't need the salary variable, it is inherited from the Employee class.

over 4 years ago · Santiago Trujillo Denunciar

0

There is no variable called result in your code. I think you made a typo there.

public class Manager extends Employee {
    public double bonus;
    public double salary = 0;

    @Override
    public double getEarnings() {
        return this.salary + this.bonus;
    }
}

It is always good to make instance variables private access modifier and use getter & setter methods for each variable to associate with them. (Encapsulation in OOP)

over 4 years ago · Santiago Trujillo Denunciar

0

To mark the question as answered, I will recite some comments.

The answer to this problem seems to be polymorphism (although as a beginner I have to wait to later chapters in my textbook to fully understand).

  • The problem you have here is called shadowing. You have two entirely separate fields both named salary. – chrylis -cautiouslyoptimistic- 1 hour ago

  • Possible duplicate of Hiding Fields in Java Inheritance

As stated by the comments of chrylis -cautiouslyoptimistic- and Joe.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda