Free Online Flashcard Deck

Java Inheritance and Polymorphism Free Online FlashCards

Study Java Inheritance and Polymorphism with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.

12 cards
01
Front

What relationship does extends establish in Java?

Back

A superclass is a general class; a subclass is a specialized class that extends it with extends. For example, Car extends Vehicle.

02
Front

How many direct superclasses can a Java class have?

Back

Java supports single inheritance for classes: each class has exactly one direct superclass, except Object at the top of the hierarchy.

03
Front

Are constructors inherited by subclasses?

Back

Constructors are not inherited. A subclass constructor must initialize the superclass portion, usually by calling an appropriate superclass constructor with super(...).

04
Front

What does super(...) do in a constructor?

Back

super(...) calls a constructor in the direct superclass and must be the first statement in the subclass constructor. If omitted, Java attempts to insert super().

05
Front

How do this(...) and super(...) differ?

Back

this(...) calls another constructor in the same class, whereas super(...) calls a constructor in the direct superclass. Either call must be the first statement.

06
Front

How can a subclass use a private superclass field?

Back

A subclass cannot directly access a private superclass field. It must use an accessible method, such as a public or protected accessor or controlled operation.

07
Front

What makes a method an override?

Back

A subclass overrides an inherited instance method by using the same name and parameter list, with the same or a covariant return type and no weaker access.

08
Front

How do overloading and overriding differ?

Back

Overloading uses different parameter lists, usually resolved at compile time. Overriding uses the same parameter list in a subclass and enables runtime polymorphism.

09
Front

What do reference type and runtime type control?

Back

For Animal pet = new Dog(), the reference type is Animal and the runtime object type is Dog. The reference type controls accessible members; the runtime type selects overridden instance methods.

10
Front

What does Animal a = new Dog(); a.speak(); execute?

Back

Calling a method through an Animal reference that refers to a Dog runs Dog's overridden instance method. This runtime selection is dynamic dispatch, or late binding.

11
Front

Why is upcasting safe?

Back

Upcasting assigns a subclass object to a superclass reference, such as Animal animal = dog. It is safe because every subclass object is also a superclass object.

12
Front

When can a superclass reference be downcast safely?

Back

Downcasting is valid only when the referenced object is actually an instance of the target subclass. Otherwise, the cast can throw ClassCastException; use instanceof when a check is needed.