OOP Midterm Flashcards
Uses-a types (3)
Dependency
Association
Direct Association
Has-a types (2)
Aggregation
Composition
Is-a types (1)
Inheritance
Implements-a types (1)
Interface Implementation (realization)
Dependency
transient relationship between two classes. Implementation of class A that uses class B does not affect class A definition
Class B is unaware of the existence of the class A (Asymmetry)
(Minimize this relationship to reduce coupling)
Association
relationship between Class A and Class B last as long as both object live at runtime. One class is owned by the object of another class
dependency example
A method of class Sequence uses a Stack class to verify palindrome. A student enrollment class uses a student class in its method
i.e. A method of class A is passed as parameter of class B and returns a value of class B.
association example
A teacher class has a method to assign students to a class while students can (or can not) specify which teacher is teaching their class
i.e. class A has an attribute that is class B
cardinality
Defines multiplicity between objects (one-to-one, one-to-many…)
direct association
A strong connection between classes where both classes are generally aware of each other and have direct references or attributes that link them
direct association example
A teacher class where each student is defined a specific teacher and each teacher has a direct reference to their student. Both classes are aware of each other
association subtypes
aggregation
composition
A child class that cannot exist independently of its parent class
composition example
Class car has a class Engine. Since Car owns Engine, it controls the lifetime of this class meaning once Car is destroyed, so is Engine
aggregation
Child class is independent of its parent class meaning its a standalone and unidirectional class that can exist independent of it’s parent class. Association is unidirectional.
aggregation example
Class University aggregates Class department objects. The department objects can exist independently of the university
i.e. Class A contain objects of class B over period of time. Both objects survive individually
restricted form of aggregation
composition
composition
Class A composes or owns class B and represents “part-of” relationship. Class B cannot have an existence without Class A (High dependency)
Inheritance
Class B is derived and dependent on Class A. Class B can access and utilize members of Class A and extend or specialize behavior of Class A
Interface Implementation
Class B is a realization of interface A. Class B must override all Class A methods
Expansion
Mechanism for building on top of existing classes. Building upon capabilities of class A in class B
Contraction
Specializing and narrowing down the capabilities of a class. Class B restricts or refines behaviors or attributes of inherited Class A
transative
A property of relationship that extends from one element to another through a series of elements.
i.e. Class A is related to Class B and Class B is related to Class C so Class A is related to Class C
concept reuse in inheritance
Class B can override behavior of parent class. Class B shares definitions of methods but not implementations
code reuse in inheritance
Class B inherits behavior from Class A
Contractual Obligation
Overridden method sis required to keep the same name, return type, and parameters as the base class
override
Method replacement keyword that tells compiler to replace a method from inherited base class with its derived class’s implementation
abstract class
a restricted class that acts similar to an interface and can have both abstract and regular methods
abstract method
Can only be used in an abstract class and it does not have implementation
Forms of Inheritance (8)
Specialization
Specification
Construction
Generalization
Extension
Limitation
Variance
Combination
Specialization
Child class is a special case of parent class
Specification
The parent class defines behavior that is implemented in the child class but not in the parent class.
Parent class can be an abstract class or interface and child class is a realization of the parent
Construction
Child class makes use of parent’s behavior but is not a subtype of the parent (breaks principle of substitution)
Principle of Substitution
subclass is a subtype of the parent meaning the object type associated with a value held by a variable may not exactly match the type associated with the declaration for that variable
Shape s = Triangle t;
Generalization
Child class modifies or overrides some methods of its parent class (opposite of subclassing for specialization)
must override one method from parent
Extension
Child class adds new functionality to the parent class but does not change inherited behavior
no modifications to parent methods, new methods are owned by the child class
Variance
The child class and parent class are variants of each other, and the the class-subclass relationship is arbitrary
Two or more classes have similar implementations but do no posses any hierarchical relationship between the concepts represented by the classes
Multiple (Combination)
The child class inherits features from more than one parent class.
i.e. inherits from parent class and implements multiple interfaces
benefits of inheritance
software reuse
consistency of interface
rapid prototyping
polymorphism
information hiding
polymorphism
permits programmer to generate high-level reusable components that can be tailored to fit different applications by changes in their low-level parts
Inheritance Disadvantages
Execution Speed
Program size
Message Passing Overhead
Program Complexity
Execution Speed
Inherited methods deal with arbitrary subclasses are often slower than specialized code
Limitation
The child class restricts the use of inherited parent class behavior