Java and Object Oriented Programming [#1]
What is Object Oriented Programming(OOP)?
This is a programming paradigm based on the concept of objects, which can contain data and code which data is in the form of fields also known as attributes or properties and code in the form of procedures also known as methods.
Many of the most widely used programming languages such as C++, Javascript and Python namely but a few implement Object Oriented Programming other than Java.
What are Objects?
Objects are always called instances of a class and has a state and behavior. For example, a dog is an object that has
- State: name, breed, color etc
- Behaviors: barking, running, sleeping etc
Class
A class in java is a blueprint for the object and before we create an object we first need to define the class.
Think of the class a sketch(prototype) of a house and it contains all details about the windows, floors, doors and the others. Based on these descriptions we build the house and therefor the House is the object.
NB: Many different houses can be made based on the same description of the class.
Creating a class in Java
We can create a class in Java using the class keyword. Such as
class className {
// fields
// nethods
}
In this, the fields and methods represent the state and behavior of the object respectively.
- fields are used to store data
- methods are used to perform some operations
Our car object we can create the class
class Car {
// state or field
String name;
int manufacturer;
String color;
// behavior or method
void braking() {
System.out.println("Car is braking");
}
void speeding() {
System.out.println("Car is speeding");
}
}
In the above example, we have created a class named Car which has fields name, manufacturer, color and methods braking(), speeding().
Car is a prototype and now we can create any number of cars using the prototype. All the bicycles will share the fields and methods of the prototype.
Java Objects
An object is an instance of a class. Our class we created called Car then sportsCar, suvCar and f1Car can be considered as the objects of that class.
Creating an Object in Java
This is how an object of a class is created in java
className objectName = new className();
// for Car class
Car sportsCar = new Car();
Car suvCar = new Car();
// sportsCar and suvCar are the names of the objects.
Let's observe the use of the new keyword along with the constructor of the class to create an object. Constructors are almost the same as methods and use the same name as the class only that it doesn't have a return type. for example;
class Car {
Car() {
// constructor body
}
}
// Car() is a constructor and doesn't have a return type
Access Members of a class
The name of the object is along with . (the dot operator) we can be able to access the members of a class. For example;
class Car {
// state or field
String name;
int manufacturer;
String color;
// behavior or method
void braking() {
System.out.println("Car is braking");
}
void speeding() {
System.out.println("Car is speeding");
}
}
// create object named suvCar
Car suvCar = new Car();
// access fields
suvCar.name;
suvCar.maufacturer;
// access methods
suvCar.speeding();
Here you can be able to access the fields and methods of this class.
We are now able to understand what a class is and an object. Let's look at a fully working example;
class Lamp {
// stores the value for light
// true if light is on
// false if light is off
boolean isOn;
// method to turn on the light
void turnOn() {
isOn = true;
System.out.println("Light on? " + isOn);
}
// method to turnoff the light
void turnOff() {
isOn = false;
System.out.println("Light on? " + isOn);
}
}
class Main {
public static void main(String[] args) {
// create objects led and halogen
Lamp led = new Lamp();
Lamp halogen = new Lamp();
// turn on the light by
// calling method turnOn()
led.turnOn();
// turn off the light by
// calling method turnOff()
halogen.turnOff();
}
}
Output:
Light on? true
Light on? false
In our next part we are going to look at methods, overloading and constructors.
Later we shall look into concepts like abstraction, encapsulation, inheritance and polymorphism.
For a further and more deep explanation about Object Oriented Programming, check out this wonderful playlist.
Object Oriented Programming in Java by Kunal Kushwaha.
Feel free to connect with me on github, Linkedin and Twitter thank you.