Topics Covered
- 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)
Java Object
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
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.
package com.javatutorial.example; /* * DeskFan object definition */ public class DeskFan { boolean powerOn = false; int currentGearSpeed = 3; boolean isOscillating = true; void turnOn(boolean newVal) { powerOn = newVal; } void changeGearSpeed(int newVal) { currentGearSpeed = newVal; } void changeOscillation(boolean newVal) { isOscillating = newVal; } public void showState() { System.out.println("Printing the current state of object DeskFan"); System.out.println("Is it powerOn:" + powerOn); System.out.println("What is the current speed:" + currentGearSpeed); System.out.println("Is it oscillating:" + isOscillating); } }
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.
package com.javatutorial.example; public class Main { public static void main(String[] args) { DeskFan fredDFan = new DeskFan(); //this represents fred's deskfan DeskFan anthonyDFan = new DeskFan(); //this represents anthony's deskfan fredDFan.turnOn(true); fredDFan.changeGearSpeed(2); fredDFan.changeOscillation(true); fredDFan.showState(); anthonyDFan.turnOn(true); anthonyDFan.changeGearSpeed(1); anthonyDFan.changeOscillation(false); anthonyDFan.showState(); } }
If you run the program above it would print:
Printing the current state of object DeskFan Is it powerOn: true What is the current speed:2 Is it oscillating:true Printing the current state of object DeskFan Is it powerOn: true What is the current speed:1 Is it oscillating:false
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
The syntax for defining the superclass is not that complex but requires your full attention.
class DeskFanA extends DeskFan{ //you can write your own implementation here }
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
interface DeskFanInt { void turnOn(boolean newVal); void changeGearSpeed(int newVal); void changeOscillation(boolean newVal); }
and this is how to implement an interface
public class DeskFanA implements DeskFanInt { public static void main(String[] args) { } @Override public void turnOn(boolean newVal) { } @Override public void changeGearSpeed(int newVal) { } @Override public void changeOscillation(boolean newVal) { } }
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.