Journey to Java: Episode 6 “Intro to Object-Oriented Programming”

Java is among many programming languages that is an object-oriented programming language.

Object-oriented programming is about creating objects that contain both data and methods. It has several advantages over procedural programming:

  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP helps to keep the Java code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
  • OOP makes it possible to create full reusable applications with less code and shorter development time

What is a class and what is an object?

Class

Classes are like templates to create objects. They pass down all variables and methods to the instances of the class. I like to imagine the class is a factory. Say you have a toy factory, the factory isn’t a toy, but it creates toy objects with a blueprint.

Object

Objects are the “thing” being created by a class. It’s a way to represent real world things in code. There is no cat datatype but we can create a cat object that can walk, meow, eat, and have a color. Objects should have two traits, state and behavior. State is attributes of the object such as a “color”. Behavior would be something like “jump on the counter”

How to create a new class in Java

Create a file inside the src folder with a capital letter appended by .java. An example would be Car.java. Inside that file create a new class.

public class Car {}

Create a field or attribute

Classes allow us to create variables that can be used throughout the entire class. These are know as class variables or fields. When creating a field for a class you must also declare an access modifier similar to the class definitions. As a general rule when defining an access modifier in a field we want to use the access modifier private. Private adheres to encapsulation, which is one of the 4 pillars of OOP, hiding the field and methods from being accessed publicly while allowing access to only the class. To create a field we need an access modifier, a datatype, and a name:

public class Car {
private String make;
private String model;
private String color;
}

Now that we have a blueprint set up to spit out cars from our car factory we can start to create instances of car objects.

Creating instances of objects

Inside our main method we can start to call to our Car class.

public class Main {
public static void main(String[] args){
Car ford; = new Car();
Car chevy; = new Car();
}}

Methods can be preformed on instances of classes as it is a new datatype. we do not want to access these fields from other classes. Instead we should create class methods inside the class we want to work with.

Creating a class method

To adhere to encapsulation we want to make a public class method to be used through the program. To work on an instance of a class we can use the keyword this as a placeholder. In order to set the field of this we need to make it equal the parameter passed in to the class method. This is known as the setter method.

public class Car {
private String make;
private String model;
private String color;
public void setMake(String make) {
this.make = make;
}}

Now that we have a public way to operate and set a fields information we can go back to the main method.

public class Main {
public static void main(String[] args){
Car ford; = new Car();
Car chevy; = new Car();
ford.setMake("Ford");}}

From this point we could repeat this process to finish a full car object.

public class Main {
public static void main(String[] args){
Car ford; = new Car();
Car chevy; = new Car();
ford.setMake("Ford");
ford.setModel("Mustang");
ford.setColor("Silver");
}}public class Car {
private String make;
private String model;
private String color;
public void setMake(String make) {
this.make = make;
}
public void setModel(String model) {
this.model = model;
}
public void setColor(String color) {
this.color = color;
}
}

At this point in the process we are able to create a class, create an object that is an instance of that class, and access the fields of the object and update them using a setter. Now we need to be able to access those values using a getter method.

public class Car {
private String make;
private String model;
private String color;
public void setMake(String make) {
this.make = make;
}
public void setModel(String model) {
this.model = model;
}
public void setColor(String color) {
this.color = color;
}
Public String getMake() {
return this.make
}
}

This has now created a method where we can return the value of the current instance we are working with. Now we can go to the main method and try to call this public method.

public class Main {
public static void main(String[] args){
Car ford; = new Car();
Car chevy; = new Car();
ford.setMake("Ford");
ford.setModel("Mustang");
ford.setColor("Silver");

System.out.println("make is " + ford.getMake());
}}

This is the end of this part of object oriented programming in Java. There will be multiple blogs on this amazing topic. In review we talked about:

  • What is OOP?
  • What is a class?
  • What is an object?
  • How to create a class.
  • How to create an object.
  • How to create fields.
  • How to create class methods.
  • How to set with setter methods.
  • How to get with getter methods.

Student at The Flatiron School in Washington DC