unit 9 Flashcards
What is inheritance in Java?
A mechanism where one class acquires the properties and behaviors of another class.
What is a superclass?
The parent class whose features are inherited.
What is a subclass?
The child class that inherits from the superclass.
How do you define a subclass?
public class SubclassName extends SuperclassName
What keyword is used to create a subclass?
extends
Can a subclass have its own fields and methods?
Yes
What is method overriding?
Providing a new version of a method in the subclass with the same signature.
What keyword is used to call a superclass constructor?
super
Where must the call to super() appear in a constructor?
First line of the subclass constructor.
What happens if no constructor is written in a subclass?
The compiler automatically inserts a call to super().
Can constructors be overridden?
No
What is polymorphism?
The ability for the same method call to behave differently depending on the object’s runtime type.
What happens when you assign a subclass object to a superclass reference?
The reference can call only the methods declared in the superclass (unless overridden).
What is dynamic method dispatch?
The method that gets called is determined by the object’s actual type at runtime.
Can a superclass reference call a subclass-only method?
No
What is an abstract class?
A class that cannot be instantiated and may contain abstract methods.
What is an abstract method?
A method without a body that must be implemented by subclasses.
Does Java support multiple inheritance of classes?
No
What class does every Java class ultimately extend?
The Object class.
What are common methods from the Object class?
toString()
What is toString() used for?
Returning a string representation of an object.
What is equals() used for?
Comparing two objects for logical equality.
What is @Override annotation used for?
Indicating that a method is overriding a superclass method.
What is upcasting?
Treating a subclass object as if it were a superclass object.