Object Oriented Programming is a programming concept that works on the principle that objects are the most important part of your program.
The primary purpose of Object-oriented programming is to increase the flexibility and maintainability of programs.
Object-oriented programming (OOP) is nothing but that which allows the writing of programs with the help of certain classes and real-time objects.
The main aim of object-oriented programming is to implement real-world entities, for example, object, classes, abstraction, inheritance, polymorphism, etc.
1. Object
An Object is a bundle of data and its behaviour(often known as methods). Also it is an instance of a class.
An Object also means a real world entity such as a notebook/pen/pencil etc.
Example : A cat is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.
2. Class
A class is a group of similar Objects. It is a considered a blueprint using which you can create as many Objects as you like. An example could be having a class of Vehicles, and there can be Objects like 4 wheelers, 2 wheelers, etc and they will further have some properties like engine, make, model, year of manufacture, etc.
3. Constructor
A constructor is a block of statement which has same name as that of class and is used to initialize the class member properties.
Example :
public class OopsExample {
int age;
String name;
//This is a Default constructor
OopsExample(){
this.name="John";
this.age=35;
}
//And this one is Parameterized constructor
OopsExample(String n,int a){
this.name=n;
this.age=a;
}
public static void main(String args[]){
OopsExample obj1 = new OopsExample();
OopsExample obj2 = new OopsExample("Jason", 40);
System.out.println(obj1.name+" "+obj1.age);
System.out.println(obj2.name+" "+obj2.age);
}
}
OUTPUT
John 35
Jason 40
OOPs features : 4 of them with Examples
1. Encapsulation :
Wrapping up of data into a single unit is called Encapsulation. In terms of Java, if your are creating a Class with data members(variables) and behaviors(methods) then it means you are following encapsulation.
2. Abstraction :
Abstraction is a process when only the relevant data or information is shown to the user and other not necessary information is hidden from it.For Example, A car, In a car a driver/passenger knows only the relevant information which is required to drive the car, but how the engine is burning fuel inside, what and how it happens when you press the accelerator , what happens when you change the gear, a user doesn’t know.
He just have superficial knowledge or the use of certain equipment inside the car but not the inner functionality as a whole.
3. Polymorphism :
Polymorphism means having more than one form, In Object oriented programming language, Polymorphism refers to the ability of a variable, object or function to take on multiple forms.
There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.
I. Static Polymorphism or compile time polymorphism : Achieved by method overloading, which means methods having the same name, same return type but different signature(number of parameters).
class MethodOverloading
{
public void display(char ch)
{
System.out.println(ch);
}
public void display(char ch, int num)
{
System.out.println(ch +“ - "+num);
}
}
public class Test
{
public static void main(String args[])
{
MethodOverloading obj = new MethodOverloading();
obj.display('john');
obj.display('john',40);
}
}
OUTPUT
john
john - 40
II. Dynamic Polymorphism or run time polymorphism : In dynamic polymorphism a call to overridden method is resolved at run time and that is why referred as run-time polymorphism.
class Animal{
public void breed(){
System.out.println("Default breed");
}
}
public class Dog extends Animal{
public void breed(){
System.out.println("German Shepherd");
}
public static void main(String args[]){
Animal obj = new Dog();
obj.breed();
}
}
OUTPUT
German Shepherd
4. Inheritance :It is one of the code saving techniques, which helps in reducing the redundant code. It works by letting child class adapt properties of parent class, where the original class is called the parent class whereas the new one adapting the parents features is called the child class.
we use the keyword “extends” in classes and “implements” in interfaces.
Summary
In this article we learnt about Object Oriented Programming in Java and its 4 major concepts. We also learnt about Object, Class and its Constructor.
Hope you liked the article !