[7]Lecture 10: Inheritance

25 July 2022
4.7 (114 reviews)
28 test answers

Unlock all answers in this set

Unlock answers (24)
question
Are private fields and methods of the superClass inherited in the subClass.
answer
NO. Private members of the superclass are not inherited.
question
If a subclass constructor does not explicitly call a superclass constructor, what will Java automatically do?
answer
If a subclass constructor does not explicitly call a superclass constructor, Java will automatically call the superclass's default constructor, OR NO-ARG constructor, JUST BEFORE the code in the subclass's constructor executes.
question
Can a method be oeverloaded and simultaneously be overriding a method in a superclass?
answer
Yes. For example, if the superclass has the method int getApples(), then the subclass can have the methods
question
"has a" is a part of what concept?
answer
aggregation
question
"is a" is a part of what concept?
answer
inheritance
question
When a sub class inherits fields and methods from a super class, they are not included in the sub class.
answer
true
question
What keyword indicates that a subclass inherits from a super class?
answer
extends
question
In a UML diagram, how do we indicate that a subclass extends a super class?
answer
We use an open arrow head pointing from the subclass to the super class.
question
We can say that a subclass is a specialized version of the SuperClass.
answer
True
question
Given two classes, Auto() and SportsCar() which extends Auto(), when a new SportsCar object is created, java will first create what object?
answer
First an object of the superClass Auto() is created, then the subclass SportsCar() object.
question
Can private members of the superclass be accessed by the subclass?
answer
NO. Private members of the superclass are not inherited.
question
Given a superClass with a private field numHours, and a subclass called Student, Is the following statement syntactically correct? SubClass child = new SubClass(); println("My child studies " + child.numHours + " hours a day.")
answer
NO. The sub class child cannot inherit private fields or methods from the superclass.
question
SuperClass constructors ARE or ARE NOT inherited.
answer
NOT. Constructors are NOT inherited.
question
When a subclass is instantiated, the superclass _____________ constructor is executed first.
answer
default
question
When a subclass is instantiated, what is instantiated first?
answer
The superclass default constructor. Think of this as "The parent has to be created before the child is created."
question
What does the super keyword refer to?
answer
The super() keyword refers to an objects superclass, and it can be used to explicitly call the constructor of the superclass.
question
What happens if you have written your own constructor in the superclass, and you omit the default constructor...
answer
You can add a no-arg constructor in the superclass, or when writing the subclass constructor, issue a call to the superclass constructor.
question
Must the call to the superclass constructor be the first java statement in the subclass constructor?
answer
Absolutely.
question
If the superclass does not have a default no-arg constructor, what must happen?
answer
Every subclass must explicitly invoke the superclass constructor using the keyword SUPER.
question
How can a subclass method invoke the overridden method found in the superclass?
answer
By using the super keyword
question
What is a protected Member?
answer
Protected members of a class may be accessed by methods in a subclass and by methods in the same package as the class. This limits what has access to the member.
question
In a UML diagram how is a protected member designated?
answer
#
question
How do we prevent a method from being overridden in the superclass by a subclass?
answer
By using the final modifier. public final void message();
question
What is the Object class?
answer
The Java API has a class named object, which all other classes directly or indirectly inherit from. Tow of the most useful members that classes inherit are the toString and equals methods.
question
Why does every class have a toString or equals method?
answer
because every class inherits from the object class.
question
Can static members be overridden?
answer
NO. Static members are a property of a class and therefore cannot be overridden.
question
What is the difference between overloading and overriding?
answer
Overloading is when a method has the same name as one or more other methods, but a different parameter list. Although overloaded methods have the same name, they have different signatures. When a method OVERRIDES another method however, they both have the same signature.
question
Is this syntactically correct? if (dogs[4] == null){..}
answer
Yes.