When an object is complex, you shouldn’t use just one class. How should you break it up?
Into constiuent parts, defining one class as the whole and the other classes as part of the whole.
What is it called when the “whole” class is the exclusive owner of the parts classes?
A composition
In a composition relationship, how many owners can a composition part have?
Only one.
Example composition hierarchy, human body example.
What is the relationship between a containing class and one of its part classes know as?
A has-a relationship.
Example: Each human body has a brain and has a heart.
What type of has-a relationship is it when one class is whole and other classes are parts of the whole?
Aggregation, similar to composition, but there are no additional constraints that requires parts to be exclusively owned by the whole.
What does a Universal Modeling Language (UML) class diagram show?
The relationship between a programs classes.
What do the symbols in a UML mean?
Example also attached
What is required to implement a program that uses aggregation and composition?
What are other considerations when implementing a program that uses aggregration and composition?
What is inheritance?
When a new class is derived from an existing class.
Why is it called inheritance?
Because the new class inherits/borrows all the features (data and methods) of the existing class.
Why is inheritance important to programming languages?
Because it allows programmers to reuse existing software. More specifically, the existing class is used by all of the classes that are derived from it.
Example inheritance hierarchy that keeps track of people in a department store
In an inheritance hierarchy, the pairs of classes are linked together. For each pair what are the classes called?
The more generic class is call the superclass and the more specific class is called the subclass.
In an inheritance which class is derived from the other?
A subclass is derived from a superclass.
Meaning the subclass inherits all of the superclasses data and methods.
In an inheritance, which class has more capability?
Subclasses have more capability, they can do everything that a superclass can do, plus more.
What are the requirements for a UML class diagram for inheritance relationships?
That is use an arrow for the relationship, with a hollow arrowhead pointing to the superclass.
What is the difference in arrows between UML class diagrams and the direction in which inheritance flows?
They go opposite one another.
What are the benefits of inheritance?
- It helps with code reusability
– A superclasses code can be used for multiple subclasses. That eliminates code redundancy and make debugging and upgrading easier.
– A programmer can use an existing class to easily create a new subclass (no need to “reinvent the wheel”)
- Smaller modules (because classes are split into superclasses and subclasses)
– That makes debugging and upgrading easier
How can a constructor call a constructor in its superclass? (syntax)
super (“arguments”);
When is the only time a constructor call is allowed?
If it’s the very first line in the constructor’s body.
This rule is applicable in two circumstances:
What is unneccesary, in an instance method, if you call a method that’s in the same class as the class you’re currently in?
The reference variable dot prefix
In what circumstances, in relation to an instance method, are variable dot prefix’s unnecessary?