Java OOP Concepts Tutorial (Complete Beginner Guide)
Introduction
Object-Oriented Programming (OOP) is the foundation of Java. Almost every Java application—whether it's a web application, Android app, banking system, or enterprise software—uses OOP principles.
In the previous module, you learned how to create classes and objects. In this module, you'll learn the four pillars of Object-Oriented Programming along with Interfaces and Abstract Classes.
The four pillars of OOP are:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
These concepts help developers write software that is:
- Easy to maintain
- Reusable
- Secure
- Flexible
- Scalable
By the end of this tutorial, you'll understand how professional Java applications are designed using OOP.
Table of Contents
- What is OOP?
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
- Interfaces
- Abstract Classes
- Interface vs Abstract Class
- Best Practices
- Practice Projects
- Summary
What is Object-Oriented Programming?
Object-Oriented Programming is a programming paradigm that models software using objects.
Each object contains:
- Data (Fields)
- Behavior (Methods)
Java implements OOP through four main principles:
1Object-Oriented Programming 2 3├── Encapsulation 4├── Inheritance 5├── Polymorphism 6└── Abstraction
Encapsulation
What is Encapsulation?
Encapsulation is the process of wrapping data and methods together into a single unit (class) while restricting direct access to the data.
Instead of accessing variables directly, you use getter and setter methods.
Benefits
- Data Security
- Controlled Access
- Better Maintenance
- Easy Validation
Example
1class Student { 2 3 private String name; 4 5 public void setName(String name) { 6 this.name = name; 7 } 8 9 public String getName() { 10 return name; 11 } 12 13}
Using the Class
1public class Main { 2 3 public static void main(String[] args) { 4 5 Student student = new Student(); 6 7 student.setName("Ankit"); 8 9 System.out.println(student.getName()); 10 11 } 12 13}
Output
1Ankit
Inheritance
What is Inheritance?
Inheritance allows one class to acquire the properties and methods of another class.
It promotes code reuse.
Syntax
1class Child extends Parent { 2 3}
Example
1class Animal { 2 3 void eat() { 4 System.out.println("Animal is eating."); 5 } 6 7} 8 9class Dog extends Animal { 10 11 void bark() { 12 System.out.println("Dog is barking."); 13 } 14 15}
Using Inheritance
1public class Main { 2 3 public static void main(String[] args) { 4 5 Dog dog = new Dog(); 6 7 dog.eat(); 8 dog.bark(); 9 10 } 11 12}
Output
1Animal is eating. 2Dog is barking.
Types of Inheritance in Java
| Type | Supported |
|---|---|
| Single | ✅ |
| Multilevel | ✅ |
| Hierarchical | ✅ |
| Multiple (Classes) | ❌ |
| Multiple (Interfaces) | ✅ |
Polymorphism
What is Polymorphism?
Polymorphism means "many forms."
The same method can behave differently depending on the object or parameters.
Java supports two types:
- Compile-time Polymorphism (Method Overloading)
- Runtime Polymorphism (Method Overriding)
Compile-Time Polymorphism
Method overloading occurs when multiple methods have the same name but different parameters.
1class Calculator { 2 3 int add(int a, int b) { 4 return a + b; 5 } 6 7 int add(int a, int b, int c) { 8 return a + b + c; 9 } 10 11}
Runtime Polymorphism
Method overriding occurs when a subclass provides its own implementation of a method.
1class Animal { 2 3 void sound() { 4 System.out.println("Animal Sound"); 5 } 6 7} 8 9class Dog extends Animal { 10 11 @Override 12 void sound() { 13 System.out.println("Bark"); 14 } 15 16}
Using Runtime Polymorphism
1Animal animal = new Dog(); 2 3animal.sound();
Output
1Bark
Abstraction
What is Abstraction?
Abstraction means showing only the essential details while hiding implementation details.
Example:
You drive a car using the steering wheel, accelerator, and brakes. You don't need to know how the engine works internally.
Java provides abstraction using:
- Abstract Classes
- Interfaces
Abstract Class
An abstract class cannot be instantiated directly.
It may contain:
- Abstract methods
- Normal methods
- Variables
- Constructors
Example
1abstract class Animal { 2 3 abstract void sound(); 4 5 void sleep() { 6 System.out.println("Sleeping..."); 7 } 8 9}
Implementing an Abstract Class
1class Dog extends Animal { 2 3 @Override 4 void sound() { 5 System.out.println("Bark"); 6 } 7 8}
Using It
1public class Main { 2 3 public static void main(String[] args) { 4 5 Animal animal = new Dog(); 6 7 animal.sound(); 8 animal.sleep(); 9 10 } 11 12}
Output
1Bark 2Sleeping...
Interfaces
An interface defines a contract that implementing classes must follow.
Interfaces support abstraction and multiple inheritance.
Example
1interface Vehicle { 2 3 void start(); 4 5}
Implementing an Interface
1class Car implements Vehicle { 2 3 @Override 4 public void start() { 5 6 System.out.println("Car Started"); 7 8 } 9 10}
Using the Interface
1public class Main { 2 3 public static void main(String[] args) { 4 5 Vehicle vehicle = new Car(); 6 7 vehicle.start(); 8 9 } 10 11}
Output
1Car Started
Multiple Interfaces
Java allows a class to implement multiple interfaces.
1interface Camera { 2 3 void capture(); 4 5} 6 7interface MusicPlayer { 8 9 void playMusic(); 10 11} 12 13class Smartphone implements Camera, MusicPlayer { 14 15 public void capture() { 16 System.out.println("Photo Captured"); 17 } 18 19 public void playMusic() { 20 System.out.println("Playing Music"); 21 } 22 23}
Interface vs Abstract Class
| Feature | Interface | Abstract Class |
|---|---|---|
| Constructors | ❌ | ✅ |
| Instance Variables | ❌ | ✅ |
| Multiple Inheritance | ✅ | ❌ |
| Abstract Methods | ✅ | ✅ |
| Concrete Methods | ✅ (default/static methods allowed) | ✅ |
| Object Creation | ❌ | ❌ |
OOP Relationship Diagram
1 Animal 2 ▲ 3 │ 4 ┌───────┴────────┐ 5 │ │ 6 Dog Cat 7 │ │ 8 sound() sound() 9 10 Runtime Polymorphism
Best Practices
- Keep fields
privateand expose them through methods when appropriate. - Use inheritance only when there is a true "is-a" relationship.
- Prefer interfaces to define capabilities or contracts.
- Use abstract classes when subclasses share common implementation.
- Favor composition over inheritance when inheritance is not a natural fit.
- Override methods carefully and use the
@Overrideannotation. - Keep classes focused on a single responsibility.
Practice Project 1: Employee Management
1abstract class Employee { 2 3 String name; 4 5 Employee(String name) { 6 this.name = name; 7 } 8 9 abstract double calculateSalary(); 10 11} 12 13class FullTimeEmployee extends Employee { 14 15 private double monthlySalary; 16 17 FullTimeEmployee(String name, double monthlySalary) { 18 super(name); 19 this.monthlySalary = monthlySalary; 20 } 21 22 @Override 23 double calculateSalary() { 24 return monthlySalary; 25 } 26 27} 28 29public class Main { 30 31 public static void main(String[] args) { 32 33 Employee employee = new FullTimeEmployee("Ankit", 50000); 34 35 System.out.println(employee.name); 36 System.out.println("Salary: ₹" + employee.calculateSalary()); 37 38 } 39 40}
Sample Output
1Ankit 2Salary: ₹50000.0
Practice Project 2: Shape Calculator
1abstract class Shape { 2 3 abstract double area(); 4 5} 6 7class Circle extends Shape { 8 9 private double radius; 10 11 Circle(double radius) { 12 this.radius = radius; 13 } 14 15 @Override 16 double area() { 17 return Math.PI * radius * radius; 18 } 19 20} 21 22class Rectangle extends Shape { 23 24 private double length; 25 private double width; 26 27 Rectangle(double length, double width) { 28 this.length = length; 29 this.width = width; 30 } 31 32 @Override 33 double area() { 34 return length * width; 35 } 36 37} 38 39public class Main { 40 41 public static void main(String[] args) { 42 43 Shape circle = new Circle(5); 44 45 Shape rectangle = new Rectangle(4, 6); 46 47 System.out.println(circle.area()); 48 System.out.println(rectangle.area()); 49 50 } 51 52}
Sample Output
178.53981633974483 224.0
Practice Project 3: Animal System
1interface Animal { 2 3 void sound(); 4 5} 6 7class Dog implements Animal { 8 9 public void sound() { 10 System.out.println("Bark"); 11 } 12 13} 14 15class Cat implements Animal { 16 17 public void sound() { 18 System.out.println("Meow"); 19 } 20 21} 22 23public class Main { 24 25 public static void main(String[] args) { 26 27 Animal dog = new Dog(); 28 Animal cat = new Cat(); 29 30 dog.sound(); 31 cat.sound(); 32 33 } 34 35}
Sample Output
1Bark 2Meow
Summary
In this tutorial, you learned:
- The purpose of Object-Oriented Programming in Java
- How encapsulation protects data using private fields and access methods
- How inheritance enables code reuse through parent-child relationships
- The difference between compile-time and runtime polymorphism
- How abstraction hides implementation details using abstract classes and interfaces
- How interfaces define contracts for implementing classes
- The differences between interfaces and abstract classes
- Best practices for designing flexible, maintainable, and reusable object-oriented applications
These concepts are the foundation of professional Java development and are widely used in frameworks such as Spring Boot, Jakarta EE, Android, and JavaFX.
Practice Exercises
Exercise 1: Banking System
Create an abstract Account class with deposit() and withdraw() methods. Implement SavingsAccount and CurrentAccount.
Exercise 2: Vehicle Rental
Create a Vehicle interface with methods such as start(), stop(), and calculateRent(). Implement Car, Bike, and Truck.
Exercise 3: Online Payment
Create a Payment interface with a pay(double amount) method. Implement:
- CreditCardPayment
- UpiPayment
- NetBankingPayment
Use runtime polymorphism to process different payment methods.
Exercise 4: Employee Hierarchy
Create an Employee base class and extend it with:
- Manager
- Developer
- Intern
Override a method that calculates salary or bonus for each type.
Exercise 5: Library Management System
Design a library application using OOP principles:
- Encapsulate book details using private fields.
- Use inheritance for different book types (e.g.,
PrintedBook,EBook). - Define a
Borrowableinterface. - Use polymorphism to handle different borrowing behaviors.
Completing these exercises will prepare you for the next phase on Exception Handling, File I/O, and Collections, where you'll build more robust and production-ready Java applications.