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?
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class. superclass (parent) - the class being inherited from.
With the use of the extends keyword, the subclasses will be able to inherit all the properties of the superclass except for the private properties of the superclass.
The extends keyword extends a class (indicates that a class is inherited from another class).
reference.method()
is invokedobject
type referenced by the reference
static class Lecture {
public void addAttendant(Person p) {
p.join(this);
}
}
addAttendant
, then polymorphism can control which method will be called based on the object
type when someone invokes a method on a reference type of Lecture
or one of its subclass(es)
.Lecture.addAttendant
, the method signature that matches the p.join(this)
is join(Lecture)
(even though p
could be dynamically referenced). Here there is no polymorphism even though the object referenced by this
could be a polymorphic type.addAttendant
method, this
inside p.join(this)
is hold Lecture
class, as there is no implementation of addAttendant
in child class. So, it invoke join(Lecture lec)
method. It ignores OnlineLecture
behaviour.class Lecture {
public void addAttendant(Person p) {
p.join(this);
}
}
One approach to solving this ambiguous behavior, implement @Override
the method addAttendant(Person p)
inside OnlineLecture subclass :
public class InheritanceProblem {
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 {
@Override
public void addAttendant(Person p) {
p.join(this);
}
public String toString() {
return "an online lecture";
}
}
Output:
Joining an online lecture
Teacher joining an online lecture
Joining an online lecture
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With