[7]Lecture 10: Inheritance
"has a" is a part of what concept?
aggregation
Why does every class have a toString or equals method?
because every class inherits from the object class.
When a subclass is instantiated, the superclass _____________ constructor is executed first.
default
What keyword indicates that a subclass inherits from a super class?
extends
"is a" is a part of what concept?
inheritance
When a sub class inherits fields and methods from a super class, they are not included in the sub class.
true
In a UML diagram how is a protected member designated?
#
Given two classes, Auto() and SportsCar() which extends Auto(), when a new SportsCar object is created, java will first create what object?
First an object of the superClass Auto() is created, then the subclass SportsCar() object.
Must the call to the superclass constructor be the first java statement in the subclass constructor?
Absolutely.
How do we prevent a method from being overridden in the superclass by a subclass?
By using the final modifier. public final void message();
How can a subclass method invoke the overridden method found in the superclass?
By using the super keyword
If the superclass does not have a default no-arg constructor, what must happen?
Every subclass must explicitly invoke the superclass constructor using the keyword SUPER.
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, Java will automatically call the superclass's default constructor, OR NO-ARG constructor, JUST BEFORE the code in the subclass's constructor executes.
Are private fields and methods of the superClass inherited in the subClass.
NO. Private members of the superclass are not inherited.
Can private members of the superclass be accessed by the subclass?
NO. Private members of the superclass are not inherited.
Can static members be overridden?
NO. Static members are a property of a class and therefore cannot be overridden.
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.")
NO. The sub class child cannot inherit private fields or methods from the superclass.
SuperClass constructors ARE or ARE NOT inherited.
NOT. Constructors are NOT inherited.
What is the difference between overloading and overriding?
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.
What is a protected Member?
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.
What is the Object class?
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.
What does the super keyword refer to?
The super() keyword refers to an objects superclass, and it can be used to explicitly call the constructor of the superclass.
When a subclass is instantiated, what is instantiated first?
The superclass default constructor. Think of this as "The parent has to be created before the child is created."
We can say that a subclass is a specialized version of the SuperClass.
True
In a UML diagram, how do we indicate that a subclass extends a super class?
We use an open arrow head pointing from the subclass to the super class.
Is this syntactically correct? if (dogs[4] == null){..}
Yes.
Can a method be oeverloaded and simultaneously be overriding a method in a superclass?
Yes. For example, if the superclass has the method int getApples(), then the subclass can have the methods
What happens if you have written your own constructor in the superclass, and you omit the default constructor...
You can add a no-arg constructor in the superclass, or when writing the subclass constructor, issue a call to the superclass constructor.