Object Oriented

Java is an object-oriented programming (OOP) language, which means that we can design classes, objects, and methods that can perform certain actions. These behaviors are important in the construction of larger, more powerful Java programs.

Classes

Syntax

One fundamental concept of object-oriented programming in Java is the class.

A class is a set of instructions that describe how a data structure should behave.

Java provides us with its own set of pre-defined classes, but we are also free to create our own custom classes.

Classes in Java are created as follows:

1
2
3
4
5
//Create a custom Car class

class Car {

}

Constructors

Let’s start by creating the starting state of our class. We can do this by adding a class constructor to it.

  1. A class constructor will allow us to create Doginstances. With a class constructor, we can set some information about the Dog.
  2. If we do not create a class constructor, Java provides one that does not allow you to set initial information.

The code below demonstrates how a class constructor is created:

1
2
3
4
5
6
7
class Car {

    //The class constructor for the Car class
    public Car() {

    }
}

Instance Variables

When we create a new class, we probably have specific details that we want the class to include. We save those specific details into instance variables.

Here is an instance variable in the Car class that describes a detail that we might want to associate with a car:

1
2
3
4
5
6
7
8
9
class Car {

    //Using instance variables to model our Car class after a real-life car
    int modelYear;

    public Car() {

    }
}

Constructor Parameters

Perfect! By adding a class constructor and creating instance variables, we will soon be able to use the Dog class. However, the class constructor Dog is still empty. Let’s modify this by adding parameters to the Dogconstructor.

You can think of parameters like options at an ice cream store. You can choose to order a traditional ice cream cone, but other times you may want to specify the size of the cone or the flavor of the ice cream.

For the Dog class, we can specify the initial dog age by adding parameters to the class constructor.

  1. Parameters allow data types to be created with specified attributes.

Let’s add parameters to our Car class constructor:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Car {

    //Use instance variables to model our Car class after a real-life car
    int modelYear;

    public Car(int year) {

        modelYear = year;
    }
}

The Main Method

This is Java’s built-in main method. We will learn more about methods and keywords around the main method later on, but first let’s understand what the purpose of main is.

  1. When Java runs your program, the code inside of the main method is executed.

For now, you can ignore the keywords in the main method that we have not yet covered. You will learn about them later in the course.

Objects

To use the Dog class, we must create an instance of the Dog class. An instance of a class is known as an object in Java.

The example below demonstrates how to create a Car object:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Car {

    int modelYear;

    public Car(int year) {

        modelYear = year;

    }

    public static void main(String[] args){

        Car myFastCar = new Car(2007);

    }
}

In the example above, we create a Car object named myFastCar. When creating myFastCar, we used the class constructor and specified a value for the required int parameter year.

2007 is the model year of myFastCar. Note that we declared the new object inside the main method.

Method

A method is a pre-defined set of instructions. Methods are declared within a class. Java provides some pre-defined methods available to all classes, but we can create our own as well.

Let’s create a new method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Car {

    int modelYear;

    public Car(int year) {

        modelYear = year;

    }

    //Our new method to help us get "started"
    public void startEngine() {

        System.out.println("Vroom!");

    }

    public static void main(String[] args){

        Car myFastCar = new Car(2007);

    }
}

Here is an example of calling a method on an object using the Car class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Car {

    int modelYear;

    public Car(int year) {

        modelYear = year;

    }

    public void startEngine() {

        System.out.println("Vroom!");

    }

    public static void main(String[] args){

        Car myFastCar = new Car(2007);
        myFastCar.startEngine();
    }
}

Inheritance

One of the object-oriented programming concepts that allows us to reuse and maintain code more efficiently is called inheritance. It is used to share or inherit behavior from another class. Let’s look at an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Car extends Vehicle {

    int modelYear;

    public Car(int year) {

        modelYear = year;

    }

    //Other methods omitted for brevity...

    public static void main(String[] args){

        Car myFastCar = new Car(2007)
        myFastCar.checkBatteryStatus();

    }
}

class Vehicle {

    public void checkBatteryStatus() {

        System.out.println("The battery is fully charged and ready to go!");

    }
}