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 subclass is allowed, however a superclass can have as many subclasses as you like.

The syntax for defining the superclass is not that complex but requires your full attention.

From the above declaration deskFanA will inherit all the methods and variables of the parent class DeskFan. Because the deskFanA might have additional functionality like adjusting height then aside from the existing methods of deskFan we would also be needing adJustHeight method.

But what if deskFanA has different implementation of showState? Then we can just override the deskFan showState method. How can we do that? Just copy the same method name and declaration to deskFanA and put your own implementation. That’s what we call method overriding. If you encounter this term, always think that it is simply copying the same method signature of our parent class and put in our own implementation.

Java Interface

A java interface is a template of classes. It doesn’t have any concrete implementation.  If your class is a blueprint of an object, an interface is the template of your class. It forces the the implementing classes to have the basic functionality define on the interface class.

and this is how to implement an interface

As you can see the implementing class should contain what is the functionality of the interface class. Keyword is implements. Once the required methods were implemented, we can add now add our own implementation of object DeskFan. We might be putting some logic in determining the current state of our newly created object or we could also add additional methods for our version of DeskFan. Moreover, as you might have already noticed, interface class has the sole purpose of providing the template for the implementing classes.

Conclusion

At this point in time, you should already a full understanding of the OOP concepts and how it is being used in java programming. The OOP topics are a bit advance, but since Object oriented programming is the foundation of java, it’s sensible to start with this topic.

So far you must already have a good grasp of how coding in java is being done, let’s move on to the next topic.

Previous
Home
Next