Animal is a superclass of Dog and Dog has a method called bark
public void bark()
{
System.out.println("woof");
}
Consider the following:
Animal a = new Dog();
if (a instanceof Dog){
a.bark();
}
What will happen?
I said 2 as we are checking if the object is a dog; as dog is the class with the bark method in it, if it is then we call it which will print out :s
Is my understanding correct here?
This won't compile since Animal does not have a method called bark. Think of it this way, all dogs are animals, but not all animals are dogs. All dogs bark, but not all animals bark.