Introduction
Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize code and data. OOP is a fundamental concept in Java and is used to model real-world entities and relationships, making code more modular, reusable, and maintainable. In this chapter, we will explore the core principles of OOP and how they are implemented in Java.
Table of Contents
- What is Object-Oriented Programming?
- Principles of OOP
- Classes and Objects
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
- Conclusion
1. What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which can contain data in the form of fields (also known as attributes or properties) and code in the form of methods (also known as functions or procedures). OOP aims to implement real-world entities like inheritance, polymorphism, encapsulation, and abstraction in programming.
2. Principles of OOP
There are four main principles of Object-Oriented Programming:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Encapsulation
Encapsulation is the concept of bundling data (variables) and methods (functions) that operate on the data into a single unit called a class. It restricts direct access to some of an object’s components, which can prevent the accidental modification of data.
Inheritance
Inheritance is the mechanism by which one class (the child or subclass) can inherit fields and methods from another class (the parent or superclass). This allows for code reusability and the creation of a hierarchical relationship between classes.
Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon, even though they share the same name. There are two types of polymorphism: compile-time (method overloading) and runtime (method overriding).
Abstraction
Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object. It helps in reducing programming complexity and effort.
3. Classes and Objects
Class
A class is a blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into one single unit.
Example: Defining a Class
public class Car {
// Fields (attributes)
String color;
String model;
// Constructor
public Car(String color, String model) {
this.color = color;
this.model = model;
}
// Method
public void displayDetails() {
System.out.println("Car model: " + model + ", Color: " + color);
}
}
Object
An object is an instance of a class. It is created using the new
keyword.
Example: Creating an Object
public class Main {
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car("Red", "Toyota");
// Calling a method on the object
myCar.displayDetails();
}
}
Output:
Car model: Toyota, Color: Red
4. Encapsulation
Encapsulation restricts direct access to some components of an object, which is a means of preventing unintended interference and misuse of the data.
Example: Encapsulation
public class Person {
// Private fields
private String name;
private int age;
// Public getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 0) {
this.age = age;
} else {
System.out.println("Age cannot be negative or zero.");
}
}
}
Example: Using Encapsulation
public class Main {
public static void main(String[] args) {
// Creating an object of the Person class
Person person = new Person();
// Setting values using setter methods
person.setName("John");
person.setAge(30);
// Getting values using getter methods
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
}
}
Output:
Name: John
Age: 30
5. Inheritance
Inheritance is a mechanism where a new class inherits the properties and behavior of an existing class.
Example: Inheritance
// Parent class
public class Animal {
public void eat() {
System.out.println("This animal eats food.");
}
}
// Child class
public class Dog extends Animal {
public void bark() {
System.out.println("The dog barks.");
}
}
Example: Using Inheritance
public class Main {
public static void main(String[] args) {
// Creating an object of the Dog class
Dog myDog = new Dog();
// Calling methods from the parent and child classes
myDog.eat(); // Method inherited from Animal class
myDog.bark(); // Method of Dog class
}
}
Output:
This animal eats food.
The dog barks.
6. Polymorphism
Polymorphism allows methods to perform different tasks based on the object they are acting upon.
Example: Method Overloading (Compile-time Polymorphism)
public class Calculator {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}
// Method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
}
Example: Using Method Overloading
public class Main {
public static void main(String[] args) {
// Creating an object of the Calculator class
Calculator calc = new Calculator();
// Calling the overloaded methods
System.out.println("Sum of 2 and 3: " + calc.add(2, 3));
System.out.println("Sum of 1, 2 and 3: " + calc.add(1, 2, 3));
}
}
Output:
Sum of 2 and 3: 5
Sum of 1, 2 and 3: 6
Example: Method Overriding (Runtime Polymorphism)
// Parent class
public class Animal {
public void sound() {
System.out.println("This animal makes a sound.");
}
}
// Child class
public class Cat extends Animal {
@Override
public void sound() {
System.out.println("The cat meows.");
}
}
Example: Using Method Overriding
public class Main {
public static void main(String[] args) {
// Creating an object of the Cat class
Animal myCat = new Cat();
// Calling the overridden method
myCat.sound();
}
}
Output:
The cat meows.
7. Abstraction
Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object.
Example: Abstract Class
// Abstract class
public abstract class Shape {
// Abstract method (does not have a body)
public abstract void draw();
}
// Concrete class
public class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
}
Example: Using Abstract Class
public class Main {
public static void main(String[] args) {
// Creating an object of the Circle class
Shape myShape = new Circle();
// Calling the abstract method
myShape.draw();
}
}
Output:
Drawing a circle.
Conclusion
Object-Oriented Programming (OOP) in Java provides a robust framework for building modular, reusable, and maintainable code. By understanding and applying the core principles of OOP?encapsulation, inheritance, polymorphism, and abstraction?you can create complex and efficient Java applications. In this chapter, we explored the fundamental concepts of OOP and demonstrated how they are implemented in Java through various examples.