What is Object Oriented Programming?
Object oriented programming (adorably we call it OOP) is all about breaking down large codebase into individual pieces and establishing a relationship between them. In java, those individual pieces are called objects. So, in other words, object oriented programming means creating individual objects and interacting between them to achieve the desired outcome.
To understand it better, let’s consider three friends who started a new restaurant.
Name | Role |
---|---|
Rahul | He is an excellent cook. You must try his biriyani. |
Harsha | His specialization is garnishing. He can even make ordinary food look mouth watering. |
Suman | Suman can welcome customers with a charming smile, take their orders, and receive the bill. |
Now on a beautiful day, a customer comes into the restaurant and then –
Suman |
Hello sir (with a smile), please have a seat. What can I get for you? |
||||
Can I have one chicken fried rice and one egg drop soup please? |
Customer |
||||
Suman |
Sure sir. I will be back with your food. |
||||
Suman goes back to the kitchen and… | |||||
Suman |
Hey Rahul, make one chicken fried rice and one egg drop soup please. |
||||
(After 20 minutes) Hey Harsha, I am ready with the food. Please make them presentable. |
Rahul |
||||
Harsha |
(After 5 minutes), Here you go Suman. |
||||
Suman receives the food and serves it to the customer… |
This is an OOP system in action. Individually all three of them are like objects. Each one of them knows how to do their own job. Each one talks to the others to get different parts of the whole business process done. They only ask others for an outcome, but they never tell others how to do the job. Pretty simple, right?
How to define what an object knows and what it can do?
Instance variable: There can be something that an object knows about itself. We can specify them as instance variables. Those instance variables define the state of the object. Let’s consider bike objects. One bike can be red in color and registration number as A123. Another bike can be black in color and registration number as B456.
So, we can say, we have two instance variables for Bike – color and registration number. For one bike the values are red and A123, and for the other bike, the values are black and B456 respectively.
Method: Also there will be a couple of things that an object can do. Like a bike can start, speed up, slow down, stop. Those individual capabilities can be defined as methods (sometimes we call it behavior). If we want, we can also send things to a method. In a method, we can use parameters to specify what that method can accept, and a caller can send arguments corresponding to those parameters.
Method can also return value. Every method must declare a return type. If the return type is void, that means the method will not return anything. Otherwise you can declare a method to give a specific type of value back to the caller.
Ok, I got that. But how can I create an object? I heard an object is created from class.
Oh! I forgot to tell you about class. A class is a blueprint for an object. It tells the virtual machine how to create an object of that particular class. Each object made from that class can have their own values (state). For example you might use bike class to make hundreds of different bikes each having different colors, labels and so on. So the class is the blueprint and the object is an instance of that class. When a class is created, no memory is allocated. But when an object is created out of that class, memory is allocated to that object.
You just mentioned Virtual Machine. What is that?
A java virtual machine (JVM) provides a runtime environment for a java application. This JVM is platform dependent, meaning for every operating system a separate JVM is available which is capable of converting the input into underlying OS readable language. To write an application –
- First we write the java code and store it with the .java extension.
- When we compile it, a .class file is generated with the same name. This file contains platform independent bytecode (not human readable text) which is understandable by JVM.
- Then we run this class file in JVM, who understands this bytecode and translates it into underlying OS readable language.
So, you see, the combination of bytecode and JVM makes the java language platform independent. Once you compile the java files, we generate bytecodes. Which is same for all JVM. Now we can run the same bytecode to different OS using OS specific JVM. That’s why we say in java
“write once and run anywhere”.
This is a cool feature. Can you write a java code and run it?
I hope you have java installed in your system. You can check that by using command “java -version
” on command prompt and you should get a valid version.
C:\Users\aditi>java -version
java version "1.8.0_201"Java(TM) SE Runtime Environment (build 1.8.0_201-b09)Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
Now create a class HelloJava and save it as HelloJava.java
public class HelloJava {
public static void main(String[] args) {
System.out.println("Hello java");
}
}
Then go to the directory where you saved it and run below command –
C:\Users\aditi>javac HelloJava.java
You should get a file called HelloJava.class inside the same directory. To get this output you can run below command –
C:\Users\aditi>java HelloJavaHello java
When you execute the above command, you basically tell JVM to load the Bike.class and run “public static void main(String[] args)” method, which is the starting point of a program.
If I want to print something, what should I do?
You have already seen it. You can use the println()
method to print something in Java. This println()
method will add a new line after each output.
public class HelloJava {
public static void main(String[] args) {
System.out.println("Hello");
System.out.println("World");
System.out.println("How are you");
}
}
Output:
Hello
World
How are you
If you don’t want that new line, you have to use print()
method. This method does not insert a new line at the end of the output.
public class HelloJava {
public static void main(String[] args) {
System.out.print("Hello ");
System.out.print("World ");
System.out.print("How are you");
}
}
Output: Hello World How are you
That’s it for now. Will meet again with new topic,