View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
Java

Step by Step Java Tutorial Con…

  • 191 Lessons
  • 32 Hours

Principles of OOP (Object-Oriented Programming)

Updated on 29/01/20254,451 Views

Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of objects and classes. It is designed to model real-world entities and relationships using objects. OOP is highly beneficial for creating modular, reusable, and maintainable code, and the core principles that govern it are Encapsulation, Inheritance, Polymorphism, and Abstraction. Understanding these principles is crucial for grasping the fundamentals of OOP.

Let’s explore each principle in depth.

1. Encapsulation

Encapsulation is the process of bundling data (variables) and the methods (functions) that operate on that data into a single unit, typically a class. The goal of encapsulation is to protect the internal state of an object from outside interference and misuse, allowing changes to be made only through well-defined interfaces.

Encapsulation ensures that an object's internal workings are hidden, and only the necessary information is exposed through public methods. This concept is vital for creating robust and secure code since it restricts direct access to some of the object’s components, providing controlled interaction.

#Example:

```java
public class Car {
private String model;
private int speed;
public Car(String model, int speed) {
this.model = model;
this.speed = speed;
}
// Getter and setter methods to control access
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
if (speed >= 0) {
this.speed = speed;
}
}
}
```

In this example, the `Car` class encapsulates the `model` and `speed` fields, controlling access through getter and setter methods. This prevents external code from modifying the car's speed or model in an unintended way.

2. Inheritance

Inheritance is the mechanism by which one class can acquire the properties (fields) and behaviors (methods) of another class. It allows for code reuse and the extension of existing classes. The class that inherits from another is called the subclass or child class, and the class from which it inherits is known as the superclass or parent class.

This principle helps in reducing redundancy, making the code more manageable and scalable. Inheritance also facilitates the hierarchical classification of classes and enables the subclass to modify or enhance the inherited methods.

#Example:

```java
public class Vehicle {
protected int wheels;
protected String fuelType;
public void move() {
System.out.println("The vehicle is moving");
}
}
public class Car extends Vehicle {
private String model;
public Car(String model) {
this.model = model;
this.wheels = 4; // Inherited property
this.fuelType = "Gasoline"; // Inherited property
}
// Overriding the move method
@Override
public void move() {
System.out.println("The car is moving");
}
}
```

In this example, `Car` inherits from the `Vehicle` class and gains access to its fields (`wheels` and `fuelType`) and methods. It can also override the `move` method to provide more specific functionality.

3. Polymorphism

Polymorphism, meaning “many shapes,” allows objects of different classes to be treated as objects of a common superclass. There are two types of polymorphism: compile-time (method overloading) and runtime (method overriding). This principle allows for flexibility and the ability to define one interface and have multiple implementations.

- Method Overloading: Multiple methods with the same name but different parameters in the same class.

- Method Overriding: A subclass provides a specific implementation of a method that is already defined in its superclass.

Polymorphism is key to making systems more modular, extensible, and easier to maintain.

#Example (Method Overriding):

Java
public class Animal {
public void sound() {
System.out.println("This is a generic animal sound");
}
}
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("The dog barks");
}
}
public class Cat extends Animal {
@Override
public void sound() {
System.out.println("The cat meows");
}
}

In this example, both `Dog` and `Cat` classes override the `sound` method of the `Animal` class, providing their own specific implementation. Polymorphism allows these objects to be treated as instances of `Animal`, despite behaving differently.

4. Abstraction

Abstraction focuses on hiding the complexity of a system by exposing only the necessary parts of an object, making it simpler for users to interact with it. It allows programmers to focus on *what* an object does rather than *how* it does it.

In OOP, abstraction is achieved using abstract classes and interfaces. An abstract class cannot be instantiated on its own and may contain abstract methods (methods without a body), while an interface defines a contract for what a class should implement without dictating how the methods are executed.

Example:

```java
public abstract class Shape {
public abstract double calculateArea();
}
public class Circle extends Shape {
private double radius;

public Circle(double radius) {
this.radius = radius;
}

@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}

In this example, `Shape` is an abstract class that defines an abstract method `calculateArea`. The `Circle` class provides a concrete implementation of the method. This abstraction allows other developers to use `Shape` without needing to know the internal details of how the area is calculated.

Benefits of OOP

By following these principles, OOP provides several benefits:

- Code Reusability: Through inheritance, classes can reuse code from parent classes, reducing redundancy.

- Scalability: Polymorphism and abstraction allow the system to grow in complexity while maintaining manageable code.

- Maintainability: Encapsulation makes code easier to debug and maintain by keeping data and functions closely tied.

- Security: Data hiding through encapsulation provides a layer of protection from external access.

Conclusion

The principles of Object-Oriented Programming—encapsulation, inheritance, polymorphism, and abstraction—form the foundation of OOP and are essential for designing efficient, maintainable, and scalable software. Understanding and applying these concepts allows developers to write cleaner, more modular code that reflects real-world entities and relationships. Whether you're a beginner or an experienced developer, mastering these principles will enhance your ability to build robust software systems.

image

Take the Free Quiz on Java

Answer quick questions and assess your Java knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Explore Our Free Software Tutorials

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.