Cse 1325 - W7 - 10042021
Cse 1325 - W7 - 10042021
Week of 10/04/2021
You can specify your own initial value for a primitive-type instance variable by
assigning the variable a value in its declaration, as in
private int numberOfStudents = 10;
Primitive Types vs Reference Types
Java’s types are divided into primitive types and reference types.
Primitive types
boolean, byte, char, short, int,
long, float , double
null
represents a “reference to nothing.”
Constructors
Each class you declare can optionally provide a constructor with
parameters that can be used to initialize an object of a class when the
object is created.
If you declare a constructor for a class, the compiler will not create a default constructor for
that class.
Constructors
Why use constructors?
If you do not provide a constructor, then your object will use the
default initializations for the instance variables.
Why?
Take the example of the electronic devices that surround us every day.
They have simple interfaces that allows you to perform actions without
know the details behind those actions.
Encapsulation
The separation of interface and implementation is extremely useful because
it allows us to use objects without understanding how they work.
This vastly reduces the complexity of using these objects and increases the
number of objects we are capable of interacting with.
Objects may communicate with one another, but they are not normally
allowed to know how other objects are implemented.
Getter
A method that returns the value of a private variable
Setter
A method that changes the value of a private variable
Abstraction
Snack Machine
Coke Machine
What is this?
A tree?
Yes
Association
Composition
Aggregation
Inheritance
UML Relationships
Association
Tortoise is an Animal
Otter is an Animal
Slow Loris is an Animal
Inheritance
"is a" or "has a"?
association/composition/aggregation?
"whole/part" relationship?
not "whole/part"
Association
"is a" or "has a"? Creep is part of Tortoise?
Tortoise is part of Creep?
Creep is a Tortoise?
Creep has a Tortoise? Yes – Creep is "whole" and Tortoise is "part"
"has a" relationship Can the Creep exist without the Tortoise?
Yes
association/composition/aggregation?
association/composition/aggregation?
"whole/part" relationship?
Can the "parts" – Bathroom and Lobby –
Visitor Center is part of Bathroom/Lobby?
exist without the "whole" - Visitor Center?
Bathroom/Lobby is part of Visitor Center?
The client code does not need to know how those methods are
implemented.
Separating Interface from Implementation
When the client code does know how a class is implemented, then the
programmer might write client code based on the class's
implementation details.