Week 6 Flashcards
A general relationship that describes an activity between 2 classes
Association
How are associations denoted? How do the 2 classes relate?
A straight line arrow and direction. They relate through their objects.
To communicate between each other, 2 classes must have a link between them. What is this link represented by?
Association.
What are 2 form of association?
Aggregation and composition
A relationship where an objects owns a collection of other identical objects
Aggregation, also known as a has-a relationship
What are some examples of aggregations?
Professor has many students, hotel has many suites. One way, reverse not possible
T/F - An aggregation relationship is represented by a field in the aggregating class
True
Aggregated class
public class Name {}
Aggregating class
public class Student {
private Name name;
private Address address;
…
}
Relationship where a class contains other references to other classes
Composition (has-a relationship)
IE. Room has a door, Car has a wheel
T/F - Composition is a special cast of aggregation
True
In __________ a child can exist independently of the parent, in ___________ the child cannot exist independent of the parent
Aggregation, composition
Composition vs Aggregation, types of association
Composition: Strong association
Aggregation: Weak association
Inheritance has what type of relationship?
is-a relationship
the ___class is a specialization of the _____class, and has characteristics of it
subclass, superclass
Can a subclass be treated like a parent type?
Yes. A motorcycle is-a vehicle. A car is-a vehicle.
Can every class be inherited?
No, final classes cannot.
Can a class inherit from more than one class ?
No, Java does not support multiple inheritance
What rules are there pertaining to the super keyword?
Super() can only be used inside the subclass constructor and it must be the first statement.
Can any other method in the subclass call the superclass constructor? (super())
No
Do we always need to call the superclass constructor when we instantiate a subclass object? Why?
Yes. When a subclass object is instantiated the subclass object must also automatically execute one of the constructors of the superclass.
Otherwise you wouldn’t be able to inherit anything.
How do you prevent a method in a class from being overridden in a subclass?
There’s a number of ways.
1. Use a static method
2. Make a private access modifier
3. You can prevent a method being overridden by subclasses by declaring it as final.
Something about default modifiers but that’s eh
Overload vs override
1.
Override = methods of same name and signature
Overload = methods of same name, different signature
- Chaining can be achieved using the “this” keyword, while subclasses may call super classes using the “super” keyword
- Constuctors can be overloaded, but constructors cannot be inherited.
Overloading occurs at _______-time, while overriding occurs at ___-___
compile time, run time
What does compile time denote? What does run time denote? How does the former relate to static?
Compile time denotes the time of program
compilation, Run-time denotes the time of program
execution
Static refers to anything created at compile time, and remains