

Object-Oriented Programming (OOP) is a programming paradigm centered around the concept of objects. In OOP, objects are instances of classes, which can encapsulate data and behavior. Java is a widely-used programming language that supports OOP principles, making it easier to design, develop, and maintain software.
OOP in Java is based on four main concepts:
Object-Oriented Programming in Java leverages encapsulation, inheritance, polymorphism, and abstraction to create a robust and maintainable codebase. Each of these concepts plays a vital role in building scalable and efficient Java applications. By understanding and applying these principles, developers can effectively model real-world entities and their interactions in their software.
Let's dive into each of these concepts in detail, including their introduction, core ideas, and hidden details.
Encapsulation is the mechanism of wrapping the data (variables) and the code (methods) that operates on the data into a single unit, known as a class. It restricts direct access to some of the object's components, which can prevent the accidental modification of data.
public class Person {
private String name;
private int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 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;
}
}
}
Inheritance is the mechanism by which one class (subclass/child class) inherits the fields and methods of another class (superclass/parent class). It promotes code reuse and establishes a relationship between classes.
extends
keyword is used to define a subclass.super()
keyword is used to explicitly call a superclass constructor.xxxxxxxxxx
public class Animal {
public void eat() {
System.out.println("This animal eats food.");
}
}
public class Dog extends Animal {
public void bark() {
System.out.println("The dog barks.");
}
}
public class TestInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark(); // Subclass-specific method
}
}
Polymorphism means "many shapes" and allows one interface to be used for a general class of actions. It enables objects to be treated as instances of their parent class rather than their actual class.
xxxxxxxxxx
public class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
public class Cat extends Animal {
public void sound() {
System.out.println("Cat meows");
}
}
public class TestPolymorphism {
public static void main(String[] args) {
Animal myAnimal = new Cat(); // Upcasting
myAnimal.sound(); // Calls the overridden method in Cat class
}
}
Abstraction is the concept of hiding the complex implementation details and showing only the essential features of the object. It helps in reducing programming complexity and effort.
xxxxxxxxxx
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}
interface Drawable {
void draw();
}
class Square implements Drawable {
public void draw() {
System.out.println("Drawing a square");
}
}
public class TestAbstraction {
public static void main(String[] args) {
Shape circle = new Circle();
circle.draw();
Drawable square = new Square();
square.draw();
}
}