Chapter 13 Aggregation, Composition, and Inheritance Flashcards

1
Q

When an object is complex, you shouldn’t use just one class. How should you break it up?

A

Into constiuent parts, defining one class as the whole and the other classes as part of the whole.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is it called when the “whole” class is the exclusive owner of the parts classes?

A

A composition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In a composition relationship, how many owners can a composition part have?

A

Only one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Example composition hierarchy, human body example.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the relationship between a containing class and one of its part classes know as?

A

A has-a relationship.

Example: Each human body has a brain and has a heart.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What type of has-a relationship is it when one class is whole and other classes are parts of the whole?

A

Aggregation, similar to composition, but there are no additional constraints that requires parts to be exclusively owned by the whole.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does a Universal Modeling Language (UML) class diagram show?

A

The relationship between a programs classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What do the symbols in a UML mean?

Example also attached

A
  • A solid line between two classes represents an association - a relationship between classes.
  • On an association line, a solid diamond indicates a composition relationship, and a hollow diamond indicates an aggregation relationship. The diamond goes next to the container class.
  • The labels on the association lines are called multiplicity values. They indicate the number of object instances for each of the two connected classes.
  • The * multiplicity value represents any size number, zero through infinity.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is required to implement a program that uses aggregation and composition?

A
  • Define one class for the whole and define seperate classes for each of the parts.
  • For a class that contains another class, declare an instance variable inside the containing class such that the instance variable holds a reference to one or more of the contained class’s objects.
  • Typically, for association line with * multiplicity values, use an ArrayList to implement the instance variable associated with the asterisked class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are other considerations when implementing a program that uses aggregration and composition?

A
  • If two classes have an aggregation relationship with non-exclusive ownership, then store the contained class’s object in an instance variable in the containing class, but also store it in another variable outside of the containing class, so the object can be added to another aggregation and have two different “owners.”
  • If two classes have a composition relationship, then store the contained class’s object in an instance variable in the containing class, but do not store it elsewhere. That way, the object can have only one “owner.”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is inheritance?

A

When a new class is derived from an existing class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why is it called inheritance?

A

Because the new class inherits/borrows all the features (data and methods) of the existing class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why is inheritance important to programming languages?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Example inheritance hierarchy that keeps track of people in a department store

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In an inheritance hierarchy, the pairs of classes are linked together. For each pair what are the classes called?

A

The more generic class is call the superclass and the more specific class is called the subclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In an inheritance which class is derived from the other?

A

A subclass is derived from a superclass.

Meaning the subclass inherits all of the superclasses data and methods.

17
Q

In an inheritance, which class has more capability?

A

Subclasses have more capability, they can do everything that a superclass can do, plus more.

18
Q

What are the requirements for a UML class diagram for inheritance relationships?

A

That is use an arrow for the relationship, with a hollow arrowhead pointing to the superclass.

19
Q

What is the difference in arrows between UML class diagrams and the direction in which inheritance flows?

A

They go opposite one another.

20
Q

What are the benefits of inheritance?

A

- 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

21
Q

How can a constructor call a constructor in its superclass? (syntax)

A

super (“arguments”);

22
Q

When is the only time a constructor call is allowed?

A

If it’s the very first line in the constructor’s body.

This rule is applicable in two circumstances:

  • constructor calls to constructors in the same class (using this)
  • -* for constructor calls to constructors in the superclass (using super)
23
Q

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?

A

The reference variable dot prefix

24
Q

In what circumstances, in relation to an instance method, are variable dot prefix’s unnecessary?

A
  • If you call a method that’s in the same class as the class you’re currently in.
  • If you call a method that’s in the superclass of the class you’re currently in.
25
Q

What is it called when a subclass has a method with the same name and the same parameter types as a method in its superclass?

A

Method overriding

26
Q

What are properties if a subclass contains an overriding method?

A
  • By default, an object of the subclass will use the subclass’s overriding method (an not the superclass’s overridden method)
  • Sometimes, an object of the subclass may need to call the superclass’s overridden method. To do that, preface the method call with “super.” (don’t forget the dot.
27
Q

What occurs if a subclass and superclass have methods with the same name, same parameter types, and different return types?

A

A compilation error.

28
Q

What can be used to specify a method definition cannot be overrideen with a new definition in a subclass?

A

The final modifier in the method heading

Example:

29
Q

Under what circumstances would you want to use final?

A
  • If you think the your method is perfect, then you might want to use final to prevent anyone else from overriding it in the future.
  • final methods might run faster since the compiler can generate more efficient bytecode for them (because there’s no need to prepare for the possibility of inheritence).
30
Q

What should you do if you want to specify that a class cannot have any subclasses?

A

Use the final access modifier in the class heading.

Example:

public final class FullTime

31
Q

What are the two basic types of class relationships?

A
  • Aggregation and composition relationships are when one class is a whole and other classes are part of the whole.
  • An inheritance relationship is when one class is a more detailed version of another class. The more detailed class is a subclass, and the other class is a superclass. The subclass inherits the superclass’s members (variable and methods).
32
Q

Why do we call aggregation and composition relationships “has a” relationships?

A

Because one class, the container, has a component class inside of it.

33
Q

Why do we call an inheritance relationship an “is a” relationship?

A

Because one class, a subclass, is a more detailed version of another class.