Inheritance and Polymorphism Flashcards
7 cards from real 1Z0-811 practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Inheritance and Polymorphism flashcards as text
Which statement is true about abstract classes in Java?
Answer: Abstract classes can have both abstract and concrete methods
Abstract classes can contain both abstract methods (with no implementation) and concrete methods (with full implementation).
What does the 'instanceof' operator return in Java?
Answer: A boolean indicating if an object is an instance of a given type
The 'instanceof' operator returns true if the object on the left is an instance of the class or interface on the right, enabling safe type checking before casting.
What exception is thrown when an invalid cast is attempted at runtime?
Answer: ClassCastException
A ClassCastException is thrown at runtime when an object cannot be cast to the specified type because it is not an instance of that type.
What is a covariant return type in method overriding?
Answer: The overriding method may return a subtype of the parent method's return type
A covariant return type allows an overriding method to return a more specific (subtype) type than the parent method's declared return type.
Which of the following is a valid polymorphic variable assignment, where Dog extends Animal?
Answer: Animal a = new Dog();
A reference variable of a parent type can hold an object of any subtype — Animal a = new Dog() is valid because Dog is-a Animal.
What happens if a subclass constructor does not explicitly call super()?
Answer: The compiler automatically inserts a call to the no-arg super() constructor as the first statement
If super() is not explicitly called, the compiler automatically inserts a call to the parent's no-arg constructor; if no such constructor exists, a compile error results.
A concrete class that extends an abstract class must:
Answer: Implement all abstract methods or itself be declared abstract
A concrete subclass of an abstract class must provide implementations for all abstract methods; otherwise, the subclass must also be declared abstract.