class Dog { public void bark() { System.out.println("Dog bark"); } }
class Hound extends Dog {
public void bark() { System.out.println("Hound bark"); }
public void sniff() { System.out.println("Hound sniff"); }
}
public class CastingMethodCall {
public static void main(String r[]) {
((Dog)new Hound()).bark();
//((Dog)new Hound()).sniff();
}
}
On executing above, I get o/p as "Hound bark". So it means the cast of Dog has nothing to play any role that I can safely assume, since new Hound().bark() also gives same o/p of "Hound bark".
However, if I remove the comment from next line, i.e. ((Dog)new Hound()).sniff() and then execute program, it gives compiler error saying cannot find method sniff() in class Dog. So if in the prev syntax Dog did not play any role (as i am guessing), on what basis does it play role to invoke sniff() ?? Help. Thanks.