Classes are a lot like cookie cutters. Just like people like cookies, programmers like objects. And where do cookies and objects come from? Cookie cutters and classes. Classes are used to create objects, just like cookie cutters are used to create cookies. After an object is created, it fits into a certain mold, but you can make minor adjustments to it . After a cookie is made, it keeps it's same shape, but you can add frosting to it.

Imagine trying to make the same cookie over and over again without a cookie cutter. What a pain, right?! Imagine all the defective cookies you would have. That mess is somewhat similar to procedural code. With a cookie cutter, you can make perfect cookies every time. With classes, you can make perfect objects every time.

After watching the videos, hopefully you'll see the great reusability that classes can provide.

Here is an example of a Java class that represents a person:

public class Person {
	private int age;
	private String name;
	
	//Constructor
	public Person() {
		this.age = 0;
		this.name = "John";
	}
	
	//Overloaded Constructor
	public Person(int age, String name) {
		this.age = age;
		this.name = age;
	}
	
	//Setter for the name
	//Note, parameter is a String
	public void setName(String name) {
		this.name = name;
	}
	
	//Setter for the age
	//Note - parameter is an integer
	public void setAge(int age) {
		this.age = age;
	}
	
	//Getter for the name
	//Note - returns a string
	public String getName() {
		return this.name;
	}
	
	//Getter for the age
	//Note - returns an integer
	public int getAge() {
		return this.age;
	}
	
	//A string representation of this class
	//Note - when the object is created, this method will
	//overide the println() method
	public String toString() {
		return "Hi, my name is " + this.name + ", and I am " + this.age + " years old. nn";
	}
}

And here is a Main class that makes use of this class

public class Main {
	public static void main(String[] args) {
		Person rob = new Person("Rob", 22);
		System.out.println(rob);
		
		//Unfortunately, Rob gets older...
		rob.setAge(53);
		System.out.println(rob);
	}
}

If you have any questions, please leave a comment or send me a message!