OBJECT ORIENTED PROGRAMING IN JAVA
Introduction:
Java is an object oriented programming language which is designed to program using classes and objects.
An object is desgined as an entity which has state and behavior and a object is an instance of a class. Eg a building (Object) which is constructed based on a blueprint.
Here: the state is defined by the variable in the class and the methods defines the object’s behavior.
A class is a blueprint which defines the state and behavior to its instance ie., Object. Eg. The blueprint of a building which has all the information of a building.
Inheritance:
Inheritance a technique which a sub/child class acquire all the properties of the parent/super class.
It is achieved by using "extends" keyword.
class Vehicle{
int noOfWheels;
int capacityOfFuelTank;
String fuelType;
public void start(){
System.out.println("Vroom Vroom");
}
}
// here vehicle is a super class. It has all the common properties of a vehicle.
class MotorCycle extends Vehicle{
int topSpeed;
int mileage;
}
This class inherits the vehicle super class by using extends keyword and this class also has some properties of its own.
class Car extends Vehicle{
boolean isAc;
boolean sunScreen;
}
Like the MotorCycle class, this Car class also inherits the vehicle super class by using extends keyword and this class also has some properties of its own.
Type of Inheritance:
There are 4 types,
Multiple Inheritance is not supported in Java which make Interface a powerful one.
Polymorphism:
Polymorphism, Like the word suggest one action is performed in many ways.
Types :
Compile time polymorphism :
Class Main{
public static void main(String[] args)
{
add(5.0,6.0);
add(5,6);
}
public static void add(int i, int j) {
System.out.println(i+j);
}
public static void add(double i,double j)
{
System.out.println(i+j);
}
}
Here there are 2 methods with the same name. The compiler decides which method to call based on the type of argument / number of argument.
In the first case it decides to call the second method and the second calls the first method respectively.
This is also called as method overloading.
There are two ways to overload the method in java
Run time polymorphism :
class Vehicle{
int noOfWheels;
int capacityOfFuelTank;
String fuelType;
public void start(){
Recommended by LinkedIn
System.out.println("Vroom Vroom");
}
}
// here vehicle is a super class. It has all the common properties of a vehicle.
class MotorCycle extends Vehicle{
int topSpeed;
int mileage;
public void start(){
System.out.println("ta ta ta troom");
}
}
public static void main(String[] args){
Vehicle myBike = new MotorCycle();
myBike.start();
}
The myBike.start() call the method from the MotorCycle class. This is decided during the run time which method to be called ie., (Method Dispatch).
This is also called as method overriding. And follows these below points
Abstraction:
The process of hiding the implementation and showing the functionality. An abstract class is created using abstract keyword. An abstract class cannot be instantiated like a normal class. A class does need to inherit the abstract class and instantiate an object.
An abstract class may have abstract method, while an interface only have abstract method.
Eg. While using self-start in a motorcycle. It just starts. Inside a big process goes.
There are 2 ways to achieve abstraction
abstract class and interface.
Eg:
abstract class Vehicle{
public void start();
}
class MotorCycle extends Vehicle{
void start(){
System.out.println("Started Sucessfully");}
public static void main(String args[]){
Vehicle myBike = new MotorCycle();
myBike.run();
}
}
Encapsulation:
The process of wrapping data to prevent the accessing of data which is achieved by using access modifiers.
Access modifiers:
class MotorCycle extends Vehicle{
private int topSpeed;
int mileage;
public void start(){
System.out.println("ta ta ta troom");
}
}
public static void main(String[] args){
Vehicle myBike = new MotorCycle();
myBike.topSpeed = 0; // this is not possible because of private keyword.
myBike.start();
}
mostly we use getters and setters to change/ access the private data.
Static Keyword:
Static keyword is used for memory management mainly. Static keyword can be used with variables, methods, blocks. The static keyword belongs to the class than an instance of the class.
We cannot access a static method from a non static method using polymorphism.
It changes how run time polymorphism works.
Final Keyword:
The Final keyword in java is used to restriction. Final keyword can be used along variable, method, class. The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let's first learn the basics of final keyword.
Conclusion
Object oriented programming is a powerful concept used by programming language to mimic the real life entities. Classes and objects are the fundamental building blocks of OOP in Java. Classes define the properties and methods of an object, while objects are instances of classes that have a state and behavior. We can write code that is easier to understand, test, and maintain over time.