Intro to OOP

What is Object-Oriented Programming?

What are Objects?

In order to understand objects in Java, first allow me to explain what everyday objects are. In your daily life, you encounter objects that perform operations. Your pencil is an object that can write on a sheet of paper. Your camera is an object that is able to take a photo of you. You use all these objects to perform operations all throughout your day.

How do everyday objects have to do with Java?

Java is a programming language that is classified as a strictly Object-Oriented language. Just like how you need objects every day to help you perform operations, Java also needs to create individual objects in order to perform its operations in a program. How Java creates objects in a program is by using a Class. A class is a blueprint or set of attributes and purposes that an object will have. For example, a digital camera has a set of attributes such as the size of its lenses or the camera resolution, and its purpose, of course, is to take pictures or videos.

Example in Code:

public class Camera {

private int lensSize;

private int pictureWidth, pictureLength; // in pixels

public void takePicture() {

// Takes a picture!

}

public void takeVideo(int videoLength) {

// Takes a video with the specified length

}

}

As shown in the code segment above, the syntax of a class contains the public class, the fields or attributes of the class (lensSize, pictureWidth, and pictureLength) and some methods including takePicture()which is one of the operations or procedures that this object is able to perform.


Now, we are going to create an object in Java using the class we just analyzed. To do so, we will need a constructor. A constructor is what creates the Java object in memory (and instantiates the class). It will create an instance of the class as an object and initialize the fields or attributes of the object for you. An example is shown below

public class Camera {

private int lensSize;

private int pictureWidth, pictureLength;

public void takePicture() {

// Takes a picture!

}

public void takeVideo() {

// Takes a video with the specified length

}

public Camera(int lens, int width, int length) {

lensSize = lens;

pictureWidth = width;

pictureLength = length;

}

}

The constructor is able to take a few variables as parameters and initialize the fields according to the programmer's wishes. An example of creating the object and using it is shown in the Driver class below.

public class Driver {

public static void main(String[] args) {

// Creates a new Camera object with the dimensions

Camera myCamera = new Camera(5, 300, 500);


myCamera.takePicture(); // take a picture

myCamera.takeVideo(20); // take a 20-second video

}

}

The driver class has declared and initialized a Camera object and taken a picture and video with it.

These operations can be done with any kind of object. The programmer simply has to create his/her own class and define the fields and methods that he/she would like to.

Interfaces

An interface is a kind of "abstract" class in which attributes of the object are declared before they are actually implemented in a non-abstract class. The benefit of this is that a user of the interface would roughly know what an object of this type would be able to do without yet knowing how it would be implemented.


A common example is Java's List interface. The List interface can be implemented in many classes: ArrayList, LinkedList, Stack, Vector, and many more. All these classes implement the List interface which includes methods such as add, remove, get, set, and many more. This way, the programmer is sure that regardless of which class he/she chooses to implement the List with, he can be guaranteed that the basic methods and attributes of a List are able to be accessed. The partial definition of a List is shown below:

// The "E" parameter allows the user to pass an object type

public interface List<E> {

public void add(E e);

public E remove(int ind);

public void set(int ind, E e);

public E get(int ind);

}

Notice how the methods are not declared with a method body. This is because these methods are yet to be implemented in one of the classes previously mentioned.