Quiz #2 review Flashcards
What are the three types of relationships that can exist between object?
Inheritance, Containment, and Polymorphism
Inheritance definition
“inheriting” code from an already created class. Allows user to add on features to starting classes
Which = “is-a” relationship
Inheritance; sailboat is a boat
Containment definition
“has-a” relationship; car has-a engine
Base Class vs Derived Class
Base class is the starting class while the derived class inherits from the base class
Object superclass properties
cannot be inherited from
Object superclass methods
Equals, clone, toString
overriding definition
child class has the same method as the parent class
overloading definition
When a class contains two methods with the same name, but one has constructors while the other acts as the no-args constructor
Be able to set up the constructor for a derived class and use the super keyword
Be able to set up the constructor for a derived class and use the super keyword
Be able to draw an inheritance hierarchy diagram
Be able to draw an inheritance hierarchy diagram
How do you add inheritance or containment to a class?
Class extends subclass
Differences between private, protected, public, and no access
private can only be accessed by the class, protected can be accessed by the parent class and child classes, public can be accessed by anyone, and no access means nothing has access. No modifier means any file in the current directory may access
private
can only be accessed by the class its contained in
protected
can only be accessed by the parent class and any child classes
public
anyone can use
no access
no access
no public/private etc,
can be accessed by any file in the same directory
Polymorphism definition
when a program is running, you send off a child object as a parent object and you want the parent object to act like a child
When does a ClassCastException occur
When you try to send a parent object into a child
abstract classes definition
very general class that’s built to be inherited from. Defines methods with abstract keyword, but does not fill in body.
Any class that inherits must have all abstract methods defined.
Must have at least 1 abstract method
final classes definition
opposite of abstract classes.
can never be inherited from.
extends is forbidden.
will never be a “is-a”
Interfaces definition
purely abstract class.
never has variables.
all methods are abstract be default.
When should you use abstract classes?
When a class is very generic