I have 3 class and, first is Person :
public class Person {
Person() {
}
}
Second is Engineer that extends of Person
public class Engineer extends Person {
Engineer() {
}
}
and another extends of Person
public class Doctor extends Person {
Doctor() {
}
}
Last one is Work that take in constructor an object Person
public class Work {
Work(Person p) {
//how to insure that p is Engineer ?
}
}
How to detect that an object p is Engeneer and not from another class ?
You can use the
instanceofkeyword for checking the type of an object. It works like this