Object-Oriented Programming in java

Abhinav Kumar gupta
5 min readNov 8, 2020

Object-oriented programming is a programming paradigm or methodology. it is an approach to solve a problem using the programming language. The main features of oop’s to provides code Reusability and Securities to our program.

The six pillars of oop’s are

Class

Class is just a blueprint or prototype or template of an object. it does not occupy memory. it is a logical view to create an object. i.e Car is a class without its object Mercedes, BMW, Audi, etc it does not exist.

Object

Object is a real-time entity that has some identity, state, and behavior. object occupies memory. object is an instance of the class. i.e object has a name for its identity and it has some state like color, age, etc, and has some behavior like eat, run, sleep, etc. we can create an object using the new keyword.

class car
{
String color="red";
void show()
{
System.out.println("Audi car colour is red");
}
}
class Main
{
public static void main(String[] args)
{
car Audi =new car();
Audi.show();
}
}

Inheritance

inheritance in java is a mechanism in which one object acquires all the properties and behaviors of a parent object. it represents the IS-A relationship which is also known as a parent-child relationship. i.e programmer IS-A Employee, Surgeon IS-A Doctor. the advantages of inheritance that it provides code Reusability and it promotes runtime polymorphism by allowing method overriding. inheritance is achieved by using the “extends” keyword. java support only single inheritance, multilevel inheritance, and hierarchical inheritance.

class A //single inheritance
{

void showA()
{
System.out.println("class A method is call");
}
}
class B extends A
{
void showB()
{
System.out.println("class B method is call");
}
}
class Main
{
public static void main(String[] args)
{
B obj =new B();
obj.showA();
obj.showB();
}
}
class A //multilevel inheritance
{

void showA()
{
System.out.println("class A method is call");
}
}
class B extends A
{
void showB()
{
System.out.println("class B method is call");
}
}
class c extends B
{
void showC()
{
System.out.println("class C method is call");
}
}
class Main
{
public static void main(String[] args)
{
c obj =new c();
obj.showA();
obj.showB();
obj.showC();
}
}
class A//hierarchical inheritance
{

void showA()
{
System.out.println("class A method is call");
}
}
class B extends A
{
void showB()
{
System.out.println("class B method is call");
}
}
class c extends A
{
void showC()
{
System.out.println("class C method is call");
}
}
class Main
{
public static void main(String[] args)
{
B obj1 =new B();
obj1.showA();
obj1.showB();

c obj2 =new c();
obj2.showA();
obj2.showC();
}
}

Encapsulation

Encapsulation is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. incapsulation is achieved by declared the variables of a class as private and provide a public getter and setter method to modify and view the value of the variable. in encapsulation, the variables of a class will be hidden from other classes and can be accessed only through the methods of their current class. this concept is known as data hiding.

class Employee
{
private int empid;
public void setEmpid(int eid)
{
empid=eid;
}
public int getEmpid()
{
return empid;
}
}
class Company
{
public static void main(String[] args)
{
Employee e=new Employee();
e.setEmpid(101);
System.out.println(e.getEmpid());
}
}

Abstraction

Abstraction refers to be hiding the internal implementation and just highlight the essential feature. thus showing relevant data to the user and hiding implementation or details from the user is Abstraction i.e. in the real world as in the car case-relevant parts like steering, gear, horn, accelerator, etc are shown to drivers because they are necessary to the driving. but drivers need not know the internal functioning of the engine, gear, etc. abstraction is achieved by making the abstract class and the second way is by implemented interfaces.

abstract class Vehicle //abstraction using abstract class and method
{
abstract void start();
}
class Car extends Vehicle
{
void start()
{
System.out.println("car starts with key");
}
}
class Scooter extends Vechicle
{
void start()
{
System.out.prinbtln("scooter start with kick");
}
public static void main(String[] args)
{
car c=new car();
c.start();

Scooter s=new Scooter();
s.start();
}
}
interface I1 // abstraction using interface
{
void show();
}
class Test implements I1
{
public void show()
{
System.out.prinbtln("1");
}
public static void main(String[] args)
{
Test t =new Test();
t.show();
}
}

polymorphism

polymorphism The word “polymorphism” means more than one form. the ability to perform one action in different ways. i.e. in real-world water has more than one form like solid, liquid, gas. in the oop’s concept, the ability of an object to take more than one form is known as polymorphism. in java polymorphism is achieved in two different ways. the first way is in the same class if multiple methods have the same name but the number of parameters or sequences of parameter or type of parameter passes to it is different than this type of polymorphism is known as compile-time polymorphism or method overloading. and the second way is within different classes there are multiple methods with the same name and the number of parameters or sequences of parameter or type of parameter passes to it is the same and also follows the inheritance concept then this type of polymorphism is known as run time polymorphism or method overriding.

class Test \\method overloading
{
void show()
{
System.out.println("1");
}
void show(int a)
{
System.out.println("2");
}
public static void main(String[] args)
{
Test t =new Test();
t.show();
t.show(10);
}
}
class Test//method overriding
{
void show(String a, int b)
{
System.out.println("1");
}
}
class Xyz extends Test
{
void show(String a, int b)
{
System.out.println("2");
}
public static void main(String[] args)
{
Test t=new Test();
t.show("test",10);

Xyz x =new Xyz();
x.show("test1",20);
}
}

--

--