0% found this document useful (0 votes)
11 views10 pages

Slides OOP Part 2 Polymorphism Composition Part 2

Uploaded by

hungle1012004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Slides OOP Part 2 Polymorphism Composition Part 2

Uploaded by

hungle1012004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Composition

In the previous video, I talked about composition and compared it to inheritance.


Inheritance is a way to reuse functionality and attributes.
Composition is a way to make the combination of classes act like a single coherent
object.

COMPLETE JAVA MASTERCLASS


Composition Part 2
Composition is creating a whole from different parts

I built this personal computer, by passing objects to the constructor, like assembling
the computer.
I can actually hide the functionality further.
In this case, I'm not going to allow the calling program to access those objects,
the parts directly.
I don't want anybody to access the Monitor, Motherboard, or ComputerCase directly.

COMPLETE JAVA MASTERCLASS


Composition Part 2
Use Composition or Inheritance or Both?

As a general rule, when you're designing your programs in Java, you probably want
to look at composition first.
Most of the experts will tell you that as a rule, look at using composition before
implementing inheritance.
You saw in this example, I actually used both.
All of my parts were able to inherit a set of attributes, like the manufacturer and
model.
The calling code didn't have to know anything about these parts to get
PersonalComputer to do something.

COMPLETE JAVA MASTERCLASS


Composition Part 2
Why is Composition preferred over Inheritance in many
designs?
The reasons composition is preferred over inheritance:
• Composition is more flexible. You can add parts in or remove them, and these
changes are less likely to have a downstream effect.
• Composition provides functional reuse outside of the class hierarchy, meaning
classes can share attributes & behavior, by having similar components, instead of
inheriting functionality from a parent or base class.
• Java's inheritance breaks encapsulation because subclasses may need direct
access to a parent's state or behavior.

COMPLETE JAVA MASTERCLASS


Composition Part 2
Why is Inheritance less flexible?

Inheritance is less flexible.


Adding a class to or removing a class from a class hierarchy may impact all
subclasses from that point.
In addition, a new subclass may not need all the functionality or attributes of its
parent class.

COMPLETE JAVA MASTERCLASS


Composition Part 2
Adding a Digital Product

Let's say I want to include digital


products, such as software products in
my product inventory.
Should Digital Product inherit from
Product?
Here, I show the model with Digital
Product, inheriting from my current
definition of Product.
If I do this, this would mean Digital
Product has Product's attributes, but
this isn't true now.

COMPLETE JAVA MASTERCLASS


Composition Part 2
Adding a Digital Product

A digital product wouldn't really have


width, height, and depth, so this
model isn't a good representation of
what I want to build.
It would be better if I didn't have
those three attributes on Product, but
instead used composition to include
them on certain products, but not all
products.

COMPLETE JAVA MASTERCLASS


Composition Part 2
Revised Class Diagram

Consider this revised class diagram.


Product
I haven't completely removed the class
model: String
hierarchy, but I've made the base class, manufacturer: String
Product, more generic.
Inherits
(IS A)

Motherboard
Dimensions (HAS A) ramSlots: int
cardSlots: int
width: int
bios: String
height: int
dimensions:Dimensions
depth: int
loadProgram(String
programName)

COMPLETE JAVA MASTERCLASS


Composition Part 2
Revised Class Diagram

I've removed the width, height, and


depth attributes from Product and Product
made a new class, Dimensions, with model: String
those attributes. manufacturer: String

Inherits
And I've added an attribute to (IS A)
Motherboard, which is dimensions,
which has those attributes. Motherboard
Dimensions (HAS A) ramSlots: int
cardSlots: int
width: int
bios: String
height: int
dimensions:Dimensions
depth: int
loadProgram(String
programName)

COMPLETE JAVA MASTERCLASS


Composition Part 2
Revised Class Diagram

Is this a better model?


Product
Well, it's more flexible.
model: String
manufacturer: String
This design allows for future
enhancements to be made, like the Inherits
(IS A)
addition of the subclass Digital Product,
without causing problems for existing Motherboard
code that may already be extending Dimensions (HAS A) ramSlots: int
Product. cardSlots: int
width: int
bios: String
height: int
By placing width, height, and depth into depth: int
dimensions:Dimensions
loadProgram(String
a dimension class, I can use programName)
composition to apply those attributes to
any product.
COMPLETE JAVA MASTERCLASS
Composition Part 2

You might also like