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?
- the assignment isn't allowed
- the call to bark is allowed and "woof" is printed at run time
- the call to bark is allowed but nothing is printed
- the call to bark causes a compile time error
- the call to bark results in a run time error
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?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…