Professional Documents
Culture Documents
Programming
OOPs the what is it and they why we use it
What is it not and why do we
need OOPs?
Let’s imagine we are going to write a program that will be a car
driving game.
We know we are going to need cars and that those cars will be
drawn on the screen.
Make
Model
Colour
Start
Drive
Park
What would be good for our cars to
be able to do in our game? DisplayInfomation
What is it not and why do we
need OOPs?
How might that look in code?
void printCarDetails() {
System.out.println(“Make is “ + make);
System.out.println(“Model is “ + model);
System.out.println(“Colour is “ + colour);
System.out.println(“Manufactured in “ + yOM);
System.out.println(“Price : $“ + price);
}
What is it not and why do we
need OOPs?
• This works fine when we have one car but when we have
more than one cars and all our cars are different colours,
makes, models, prices and have different years of
manufacture we start to have a problem.
• How could we store all the information for the many cars in
our game?
What is it not and why do we
need OOPs?
Older procedural ways of coding:
• Hard to reuse code when things are slightly different
• Difficult to manage large amounts of data
• Don’t follow the way humans think about the world very well
• Encourage cutting and pasting of code and then making small
changes
OOPs
What Object Oriented
Programming allows us to do
is create a structure in code
that “knows” it’s properties
and can “do” it’s methods
Properties Methods
Colour Park
Every time we make a new
Year DisplayInfomation version of the class it is called
an object
Price
What the car class might look
like.
public class Car {
// instance variables (what I know)
String make;
String model;
String colour;
int yOM;
float price;
void Start() {
// some code missing here
}
void Drive() {
// some code missing here
}
void Park() {
// some code missing here
}
}