Introduction to Object-Oriented Programming in Java
Sani Yusuf
Lead Software Engineering Content Developer
// 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 }
class Passport {
String firstName; // Passport holder's first name
String lastName; // Passport holder's last name
Passport() {
// Constructor of Passport class
}
}
class Passport {
String firstName; // Passport holder's first name
String lastName; // Passport holder's last name
Passport() {
this.firstName = "David"; // Set property inside constructor
this.lastName = "Beckham";
}
}
class Passport {
String firstName;
String lastName;
// Constructor with parameters
Passport(String firstName, String lastName) {
}
}
this
keyword refers to the Passport
object
class Passport {
String firstName;
String lastName;
Passport(String firstName, String lastName) {
this.firstName = firstName; // Setting class properties
this.lastName = lastName; // with constructor parameters
}
}
new
keyword
// Passport Class with constructor
class Passport {
String firstName;
String lastName;
// Constructor method
Passport(String firstName,
String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
}
// Main Class
public class Main {
// main method (program entry point)
public static void main(
String[] args) {
// Passing parameters to constructor
// Passport(firstName, lastName)
Passport myPassport =
new Passport("Michael","Jackson");
System.out.println(
myPassport.firstName); // Michael
}
}
Introduction to Object-Oriented Programming in Java