Introduction to Object Oriented Programming

What we’ll cover

What is procedural programming?

Why do we use procedural programming?

Why do we not use procedural programming?

Why do we use OOP?

Object Oriented Programming (OOP)

The 3 aspects of an object

Classes

Class naming conventions

Encapulsation

Instance-Fields

Encapsulation

// class signature
public class Person {
	// instance variables (fields)
	private String name;
	private Integer age;
	private Boolean isFemale;

	// constructor
	public Person(String name, Integer age, Boolean isFemale) {
		this.name = name;
		this.age = age;
		this.isFemale = isFemale;
	}
}

Instance-Methods

Instance-Variables (Fields)

Constructors

Nullary Constructor

public class Person { // class signature
	private String name; // instance variable

	public Person() { // constructor signature
		this.name = "Leon";
	}
}

Default Constructor

public class Person { // class signature
	private String name; // instance variable

	public Person() { // constructor signature
	}
}

Paramterized Constructor

public class Person { // class signature
	private String myName; // instance variable

	public Person(String name) { // constructor signature
		this.myName = name; // setting instance variable
	}
}

Multiple Constructors

public class Person { // class signature
	private String myName; // instance variable

	// no-arg constructor
	public Person() { // constructor signature
		this.myName = "Leon"; // setting instance variable
	}

	public Person(String name) { // constructor signature
		this.myName = name; // setting instance variable
	}
}

Assigning initial values
From Constructor

public class Person { // class signature
	private String myName; // instance variable
	private Character gender; // instance variable

	// no-arg constructor
	public Person() { // constructor signature
		this.myName = "Leon"; // setting instance variable
		this.myGender = 'M'; // setting instance variable
	}

	public Person(String name, Character gender) { // constructor signature
		this.myName = name; // setting instance variable
		this.myGender = gender; // setting instance variable
	}
}

Calling Constructors
From Constructors

public class Person { // class signature
	private String myName; // instance variable
	private Character myGender; // instance variable

	// no-arg constructor
	public Person() { // constructor signature
		this("Leon", 'M'); // nested constructor call
	}

	public Person(String name, Character gender) { // constructor signature
		this.myName = name; // setting instance variable
		this.myGender = gender; // setting instance variable
	}
}

Setters
(Mutators)

public class Person { // class signature
	private String myName; // instance variable

	public Person(String name) { // constructor signature
		this.myName = name; // setting instance variable
	}

	public void setName(String differentName) { // method signature
		this.myName = differentName; // setting instance variable
	}
}

Setters
(Mutators)

public class Person { // class signature
	private String myName; // instance variable

	public Person(String name) { // constructor signature
		setName(name); // BAD DESIGN; method can be overriden
	}

	public void setName(String differentName) { // method signature
		this.myname = differentName; // setting instance variable
	}
}

Getters
(Accessors)

public class Person { // class signature
	private String myName; // instance variable

	public Person(String name) { // constructor signature
		this.myName = name; // setting instance variable
	}

	public void setName(String differentName) { // method signature
		this.myname = differentName; // setting instance variable
	}

	public String getName() {
		return this.myName;
	}
}