public class TestClass {
public static void main(String[] args) {
Parent parent = new Parent();
Child child = new Child();
if (child instanceof Child){
System.out.println("child instanceof Child is true!");
} else {
System.out.println("child instanceof Child is false!");
}
if (child instanceof Parent){
System.out.println("child instanceof Parent is true!");
} else {
System.out.println("child instanceof Parent is false!");
}
if (child instanceof Person){
System.out.println("child instanceof Person is true!");
} else {
System.out.println("child instanceof Person is false!");
}
if (Child.class.isInstance(child)){
System.out.println("Child.class.isInstance(child) is true!");
} else {
System.out.println("Child.class.isInstance(child) is false!");
}
if (Parent.class.isInstance(child)){
System.out.println("Parent.class.isInstance(child) is true!");
} else {
System.out.println("Parent.class.isInstance(child) is false!");
}
if (Person.class.isInstance(child)){
System.out.println("Person.class.isInstance(child) is true!");
} else {
System.out.println("Person.class.isInstance(child) is false!");
}
if (Child.class.isAssignableFrom(Child.class)){
System.out.println("Child.class.isAssignableFrom(Child.class) is true!");
} else {
System.out.println("Child.class.isAssignableFrom(Child.class) is false!");
}
if (Child.class.isAssignableFrom(Parent.class)){
System.out.println("Child.class.isAssignableFrom(Parent.class) is true!");
} else {
System.out.println("Child.class.isAssignableFrom(Parent.class) is false!");
}
if (Child.class.isAssignableFrom(Person.class)){
System.out.println("Child.class.isAssignableFrom(Person.class) is true!");
} else {
System.out.println("Child.class.isAssignableFrom(Person.class) is false!");
}
//Parent class implemented the Interface 'Person'
if (Parent.class.isAssignableFrom(Person.class)){
System.out.println("Parent.class.isAssignableFrom(Person.class) is true!");
} else {
System.out.println("Parent.class.isAssignableFrom(Person.class) is false!");
}
if (Child.class.isAssignableFrom(Child.class)){
System.out.println("Child.class.isAssignableFrom(Child.class) is true!");
} else {
System.out.println("Child.class.isAssignableFrom(Child.class) is false!");
}
if (Parent.class.isAssignableFrom(Child.class)){
System.out.println("Parent.class.isAssignableFrom(Child.class) is true!");
} else {
System.out.println("Parent.class.isAssignableFrom(Child.class) is false!");
}
if (Person.class.isAssignableFrom(Child.class)){
System.out.println("Person.class.isAssignableFrom(Child.class) is true!");
} else {
System.out.println("Person.class.isAssignableFrom(Child.class) is false!");
}
//Parent class implemented the Interface 'Person'
if (Person.class.isAssignableFrom(Parent.class)){
System.out.println("Person.class.isAssignableFrom(Parent.class) is true!");
} else {
System.out.println("Person.class.isAssignableFrom(Parent.class) is false!");
}
}
}
public class Child extends Parent {
public static void main(String[] args) {
}
}
public class Parent implements Person {
public static void main(String[] args) {
}
}
public interface Person {
}