Java Object-Oriented Programming – Mastering OOP Concepts in Java (2025)
Introduction – Why Learn Java OOP?
Java is a pure object-oriented language (except for primitive types) designed to create modular, flexible, and reusable code. OOP in Java provides a structured approach that mimics real-world entities through objects and classes.
In this guide, you’ll explore:
- Core OOP principles: encapsulation, inheritance, polymorphism, abstraction
- Java-specific implementations like interfaces, inner classes, and enums
- Practical usage of constructors, attributes, and modifiers
- Real-world interaction through user input and Java’s date API
Topics Covered
| Topic | Description |
|---|---|
| Java OOP | Core concepts: encapsulation, inheritance, etc. |
| Java Classes/Objects | Creating templates and real-world instances |
| Java Class Attributes | Variables representing object state |
| Java Class Methods | Functions that define object behavior |
| Java Constructors | Special methods to initialize objects |
| Java Modifiers | Access control (public, private, etc.) |
| Java Encapsulation | Restricting access to class internals |
| Java Packages / API | Organizing classes and accessing built-in libraries |
| Java Inheritance | Reusing code across classes |
| Java Polymorphism | Multiple behaviors using the same method |
| Java Inner Classes | Class inside another class |
| Java Abstraction | Hiding implementation details |
| Java Interface | Contract-based design |
| Java Enums | Fixed set of constants |
| Java User Input | Accepting input using Scanner class |
| Java Date | Working with date/time using LocalDate, etc. |
Java OOP – The Pillars
Java is built around these 4 OOP pillars:
| Principle | Meaning |
|---|---|
| Encapsulation | Hide internal details using private variables |
| Inheritance | Share behavior and structure from base classes |
| Polymorphism | Same method, different behavior |
| Abstraction | Focus on “what” over “how” |
Java Classes & Objects
A class is a blueprint; an object is its real-world implementation.
Example:
class Car {
String brand = "Toyota";
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
System.out.println(myCar.brand);
}
}
Line-by-Line:
class Car→ Defines a class with an attribute.Car myCar = new Car();→ Creates an objectmyCar.System.out.println...→ Prints attribute value.
Java Class Attributes
Attributes (or fields) define the state of an object:
public class Person {
String name = "John";
int age = 30;
}
They can be public, private, or protected.
Java Class Methods
Methods define behaviors of objects:
class Dog {
void bark() {
System.out.println("Woof!");
}
}
Called using objectName.methodName().
Java Constructors
A constructor initializes new objects.
class Student {
String name;
Student(String n) {
name = n;
}
}
Used via new Student("Alice");
Java Modifiers
| Modifier | Purpose |
|---|---|
public | Accessible from anywhere |
private | Accessible within class only |
protected | Accessible in package/subclass |
default | Package-level access |
Java Encapsulation
Encapsulation protects internal object state:
class Account {
private int balance = 1000;
public int getBalance() {
return balance;
}
}
Only exposed via getters/setters.
Java Packages & API
Java uses packages to organize code:
import java.util.Scanner;
java.util, java.time, and java.io are common built-in packages.
Java Inheritance
Allows a class to inherit from another:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Meow");
}
}
Java Polymorphism
Same method, different output:
Animal a = new Cat();
a.sound(); // Meow
Runtime polymorphism via method overriding
Java Inner Classes
Defined inside another class:
class Outer {
class Inner {
void show() {
System.out.println("Inside Inner");
}
}
}
Useful for encapsulating logic.
Java Abstraction
Hides internal logic:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
Java Interface
Defines contract behavior:
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Bark");
}
}
Java Enums
Represents fixed constants:
enum Day {
MONDAY, TUESDAY, WEDNESDAY;
}
Use: Day d = Day.MONDAY;
Java User Input
Use Scanner to get input:
import java.util.Scanner;
Scanner input = new Scanner(System.in);
String name = input.nextLine();
Java Date
Java 8+ introduced java.time API:
import java.time.LocalDate;
LocalDate today = LocalDate.now();
System.out.println(today);
Summary – Recap & Next Steps
Java OOP provides a foundation for scalable, reusable code in real-world software.
Key Takeaways:
- Understand OOP pillars: encapsulation, inheritance, polymorphism, abstraction
- Use constructors, methods, modifiers for object behavior
- Utilize packages, interfaces, and enums for organized, robust code
- Accept input & manage dates with built-in API
Next Steps:
- Create your own
Bank,Employee, orLibraryclass with real attributes/methods - Explore JavaFX or Spring to build GUI or backend apps using OOP
Frequently Asked Questions (FAQs)
Can a class inherit from multiple classes in Java?
No. Java supports single inheritance only. Use interfaces for multiple behavior types.
What is the difference between abstract class and interface?
Abstract classes can have concrete methods; interfaces can only have abstract methods (until Java 8 which added default methods).
Can we override constructors?
No. Constructors can’t be inherited, only methods can be overridden.
Why use inner classes?
For logical grouping, accessing outer class members, and encapsulating helper classes.
What’s the benefit of encapsulation?
It secures data, provides maintainability, and improves modularity.
Share Now :
