Questions and Answers
Are private fields and methods of the superclass inherited in the subclass?
False
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?
True
"has a" is a part of what concept?
Signup and view all the answers
"is a" is a part of what concept?
Signup and view all the answers
When a subclass inherits fields and methods from a superclass, are they included in the subclass?
Signup and view all the answers
What keyword indicates that a subclass inherits from a superclass?
Signup and view all the answers
In a UML diagram, how do we indicate that a subclass extends a superclass?
Signup and view all the answers
We can say that a subclass is a specialized version of the superclass.
Signup and view all the answers
Given two classes, Auto() and SportsCar() which extends Auto(), when a new SportsCar object is created, what object is created first?
Signup and view all the answers
Can private members of the superclass be accessed by the subclass?
Signup and view all the answers
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?
Signup and view all the answers
SuperClass constructors are inherited.
Signup and view all the answers
When a subclass is instantiated, the superclass _____________ constructor is executed first.
Signup and view all the answers
When a subclass is instantiated, what is instantiated first?
Signup and view all the answers
What does the super keyword refer to?
Signup and view all the answers
What happens if you have written your own constructor in the superclass and omit the default constructor?
Signup and view all the answers
Must the call to the superclass constructor be the first Java statement in the subclass constructor?
Signup and view all the answers
If the superclass does not have a default no-arg constructor, what must happen?
Signup and view all the answers
How can a subclass method invoke the overridden method found in the superclass?
Signup and view all the answers
What is a protected member?
Signup and view all the answers
In a UML diagram, how is a protected member designated?
Signup and view all the answers
How do we prevent a method from being overridden in the superclass by a subclass?
Signup and view all the answers
What is the Object class?
Signup and view all the answers
Why does every class have a toString or equals method?
Signup and view all the answers
Can static members be overridden?
Signup and view all the answers
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.