Know the difference between functional programming and OOP
Know basic terminology in OOP
OOP Design
Structured Programming:
Function & program is divided into modules
Every module has its own data & functions which can be called by other modules.
OO approach goes one step further
Lets the programmer represent problem space elements
The elements in the problem space and their representations in the solution space are referred to as "objects"
Objects have both data and methods
Objects send and receive messages to invoke actions
Objects of the same class have the same data elements and methods
Key idea in object-oriented programming:
The real world can be accurately described as a collection of objects that interact.
An object has:
state - descriptive characteristics
behaviors - what it can do (or what can be done with it)
Bank Account object:
state: account number and current balance
behaviors: ability to make deposits and withdrawals
Note that the behavior of an object might change its state
An object is created using a class
A class is the blueprint of an object
The class uses methods to define the behaviors of the object
A class represents a concept in problem domain
Multiple objects can be created from the same class
The class that contains the main method of a Java program represents the entire program
You could (in a game, for example) create an object representing a rabbit
It has data:
How hungry it is
How frightened it is
Where it is
And methods:
eat, hide, run, dig
Members of a class:
Attributes or Fields (instance variables, data)
For each instance of the class (object), values of attributes can vary, hence they are instance variables
Methods (instance methods)
Person class
Attributes: name, address, phone number
Methods: change address, change phone number
Alice object
Name is Alice, address is ...
A class is like a factory that creates objects of that class
We ask a class to create an object by using the keyword:
new
A Clock represents a 12-hour clock
Its internal state includes: hour, minute, second
Its interface allows telling the clock that a second has elapsed and querying the clock for the time:
Objects must be initialized before use.
We must specify what is the initial state of the object before we can use it.
"new" creates a new object from
specified type:
new String();
new Book();
Returns the reference to the created object
String s = new String();
Dog d = new Dog();
Rectangle rectangle = new Rectangle();