3. Inheritance
• Allows a class to reuse features &
functionalities of another class.
• Class it inherits from is also called Base Class
or Super Class or Parent class.
• The inheriting class is called Sub Class or
Derived Class or Child class.
• Sub Class can add more attributes & methods.
• Establishes an “IS-A” relationship from sub
class object to super class object.
4. Inheritance Hierarchies
• Examples of Inheritance Hierarchy:
Shape Hierarchy, Living Being Hierarchy.
• Specialization – go down the hierarchy.
• Generalization – go up the hierarchy.
6. Java Keywords related to Inheritance
• extends => inherits from
Example:
public class Plant extends LivingBeing {
…
…
}
• super(…) => invoke the base class constructor, must be
the first statement in the sub class constructor.
• super.someMethod() => scope resolution, helps to
invoke a method with the same name from the base
class.
7. Properties of Inheritance
• Every sub class object “is an” object of the
base class as well.
• Examples: Triangle is a Shape, Plant is a
LivingBeing
• Inheritance is NOT symmetric.
“A is a B” does not imply “B is-an A”.
• Inheritance is transitive.
If “A is a B” & “B is a C” => A “is a “ C.
8. Casting w.r.t Inherited Classes
• Example Class B inherits from Class A.
• A a = new B();
• The above is valid as every B is an A, upcasting
is implicit.
• B b = (B) a; // downcasting has to be
explicit, potential ClassCastException.
• B b = new A(); // Invalid as every B is not an A
9. Composition & Aggregation
• Indicates “has a” relationship between classes.
• Examples:
Car has an engine, LaptopBag has a Laptop.
• Aggregation => component can exist
independently of aggregate object. Example:
Laptop can exist independently of LaptopBag.
• Composition => component cannot exist
independently of composite object. Example:
Engine cannot exist independently of Car.
10. Association
• Classes that collaborate with each other within
the business domain.
• Can be uni-directional or bi-directional.
• Example: Customer and Teller at a Retail Bank.
The Teller serves many Customers. The Customer
can be serviced by many Tellers.
• Cardinality: 1-to-1, 1-to-Many, Many-to-Many.
• Many-to-Many may be broken into two 1-toMany relationships using a junction (link) class.
11. Using
• A class uses a helper class as a parameter to a
method or as a local variable of a method.
• Not same as association or aggregation.
• Example: In fluid dynamics, a method of a
class which calculates the flow in 2
dimensions can use a ComplexNumber class
as a parameter or local variable.
12. Instantiation (Generics)
• Not an object level relationship, but a class-toclass relationship.
• Share the definition of a class across different
data types.
• Example: A Queue can hold objects of type
HumanBeing or Animal or Integer. Behavior of
Queue (FIFO) is same irrespective of whether
it is holding Animals or HumanBeings or
Integers.
13. UML Notation for OO Relationships
• See the UML Notation Summary pdf file