WEEK 3 - INHERITANCE AND DEBUGGING PROGRAMS Flashcards
What is the subclass?
It is a class based on an existing class (the superclass). It is more specific than the superclass.
Example.
“circle” is more specific than “shape”
What is the superclass?
It is an existing and generalized class compared to the subclass.
Example.
“shape” is more specific than “circle”
What does a subclass inherit from the superclass?
All fields and methods.
How can methods be overridden?
The subclass can create a method with the same name as another method in the superclass. This version of the method will override/replace the method from the superclass.
What keyword is used to create/declare a subclass in Java?
extends.
Example.
public class Circle extends Shape {
private Point _center;
private double _radius;
public void draw() {…}
}
What are some of the benefits of inheritance?
- One superclass can be used to make many subclasses.
- Reusability within the code.
- Improves the structures of large programs (hierarchical).
- Modular programming (encapsulation).
What is a subclass capable of?
Inheriting from a superclass, it can override methods and add new properties and new methods.
How many superclasses can there be?
One.
How do you call the superclass version of an overridden method?
Using the super keyword.
Example.
super.draw()
**You can NOT override private methods
Can you override field variables?
No, as it causes confusion and nasty bugs.
A subclass has access to what?
All non-private fields and methods.
What does protected mean?
A field or variable is visible to subclasses without making it public.
Are superclass’s Constructor inherited?
No. They are invoked explicitly (using the super keyword) or implicitly (automatically invoked).
You must use the keyword super to call the superclass
constructor. Invoking a superclass constructor’s name in a subclass causes which error?
Syntax.
What signature must be added on all overrides?
@Override
It is usually put on a separate line. Compiler will give a syntax error if an override doesn’t occur.