-1

Please don't be stuck in spelling mistakes. I didn't understand why Prof' saluer function is working with Prof arguments in the screen output.

Code output:
Mes hommages pour ma / mon collègue Neumann!


My Person class.

class Personne {
    String nom;


    Personne() {
        this("Anonymus");
    }

    Personne(String nom) {
        this.nom = nom;
    }

    String saluer(Personne p) {
        return this.nom + " salue " + p.nom + " !";
    }

    public String toString() {
        return "La personne " + nom + ".";
    }
}

My other class (PROF)

class Prof extends Personne {
    String nomCours = "Java";

    Prof() {
    }

    Prof(String arg) {
        this("NoName", arg);
    }

    Prof(String arg1, String arg2) {
        super(arg1);
        this.nomCours = arg2;
    }

    String saluer(Prof p) {
        return "Mes hommages pour ma/mon collègue " + p.nom + " !";
    }

    String saluer(Personne p) {
        return "La/le prof " + this.nom + " présente ses hommages à " + p.nom + " !";
    }

    String saluer(Etudiant e) {
        if (this.nomCours.equals(e.nomCours))
            return "Bonjour à mon étudiant(e) " + e.nom + " !";
        return "Bonjour de la part de " + this.nom + " !";
    }

    public String toString() {
        return "Le prof " + nom + " donne le cours " + nomCours + ".";
    }
}

My main class

public static void main(String[] args) {
    Personne mixte1 = new Prof("Poincaré", "Math");
    Personne mixte2 = new Prof("Neumann", "Info");
    Personne mixte3 = new Etudiant("Toi", "Info");

    System.out.println(mixte1.saluer(((Prof)mixte2)));  // problem here

}

2 Answers 2

1

Your saluer() method takes a Personne object, and your Prof object is extending Personne, so this means you (or, in this case, the compiler) can cast Prof to Personne without any errors, because they are based on the same class.

You can always cast upwards in the inheritance tree

Personne
  |
  +-- Prof
  |
  +-- Etudiant

It would also be possible to cast an Etudiant to a Personne. Or, you can cast an Etudiant to a Personne and then back to an Etudiant, that would work too. But you cannot cast an Etudiant to a Prof, because they are on the same level in the inheritance tree.

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

Comments

0

The problem is, that the class Personne does not have a method saluerthat takes another Personneas argument. Your class Profdoes, but at that line in the code, the compiler does not know this. You could (for example) write:

public static void main(String[] args) {
    Personne mixte1 = new Prof("Poincaré", "Math");
    Personne mixte2 = new Prof("Neumann", "Info");
    Personne mixte3 = new Etudiant("Toi", "Info");

    System.out.println(((Prof)mixte1).saluer(mixte2);  // problem here

}

To answer the general question: The Java-compiler tries to know as best as it can, which object's methods you are calling. When you try to call mixte1.saluer((Prof) mixte2), you mistakenly think, that the compiler knows that the method saluer(Prof prof) exists with mixte1, but you 'downcast' mixte1 to a Personne, and therefore the compiler does not know this.

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.