Java Tutorial: Object Oriented Programming


Posted 2020/06/01

Object oriented programming is a type of programming which uses custom data types (classes) which has certain data fields (variables) and operations (methods). Object oriented programming allows programmers to use these classes to create instances (objects) that can have properties of the class template.

Classes & Objects

A class is a template with methods and properties objects are based off of. Every object is made from a class and will have access to the methods from that class. Classes can help stop repetition of code in your programs.

In this example we will be making a class representing a rectangle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class Rectangle{ //class
    private double width, length; //fields

    public Rectangle(double l, double w){ //constructor
        width = w;
        length = l;
    }
    public double area(){ //calculate area
        return width*length;
    }
    public double getWidth(){ //width
        return width;
    }
    public double getLength(){ //length
        return length;
    }
    public String toString(){
        return width+"x"+length+":"+area()+"u^2";
    }
}

In the code above, we made our rectangle class which has 2 properties/fields; width and length, a constructor, and 3 methods. The constructor is a special method used to initialize objects of the class. The constructor in this class is new Rectangle(double l, double w). Now let’s make some Rectangle objects.

1
2
3
4
5
6
7
8
public class Main{
    public static void main(String[] args){
        Rectangle r1 = new Rectangle(10,5); //create object
        Rectangle r2 = new Rectangle(5,5);
        System.out.println(r1); //toString
        System.out.println(r2);
    }
}

In the runner class above, we make two rectangles; one with dimensions of 10x5 and another with 5x5. We then run their toString() methods in the println. The output should be the following:

1
2
5.0x10.0:50.0u^2
5.0x5.0:25.0u^2

This shows that we have made the two rectangles with their dimensions and the program calculated the area of each object which is 50units2 and 25units2

Inheritance

Inheritance is when one class acquires methods or fields of another class. A class in java can only inherit from one other class. The class that inherits is known as the subclass and the class that the methods and fields are inherited from is known as the superclass.

Continuing our example from above, we will be making a Square subclass because squares are rectangles.

1
2
3
4
5
6
7
8
public class Square extends Rectangle{ //square inherits from rectangle
    public Square(double l){ //square constructor
        super(l, l); //run rectangle constructor to get methods and fields
    }
    public double area(){ //override rectangle method
        return Math.pow(getLength(),2);
    }
}

In this new class we have a new constructor that takes one argument because squares have equal side lengths. We then run super(l,l) so that we can pass data to the superclass’s constructor so we can use the fields and methods in the superclass. However, in this class we make a change to the area method to make it use exponents instead of multiplication. This is known as an override and it replaces the area() method in the Rectangle superclass for one specific to the Square class.

We can create objects of the Square class and use its methods just like the Rectangle class

1
2
3
4
5
6
7
public class Main{
    public static void main(String[] args){
        Square s1 = new Square(10);
        System.out.println(s1.getLength());
        System.out.println(s1);
    }
}

Upon running this program we get this output:

1
10.0x10.0:100.0^2

This shows that we have create a square of side length 10 and an area of 100units2

Polymorphism

Polymorphism is when an object takes on other data type forms. One case where polymorphism occurs is when a subclass object is used instead of a superclass object. This is also possible with interfaces. To have polymorphism, the object needs to pass more than one IS-A test of the superclass. For example:

A Square IS-A Rectangle

Because this test is true, any object of the Square class can be used instead of an Rectangle object.

1
2
Rectangle r = new Square(10);
System.out.println(r);

Instance vs Static

Static methods or variables can be accessed from the class without creating an object. Some examples of this are methods from the String and Math classes. Instance methods or variables can only be used after creating an object. Static methods can be created by adding the static keyword to the method header.