Welcome
I N T R O D U C T I O N T O O B J E C T- O R I E N T E D P R O G R A M M I N G I N J AVA
Sani Yusuf
Lead Software Engineering Content
Developer
My experience with Java
Working with Java since 2010
Algorithms
Android mobile applications
Course created in collaboration with Miller
Archury
Senior Software Engineer at Google
Former Microsoft Engineer
1 [Link]
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Topics covered
Explore Java's approach to Object-Oriented
Programming
Simplify the complexities of Object-
Oriented Programming
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
What is object oriented programming (OOP)
OOP is a programming paradigm that models code based on objects and real-world items
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
What is a class?
Classes are like Cookie cutters
Cookies created take the shape of the
cookie cutter
Classes are blueprints for our code
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Structure of a class
// Cookie Class
class Cookie {
// Cookie class's code goes here
}
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Naming object instance
NOTE: The Main class and main method will be used throughout this course
All code will be inside the Main class
The main method is the entry point of all Java code
public static class Main {
class Cookie {
public static void main(String[] args) {
// Give the object instance a name
Cookie myCookie = new Cookie();
}
}
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Let's practice!
I N T R O D U C T I O N T O O B J E C T- O R I E N T E D P R O G R A M M I N G I N J AVA
Adding properties
I N T R O D U C T I O N T O O B J E C T- O R I E N T E D P R O G R A M M I N G I N J AVA
Sani Yusuf
Lead Software Engineering Content
Developer
Car example
Cars on the road can come in different forms, with features like color and model that
differentiate them.
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Differentiate cars
Even when cars are the same, they differ by color, license plate, vehicle registration.
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Adding properties to class
Properties describe a class
Properties can have different data types
// Car class
class Car {
String model; // Property for model of car
int topSpeed; // Property for car's top speed
boolean isInsured; // Property for current insurance state
}
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Constructors in Java
The Constructor is a method, always called when an object instance of a class is created
Constructor must have the same name as the class it is in
class Passport {
String firstName; // Passport holder's first name
String lastName; // Passport holder's last name
Passport() {
// Constructor of Passport class
}
}
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Setting properties inside the constructors
class Passport {
String firstName; // Passport holder's first name
String lastName; // Passport holder's last name
Passport() {
[Link] = "David"; // Set property inside constructor
[Link] = "Beckham";
}
}
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Constructors with parameters
Constructors can have parameters just like any other method
class Passport {
String firstName;
String lastName;
// Constructor with parameters
Passport(String firstName, String lastName) {
}
}
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Setting properties with constructor parameters
The this keyword refers to the Passport object
It is common to give constructor parameters same name as class properties
class Passport {
String firstName;
String lastName;
Passport(String firstName, String lastName) {
[Link] = firstName; // Setting class properties
[Link] = lastName; // with constructor parameters
}
}
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Creating object instances with parameters
Objects are created with the new keyword
// Main Class
Constructor parameters are passed during public class Main {
// main method (program entry point)
object creation public static void main(
String[] args) {
// Passport Class with constructor // Passing parameters to constructor
class Passport { // Passport(firstName, lastName)
String firstName; Passport myPassport =
String lastName; new Passport("Michael","Jackson");
[Link](
// Constructor method [Link]); // Michael
Passport(String firstName, }
String lastName){ }
[Link] = firstName;
[Link] = lastName;
}
}
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Let's practice!
I N T R O D U C T I O N T O O B J E C T- O R I E N T E D P R O G R A M M I N G I N J AVA
Class methods
I N T R O D U C T I O N T O O B J E C T- O R I E N T E D P R O G R A M M I N G I N J AVA
Sani Yusuf
Lead Software Engineering Content
Developer
Example of car properties
Cars can have colors.
Each car has a model.
Cars can also perform actions like go
on/off.
Cars can also give us data like a list of
devices paired with the car's Bluetooth.
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Types of methods
Void methods: Methods that do not return data
Non-Void methods: Returns data back
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Void methods
Void methods do not return any data
They are marked with the keyword void
// Main class
// Car class public class Main {
class Car { public static void main(
String[] args) {
void honkHorn() {
// Code for horn to sound Car myNewCar = new Car();
[Link]("beep beep"); [Link](); // beep beep
} }
} }
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Creating non void methods
Non void methods always have a return
type // Main class
public class Main {
Non void methods use the return keyword // main method
public static void main(
to return data
String[] args) {
// Formula class Formula myFormula =
class Formula { new Formula();
// Calling "getSquare" method
// Non void method [Link](
int getSquare(int number) { [Link](5)); // 25
// Return an "int" value }
return number * number; }
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA
Let's practice!
I N T R O D U C T I O N T O O B J E C T- O R I E N T E D P R O G R A M M I N G I N J AVA