Topics Covered

At the end of this tutorial we should be able to fully understand the following:

  • What are the fundamentals of Object Oriented Programming
  • What is an Object
  • Java Class Fundamentals
  • Inheritance Concepts
  • What is an Interface

Background of Java Object Oriented Programming (OOP)

Object Oriented programming attempts to model real life objects through its code. You might not appreciate this concept if you didn’t experience the pain of not able to reuse existing codes not unless you physically copy it and then use it on your program. This is no longer the case with java. In java, if you need to reuse existing functionality of a class, you just need to add it on your library and voila you now have the access to all the class methods and variables.

Java Object

What is an object? Look around you. On your room, you might be having a chair, aircon, table, a laptop, bulb, and a cellphone. These objects has two characteristics state and behavior.

Lets’ take for an instance a desk fan, its state is turned on, it is currently running on 3rd, and still rotating. As you would have notice there are 2 possible state of desk fan, it’s either on or off. How about the gear speed of the desk fan? a standard desk fan can have gears from 1 – 5. And how about the current rotation settings of its blade? Is it still oscillating or at rest?  These state and behavior of an object can be applied to programming as well. View state as variables and behaviors as methods.

If you still not able to correlate real life objects to java objects, don’t worry. Once we model the object deskFan, you would have a better understanding of this concept.

Java Class

Let’s go back to my previous example  object, a desk fan. There are many of these kind in existence. Different model with different manufacturers. All of which are made from a single blue print.

A class is basically a blue print of an object. From this blueprint, different types of deskfan can be created. Below class is an example implementation of object deskfan.

You might have noticed that class deskFan will likely represent the object deskfan in real world. As described on our explanation for object oriented programming, object deskFan states are powerOn, currentGearSpeed, and oscillation. And its behaviors as represented by class is changing the current state of the object, represented by methods; turnOn, changeGearSpeed, and changeOscillation.

As we have said earlier class is a blue print for objects. So let’s try to put it into programming. Let’s create multiple instances of object deskFan. Let’s imagine that we have two deskfan in our room one is owned by Fred and one is owned by Anthony.

If you run the program above it would print:

As you can see there were two objects created which is derived from the blue print of class DeskFan. Although both are of the same object DeskFan, they have different state. Try the above example.

Download the sample java project Java Tutorial – DeskFan

Inheritance in Java

If you are serious about learning about java, spend some time on understanding Inheritance. let’s consider again our object DeskFan, as you can see it has basic functionality. Consider a new model, a deskfan itself but with an added functionality like remote controlled DeskFan, lets call this DeskFanA. And consider another model that has an additional functionality of having a remote control functionality and with a timer lets call this DeskFanB. Though different functionality it is still derived from our blue print which is the original DeskFan, so those two new models are called subclass. And the original deskfan is called a superclass. In java programming, only one superclass for each su