OOPS Principles
Java is a widely-used programming language that follows the Object-Oriented Programming (OOP) paradigm. OOP is a programming model that organizes software design around objects rather than functions or logic. This approach makes Java more modular, reusable, and easier to maintain.
There are four principle of Object Oriented Programming:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
1. Encapsulation
Encapsulation is the process of wrapping data (variables) and methods (functions) into a single unit, typically a class. It restricts direct access to the internal state of an object and allows controlled access through methods, often referred to as getters and setters. This ensures data security and integrity.
Example:
public class Person {
private String name; // Private variable
// Getter method
public String getName() {
return name;
}
// Setter method
public void setName(String name) {
this.name = name;
}
}
Here, the name variable is private, meaning it cannot be accessed directly from outside the class. Instead, the getName and setName methods provide controlled access.
2. Inheritance
Inheritance allows a class (child class) to inherit properties and methods from another class (parent class). This promotes code reuse and establishes a hierarchical relationship between classes. In Java, inheritance is implemented using the extends keyword.
For example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark(); // Method from Dog class
}
}
In this example, the Dog class inherits the eat method from the Animal class, while also having its own bark method.
3. Polymorphism
Polymorphism allows objects to take on multiple forms. In Java, it is achieved through method overloading (compile-time polymorphism) and method overriding (runtime polymorphism). This makes the code more flexible and dynamic.
Method Overloading: Multiple methods in the same class with the same name but different parameters.
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
Method Overriding: A subclass provides a specific implementation of a method already defined in its parent class.
class Animal {
void sound() {
System.out.println("This animal makes a sound."); }
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("The cat meows.");
}
}
public class Main {
public static void main(String[] args) { Animal animal=new Cat();
animal.sound(); // Calls the overridden method in Cat }
}
4. Abstraction
Abstraction focuses on hiding implementation details and showing only the essential features of an object. In Java, abstraction is achieved using abstract classes and interfaces.
Abstract Class: A class that cannot be instantiated and may contain abstract methods (methods without a body).
Example:
abstract class Shape {
abstract void draw(); // Abstract method
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle.");
}
}
Interface: A blueprint for a class that contains abstract methods. A class implements an interface using the implements keyword.
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound()
{
System.out.println("The dog barks.");
}
}
