Podcast
Questions and Answers
Are private fields and methods of the superclass inherited in the subclass?
Are private fields and methods of the superclass inherited in the subclass?
False (B)
If a subclass constructor does not explicitly call a superclass constructor, what will Java automatically do?
If a subclass constructor does not explicitly call a superclass constructor, what will Java automatically do?
Java will automatically call the superclass's default constructor.
Can a method be overloaded and simultaneously be overriding a method in a superclass?
Can a method be overloaded and simultaneously be overriding a method in a superclass?
True (A)
"has a" is a part of what concept?
"has a" is a part of what concept?
"is a" is a part of what concept?
"is a" is a part of what concept?
When a subclass inherits fields and methods from a superclass, are they included in the subclass?
When a subclass inherits fields and methods from a superclass, are they included in the subclass?
What keyword indicates that a subclass inherits from a superclass?
What keyword indicates that a subclass inherits from a superclass?
In a UML diagram, how do we indicate that a subclass extends a superclass?
In a UML diagram, how do we indicate that a subclass extends a superclass?
We can say that a subclass is a specialized version of the superclass.
We can say that a subclass is a specialized version of the superclass.
Given two classes, Auto() and SportsCar() which extends Auto(), when a new SportsCar object is created, what object is created first?
Given two classes, Auto() and SportsCar() which extends Auto(), when a new SportsCar object is created, what object is created first?
Can private members of the superclass be accessed by the subclass?
Can private members of the superclass be accessed by the subclass?
Given a superclass with a private field numHours, is the statement 'SubClass child = new SubClass(); println("My child studies " + child.numHours + " hours a day.")' syntactically correct?
Given a superclass with a private field numHours, is the statement 'SubClass child = new SubClass(); println("My child studies " + child.numHours + " hours a day.")' syntactically correct?
SuperClass constructors are inherited.
SuperClass constructors are inherited.
When a subclass is instantiated, the superclass _____________ constructor is executed first.
When a subclass is instantiated, the superclass _____________ constructor is executed first.
When a subclass is instantiated, what is instantiated first?
When a subclass is instantiated, what is instantiated first?
What does the super keyword refer to?
What does the super keyword refer to?
What happens if you have written your own constructor in the superclass and omit the default constructor?
What happens if you have written your own constructor in the superclass and omit the default constructor?
Must the call to the superclass constructor be the first Java statement in the subclass constructor?
Must the call to the superclass constructor be the first Java statement in the subclass constructor?
If the superclass does not have a default no-arg constructor, what must happen?
If the superclass does not have a default no-arg constructor, what must happen?
How can a subclass method invoke the overridden method found in the superclass?
How can a subclass method invoke the overridden method found in the superclass?
What is a protected member?
What is a protected member?
In a UML diagram, how is a protected member designated?
In a UML diagram, how is a protected member designated?
How do we prevent a method from being overridden in the superclass by a subclass?
How do we prevent a method from being overridden in the superclass by a subclass?
What is the Object class?
What is the Object class?
Why does every class have a toString or equals method?
Why does every class have a toString or equals method?
Can static members be overridden?
Can static members be overridden?
Study Notes
Inheritance Principles
- Private fields and methods of the superclass are not inherited by the subclass.
- If a subclass constructor does not call a superclass constructor, Java automatically invokes the superclass's default (no-arg) constructor.
- Methods can be both overloaded and overridden in subclasses—overloading occurs when methods share the same name but differ in parameters.
Concepts in Object-Oriented Design
- The term "has a" relates to the concept of aggregation, indicating a whole-part relationship.
- The term "is a" relates to inheritance, illustrating a hierarchical relationship between classes.
- When a subclass inherits fields and methods from a superclass, they are not physically included in the subclass's memory.
Relation Indicators
- The keyword
extends
signifies that a subclass inherits from a superclass. - In UML diagrams, inheritance is represented with an open arrowhead pointing from the subclass to the superclass.
Object Creation and Constructors
- A subclass can be seen as a specialized version of the superclass, inheriting its properties and behaviors.
- When a new object of a subclass is created, the superclass's constructor is executed before the subclass's constructor.
- If a superclass has a private field (e.g.,
numHours
), it cannot be accessed directly by the subclass. - Superclass constructors are not inherited by subclasses; each subclass must invoke the superclass constructor if needed.
Default Constructor and Overriding
- The superclass's default constructor is executed first when a subclass is instantiated.
- If a superclass lacks a default no-arg constructor, all subclass constructors must explicitly call a constructor from the superclass.
- The
super
keyword is essential for invoking the superclass's constructor and accessing its members.
Access Modifiers and Method Overriding
- Protected members are accessible by methods in the subclass and methods within the same package, ensuring controlled access.
- In UML, a protected member is denoted with a
#
. - To prevent a method from being overridden in a subclass, use the
final
modifier in the superclass method definition.
Object Class and Static Members
- All classes in Java inherit from the Object class, which provides fundamental methods like
toString
andequals
. - Every class has a
toString
andequals
method because they inherit from the Object class. - Static members belong to a class and cannot be overridden by subclasses, as they are not tied to instances.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge of inheritance in Java with these flashcards. Covering key concepts such as visibility of fields and automatic constructor calls, these flashcards are perfect for anyone needing a refresher on Java inheritance principles.