The code is this:
class Main {
public static void main(String args[]) {
Person p1 = new Student();
Person p3 = new Teacher();
Student p4 = new Student();
OnlineLecture lec3 = new OnlineLecture();
lec3.addAttendant(p1);
lec3.addAttendant(p3);
lec3.addAttendant(p4);
}
}
abstract class Person {
public void join(Lecture lec) {
System.out.println("Joining "+lec);
}
public void join(OnlineLecture lec) {
System.out.println("Joining "+lec);
}
}
class Student extends Person {
public void join(Lecture lec) {
System.out.println("Student joining "+lec);
}
}
class Teacher extends Person {
public void join(OnlineLecture lec) {
System.out.println("Teacher joining "+lec);
}
}
class Lecture {
public void addAttendant(Person p) {
p.join(this);
}
public String toString() {
return "a lecture";
}
}
class OnlineLecture extends Lecture {
public String toString() {
return "an online lecture";
}
}
I don't understand why the output I get is this:
Student joining an online lecture
Joining an online lecture
Student joining an online lecture
Shouldn't 'join(this)' in the 'addAttendant' method called on lec3 result in a 'join(OnlineLecture lec3)', and therefore give this
Joining an online lecture
Teacher joining an online lecture
Joining an online lecture
as output?