๐งฑ 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
, orLibrary
class 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 :