Object-Oriented Programming(oops)

Object-Oriented Programming (OOP) is a programming paradigm that emphasizes the use of objects and classes to model real-world concepts and behaviours. OOP provides several key features that make it a popular choice for software development, including abstraction, encapsulation, polymorphism, and inheritance. These features allow developers to build software that is more flexible, maintainable, and extensible.

Class

In object-oriented programming, a class is a template or blueprint that defines the attributes (data) and behaviors (methods) of a category of objects. An object is an instance of a class that has its own state and behavior, created from the blueprint specified by the class.

For example :you can define a class called "Person" that has attributes like name, age, and gender, and behaviors like speak and walk. Then, you can create multiple objects of the "Person" class, each with their own unique name, age, and gender, and can perform the same behaviors as defined by the class.

Object

A running object-oriented software system consists of objects. Each object has structure and behavior.

There are four pillars of Object-oriented Programming:

  • Abstraction

  • Encapsulation

  • Inheritance

  • Polymorphism

Abstraction :

Abstraction in Java is a process of hiding the implementation details of a complex system, and instead exposing only the necessary details and behaviors to its users.

In Java, abstraction is achieved through interfaces and abstract classes. Abstract classes are classes that cannot be instantiated but can contain both abstract and concrete methods. Abstract methods have no implementation and must be overridden by concrete subclasses that implement the abstract methods. Interfaces are similar to abstract classes but can only contain abstract methods, and any class that implements the interface must provide an implementation for all of its methods.

Example :

interface Super{

public void walk();

}

class Sub implements Super{

public void walk(){

System.out.println("Walk slow")

}

}

Class Main{

Public static void main(String [] args)

{

Sub obj = new Super;

obj.walk();

}

}

Encapsulation :

Encapsulation is the process by which data(variables) and the code that acts upon them (methods) are integrated as a single unit. Encapsulation allows for the creation of objects that have well-defined boundaries and behavior. It is accomplished through the use of access modifiers, such as public, private, and protected, which control the visibility and accessibility of a class's properties and methods.

Example :

public class Encap{

private String name;

public String getName(){

return name;

}

public void setName(String name){

this.name=name;

}

}

Inheritance :

Inheritance in Java is a mechanism that allows a new class (subclass) to be based on an existing class (superclass), inheriting its fields and methods. The subclass can then extend or modify the inherited behavior or add its own behavior.

The class that is being extended or inherited from is called the superclass or base class, while the class that is extending or inheriting is called the subclass or derived class. In Java, inheritance is achieved using the keyword "extends" when declaring a class.

Example :

class Super{

int number =10;

}

class Sub extends Super{

public static void printNumber()

{

System.out.Println(number);

}

}

Class Main

{

public static void main(String [] args){

printNumber();

}

}

Polymorphism :

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. Polymorphism is two types, compile-time Polymorphism(method overloading) and runtime Polymorphism(method overriding). Compile-time polymorphism refers to the ability to have multiple methods with the same name but different parameters in a single class. Runtime polymorphism refers to the ability to have a method in a subclass with the same name as a method in the superclass, but with different functionality.

Example :

Method overriding :

class Overriding{

public void eat(){

System.out.println("He is eating");

}

}

class Sub extends Overriding{

public void eat(){

System.out.println("she is eating");

}

}

class main{

public static void main(String[] args){

Sub obj = new Sub();

obj.eat();

}

}

conclusion :

Abstraction, Encapsulation, Polymorphism, and Inheritance are fundamental principles of OOP, and are essential for building high-quality, flexible, and maintainable software. By leveraging these features, developers can create code that is more robust, adaptable, and efficient, reducing the risk of errors and improving overall productivity.