Questions and Answers
What is the general class in an inheritance relationship called?
superclass
What is the specialized class in an inheritance relationship called?
subclass
What key word indicates that a class inherits from another class?
extends
What members of a superclass does a subclass not have access to?
Signup and view all the answers
What key word refers to an object's superclass?
Signup and view all the answers
In a subclass constructor, when must a call to the superclass constructor appear?
Signup and view all the answers
What is the following an inexplicit call to the superclass's default constructor? super();
Signup and view all the answers
What is it called when a method in a subclass has the same signature as a method in the superclass?
Signup and view all the answers
What is it called when a subclass method has the same name as a method in the superclass but a different signature?
Signup and view all the answers
What members of a superclass are accessible to subclasses and classes in the same package?
Signup and view all the answers
When a class implements an interface, what must it provide?
Signup and view all the answers
What are fields in an interface?
Signup and view all the answers
Abstract methods must be what?
Signup and view all the answers
What can abstract classes not do?
Signup and view all the answers
You can use a lambda expression to instantiate an object that does what?
Signup and view all the answers
If a subclass constructor does not explicitly call a superclass constructor, Java will not call any of the superclass's constructors.
Signup and view all the answers
The superclass constructor always executes before the subclass constructor.
Signup and view all the answers
When a class contains an abstract method, the class cannot be instantiated.
Signup and view all the answers
What is the difference between a protected class member and a private class member?
Signup and view all the answers
What is the superclass and what is the subclass in the following line? public class Pet extends Dog
?
Signup and view all the answers
Can a subclass ever directly access the private members of its superclass?
Signup and view all the answers
What is the difference between overriding a superclass method and overloading a superclass method?
Signup and view all the answers
What is an abstract method?
Signup and view all the answers
What is an abstract class?
Signup and view all the answers
What is the name of the superclass and the subclass in the following line? public class Truck extends Vehicle
?
Signup and view all the answers
Describe the order in which the class's constructors execute when a class B inherits from class A.
Signup and view all the answers
When you create a class, why does it automatically have a toString method and an equals method?
Signup and view all the answers
How is an interface different from an abstract class, or any class?
Signup and view all the answers
Write the first line of a class named Employee, which implements an interface named Payable and Listable.
Signup and view all the answers
When designing a class hierarchy, what should be true of a superclass?
Signup and view all the answers
Study Notes
Inheritance Basics
- Superclass is the general class in an inheritance relationship.
- Subclass is the specialized class that extends the functionality of the superclass.
- The keyword "extends" indicates that a class inherits from another class.
Access Modifiers in Inheritance
- Subclasses do not have access to private members of the superclass.
- Protected members of the superclass are accessible to subclasses and classes in the same package.
- Private members can only be accessed within the class where they are declared.
Class Constructors
- In a subclass constructor, a call to the superclass constructor must appear as the first statement.
- The call to the superclass's default constructor can be done using
super();
. - The superclass constructor always executes before the subclass constructor.
Method Overriding and Overloading
- A method in a subclass that has the same signature as a method in the superclass is an example of overriding.
- A method in a subclass with the same name but a different signature than a method in the superclass is an example of overloading.
- Overriding means the same signatures are present in both methods, while overloading has different parameter lists.
Abstract Classes and Methods
- Abstract methods must be overridden in the subclass implementing them.
- Abstract classes cannot be instantiated and are intentionally left incomplete for subclasses to fill in the gaps.
Interfaces
- When a class implements an interface, it must provide implementations for all non-default methods with exact signatures and return types.
- Fields in an interface are both final and static.
- An interface allows multiple interfaces to be applied across different classes, unlike abstract classes which relate to a single hierarchy.
Class Declaration and Members
- Constructors of superclass execute before those of the subclass when a subclass object is created.
- Class objects implicitly receive methods like
toString
andequals
from the Object class. - A subclass can never directly access the private members of its superclass.
Understanding Class Relationships
- In the declaration
public class Pet extends Dog
, Dog is the superclass and Pet is the subclass. - Abstract classes should encompass common functionality across subclasses, serving as a foundation for derived classes.
- An Employee class can implement multiple interfaces like Payable and Listable through the syntax
public class Employee implements Payable, Listable
.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Enhance your understanding of inheritance in object-oriented programming with these flashcards. Explore key terms such as 'superclass', 'subclass', and 'extends'. Perfect for anyone looking to solidify their knowledge in Chapter 9 of their programming course.