OOPS Basics

Object Oriented Programming Style
Agenda
-
What is OOPS ?
-
Classes
-
Objects
-
Constructors
-
this keyword
-
Real life example



What is OOPS?
Object Oriented Programming Style (OOPS) is a programming style that deals with classes & objects.
The main aim of object-oriented programming is to implement real-world entities, for example, object, classes, abstraction, inheritance, polymorphism, etc.
A class is a blueprint for the object.

What is a class ?
What is an object ?
What is a class ?
An object is an entity that has a state and some behaviour.

Car

Car
Blueprint

Blueprint

Blueprint
Factory

Blueprint
Factory

Blueprint

Blueprint

Blueprint
Properties:

Blueprint
Properties:
- Model name

Blueprint
Properties:
- Model name
- Color

Blueprint
Properties:
- Model name
- Color
- Price

Blueprint
Properties:
- Model name
- Color
- Price
Functionalities:

Blueprint
Properties:
- Model name
- Color
- Price
Functionalities:
- Drive

Blueprint
Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock
class Car {
}

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock
class Car {
}
String
String
int

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock
class Car {
String model;
String color;
int price;
}
String
String
int

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock
class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
}
String
String
int

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock
class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
}
String
String
int

Properties:
- Model name
- Color
- Price
Functionalities:
- Drive
- Lock/Unlock
class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
String
String
int

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
Blueprint

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
Blueprint
class Main {
public static void main(String[] args) {
}
}

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
Blueprint
class Main {
public static void main(String[] args) {
Car c1 = new Car();
}
}

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
Blueprint
class Main {
public static void main(String[] args) {
Car c1 = new Car();
}
}
c1

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
Blueprint
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
}
}
c1
c2

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
Blueprint
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
}
}
c1
c2

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
Blueprint
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
}
}
c1
c2

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
}
}

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
}
}
class Car
model
color
price
Properties

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
}
}
class Car
model
color
price
Data members

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
}
}
class Car
model
color
price
drive()
lock()
unlock()
Data members
Functions

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
}
}
class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
}
}
class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
}
}
class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
}
}
class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
}
}
class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
Hatchback
Black
100000

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
}
}
class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
Hatchback
Black
100000

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
}
}
class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
Hatchback
Black
100000

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
c2.color = "Gray";
System.out.println(c2.color); // Gray
}
}
class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
Hatchback
Black
100000

class Car {
String model = "Hatchback";
String color = "Black";
int price = 100000;
void drive() {
System.out.println("Zoom zoom zoom");
}
void lock() {
System.out.println("Car is now locked");
}
void unlock() {
System.out.println("Car is now unlocked");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
// Reading the properties
System.out.println(c1.color); // Black
System.out.println(c1.model); // Hatchback
System.out.println(c1.price); // 100000
c2.color = "Gray";
System.out.println(c2.color); // Gray
}
}
class Car
model
color
price
drive()
lock()
unlock()
Data members
Class methods
model:
color:
price:
Hatchback
Black
100000
c1
c2
model:
color:
price:
Hatchback
Gray
100000
A Java constructor is special method that is called when an object is instantiated.
It has the same name as the class itself and is invoked automatically at the time of object construction.

Constructors

Example: Complex Numbers
Write a class to store and perform operations on complex numbers.
In Java, this keyword is used to refer to the current object inside a class method.

this keyword
class ComplexNumber {
int real;
int imaginary;
// Constructor
ComplexNumber(int real, int imaginary) {
this.real = real;
this.imaginary = imaginary;
}
}
OOPS Basics
By Tarun Luthra
OOPS Basics
- 176