Slides OOP Part 2 Polymorphism Composition Part 2
Slides OOP Part 2 Polymorphism Composition Part 2
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.
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.
Motherboard
Dimensions (HAS A) ramSlots: int
cardSlots: int
width: int
bios: String
height: int
dimensions:Dimensions
depth: int
loadProgram(String
programName)
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)