JAVA Tutorial
Estimated reading: 4 minutes 282 views

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 OOPCore concepts: encapsulation, inheritance, etc.
Java Classes/ObjectsCreating templates and real-world instances
Java Class AttributesVariables representing object state
Java Class MethodsFunctions that define object behavior
Java ConstructorsSpecial methods to initialize objects
Java ModifiersAccess control (public, private, etc.)
Java EncapsulationRestricting access to class internals
Java Packages / APIOrganizing classes and accessing built-in libraries
Java InheritanceReusing code across classes
Java PolymorphismMultiple behaviors using the same method
Java Inner ClassesClass inside another class
Java AbstractionHiding implementation details
Java InterfaceContract-based design
Java EnumsFixed set of constants
Java User InputAccepting input using Scanner class
Java DateWorking with date/time using LocalDate, etc.

Java OOP – The Pillars

Java is built around these 4 OOP pillars:

Principle Meaning
EncapsulationHide internal details using private variables
InheritanceShare behavior and structure from base classes
PolymorphismSame method, different behavior
AbstractionFocus 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 object myCar.
  • 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
publicAccessible from anywhere
privateAccessible within class only
protectedAccessible in package/subclass
defaultPackage-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, or Library 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 :
Share

🧱 Java Object-Oriented Programming

Or Copy Link

CONTENTS
Scroll to Top