A parent class should not have knowledge of child classes. You can implement a method calculate()
and override it in every subclass:
class Person {
String name;
void getName(){...}
void calculate();
}
and then
class Student extends Person{
String class;
void getClass(){...}
@Override
void calculate() {
// do something with a Student
}
}
and
class Teacher extends Person{
String experience;
void getExperience(){...}
@Override
void calculate() {
// do something with a Student
}
}
By the way. Your statement about abstract classes is confusing. You can call methods defined in an abstract class, but of course only of instances of subclasses.
In your example you can make Person
abstract and the use getName()
on instanced of Student
and Teacher
.