Classes and Objects
OOP or Oops
Quick review
-
When to use a queue?
-
What operations has a stack?
-
What are the advantages of a list?
- How to check if a key is present in a map?

Contents
-
Object Oriented Programming
-
Defining Classes
-
Using Classes
-
Summary
Wacky Racer





Bad joke
of the day
Hamlet: "ToBe || !ToBe..."
Programmer: "True !"
Object Oriented Programming
The objects are Data Structures which contains data and can do actions
Represent the real world as objects and relationships between them
Object example
Ordinary Car
- Company
- Model
- Color
- Owner
- Type (sedan, combi ...)
- Price
- Release date
- Doors

Do you have more?
Wacky Car
- Color
- Top Speed
- Driver
- Position
Objects
Objects group together:
- Primitives - int, double, char ...
- Objects - String, Date ...
Car
- String color
- int topSpeed
- String driver
- int position

Objects (2)

Car
Objects have actions
- Move left
- Move right
Objects (3)

Car
Actions represented as methods
public void moveLeft() {
...
}
public void moveRight() {
...
}Classes (1)
In the game there are 4 cars with color, top speed, driver and position
So in our program we have:
String car1Color = "red";
int car1TopSpeed = 160;
int car1Position = 1;
String car1Driver = "Ivancho";
String car2Color = "blue";
int car2TopSpeed = 240;
int car2Position = 2;
String car1Driver = "Mariiki";
...
car3
...
car4Classes (2)
How to avoid repetition and so much variables?
Create a template for my cars!


Classes (3)
A class is the blueprint from which individual objects are created
Define a class Car:
public class Car {
FIELDS
METHODS
}Let's define our Car
class Car {
public String color;
public int topSpeed;
public String driver;
public int position;
public void moveLeft() {
...
}
public void moveRight() {
...
}
}Break (10 min)

How to make a lot of cars now

Car car1 = new Car();
car1.color = "red";
car1.driver = "Ivancho"
Car car2 = new Car();
car2.color = "blue";
car2.driver = "Mariika";
You actualy create new type!
How to stay private?
Not going in Galaxy!
public String color;Everybody can change my color
car1.color = "red";private String color;I am the master of my color.
Nobody knows that I have color.
Get and Set methods
How to be the master of my fields but tell the others that I have color and they can do something about it?
class Car {
private String color;
public void setColor(String c) {
color = c;
}
public String getColor() {
return color;
}
}Constructors
Do something when I create you
public class CLASSNAME {
CLASSNAME() {
}
CLASSNAME([ARGUMENTS]) {
}
}
CLASSNAME obj1 = new CLASSNAME();
CLASSNAME obj2 = new CLASSNAME([ARGUMENTS]);public class Car {
public String color;
Car() {
}
Car(String someColor) {
color = someColor;
}
}
Car car1 = new Car();
car1.color = "red";
Car car2 = new Car("red");If you don't define constructor, there is an empty one automatically created
Summary
- OOP represent the real world as objects and relationships between them
- Classes are like templates for objects
- Objects are instances of classes
- Each class has fields and methods (data and actions)
- It is a good practice to use get and set methods for fields
- Constructors help to initialize the fields when object is created
Daily quiz

Questions?
Homework or not
Thank you :)
Lecture 6 - Classes and Objects
By naughtyspirit
Lecture 6 - Classes and Objects
- 426