DEV Community

Rifat Khan
Rifat Khan

Posted on

Object-Oriented Programming in Java: An Academic Perspective

This article presents a comprehensive overview of the principal concepts of Object-Oriented Programming (OOP) in Java. The treatment is designed for academic study, combining theoretical foundations with practical examples to provide a well-rounded understanding. Each section introduces a concept, outlines its syntax, highlights its significance, and offers illustrative code samples.


Table of Contents

  1. Introduction to OOP in Java
  2. OOP Components & Related Topics
  3. Constructor and Method Overloading
  4. Method Overriding
  5. Inheritance
  6. Access Modifiers
  7. Returning Objects from Methods
  8. The super Keyword
  9. The this Keyword
  10. The static Keyword
  11. The final Keyword
  12. Dynamic Method Dispatch
  13. Abstract Classes
  14. Interfaces
  15. Summary of Keywords
  16. Best Practices
  17. Exercises for Students
  18. Conclusion

Introduction to OOP in Java

Java was designed with Object-Oriented Programming (OOP) principles at its core. OOP allows developers to model real-world systems through objects, which encapsulate both state (attributes) and behavior (methods). The four fundamental principles of OOP are:

  • Encapsulation: Grouping data and related behavior while restricting external access.
  • Abstraction: Exposing essential features while concealing implementation details.
  • Inheritance: Allowing a new class to acquire the properties and behavior of an existing class.
  • Polymorphism: Supporting multiple forms of behavior through method overloading and overriding.

OOP Components and Related Topics

1️⃣ Inheritance

Inheritance is about reusing code from parent classes in child classes.

  • Inheritance ✅ (obviously)
  • The super keyword ✅ (used to access parent class properties/methods)

2️⃣ Polymorphism

Polymorphism is about one interface, many forms, i.e., the same method behaving differently.

  • Method Overriding ✅ (runtime polymorphism)
  • Dynamic Method Dispatch ✅ (runtime method selection, core of polymorphism)
  • Constructors & Method Overloading ✅ (compile-time polymorphism, also called static polymorphism)

3️⃣ Encapsulation

Encapsulation is about hiding internal data and providing controlled access.

  • The this keyword ✅ (helps in distinguishing instance variables, indirectly supports encapsulation)
  • Access Modifiers ✅ (access modifiers like private/public, which are classic examples of encapsulation)
  • The static keyword ❌ (not directly part of encapsulation, more about class-level variables)
  • The final keyword ❌ (more about immutability)

4️⃣ Abstraction

Abstraction is about hiding implementation details and showing only functionality.

  • Abstract Classes & Interfaces ✅ (core of abstraction)
  • Returning Objects from Methods ✅ (can be used to abstract internal implementation by returning objects instead of exposing fields)

✅ Summary Table

OOP Component Topics Covered
Inheritance Inheritance, super
Polymorphism Method Overriding, Dynamic Method Dispatch, Constructors & Method Overloading
Encapsulation Access Modifiers, this (indirectly)
Abstraction Abstract Classes & Interfaces, Returning Objects from Methods

Constructor and Method Overloading

Overloading refers to defining multiple constructors or methods with the same name but different parameter lists. Overloading is resolved at compile time.

Syntax Example

class Student {
    String name;
    int age;

    // Constructor overloading
    Student() { this("Unknown", 0); }
    Student(String name) { this(name, 18); }
    Student(String name, int age) { this.name = name; this.age = age; }

    // Method overloading
    void showInfo() { System.out.println(name + " - " + age); }
    void showInfo(String prefix) { System.out.println(prefix + ": " + name + " - " + age); }
}
Enter fullscreen mode Exit fullscreen mode

Academic Note

Overloading is an example of compile-time polymorphism. It provides flexibility by allowing different parameterizations of methods or constructors.


Method Overriding

Overriding occurs when a subclass provides its own implementation for a method already defined in the superclass. Resolution occurs at runtime.

class Animal {
    void sound() { System.out.println("Generic animal sound"); }
}
class Dog extends Animal {
    @Override
    void sound() { System.out.println("Dog barks"); }
}
Enter fullscreen mode Exit fullscreen mode

Academic Note

Overriding exemplifies runtime polymorphism, which enables dynamic behavior adjustment.


Inheritance

Inheritance allows classes to reuse fields and methods from other classes using the extends keyword.

class Person {
    String name;
    void greet() { System.out.println("Hello, I am " + name); }
}
class Employee extends Person {
    int salary;
}
Enter fullscreen mode Exit fullscreen mode

Academic Note

Inheritance should represent a true “is-a” relationship. Excessive inheritance hierarchies are discouraged due to maintenance challenges.


Access Modifiers

Access specifiers in Java control the visibility of classes, methods, and variables. There are four types:

Specifier Scope / Visibility Syntax Example
private Same class only private int age;
default (no modifier) Same package only int age;
protected Same package + subclasses protected int age;
public Everywhere (any class) public int age;

Concepts

  1. Private: For encapsulation; restricts access to within the class.
  2. Default: For package-level access; no keyword needed.
  3. Protected: For inheritance; accessible in subclasses and same package.
  4. Public: For global access; accessible from anywhere.

Syntax Examples

class Example {
    private int privateVar;       // accessible only in Example class
    int defaultVar;               // accessible in the same package
    protected int protectedVar;   // accessible in package & subclasses
    public int publicVar;         // accessible everywhere

    public void display() {
        System.out.println(privateVar + " " + defaultVar + " " + protectedVar + " " + publicVar);
    }
}
Enter fullscreen mode Exit fullscreen mode

Returning Objects from Methods

Java methods can return objects, allowing construction of new instances within methods.

class Box {
    int length;
    Box(int length) { this.length = length; }
    Box duplicate() { return new Box(this.length); }
}
Enter fullscreen mode Exit fullscreen mode

The super Keyword

super refers to the immediate parent class. It is used to:

  1. Invoke parent constructors.
  2. Access parent methods and variables.
class Parent {
    int num = 100;
    void show() { System.out.println("Parent method"); }
}
class Child extends Parent {
    @Override
    void show() {
        super.show();
        System.out.println("Child method, Parent num = " + super.num);
    }
}
Enter fullscreen mode Exit fullscreen mode

The this Keyword

this refers to the current object instance.

class Example {
    int x;
    Example(int x) { this.x = x; }
    void display() { System.out.println("Value: " + this.x); }
}
Enter fullscreen mode Exit fullscreen mode

The static Keyword

Static members belong to the class rather than an instance.

class Counter {
    static int count = 0;
    Counter() { count++; }
}
Enter fullscreen mode Exit fullscreen mode

Academic Note

Static methods and variables should be used judiciously, as they compromise object-oriented design by introducing global state.


The final Keyword

The final keyword prevents modification or extension.

  • Final variables: cannot be reassigned.
  • Final methods: cannot be overridden.
  • Final classes: cannot be inherited.
final class Vehicle {
    final int speedLimit = 100;
    final void displayLimit() { System.out.println("Speed limit: " + speedLimit); }
}
Enter fullscreen mode Exit fullscreen mode

Dynamic Method Dispatch

Dynamic method dispatch resolves overridden methods at runtime.

class A { void show() { System.out.println("A.show"); } }
class B extends A { @Override void show() { System.out.println("B.show"); } }

A ref = new B();
ref.show(); // B.show
Enter fullscreen mode Exit fullscreen mode

Abstract Classes

Abstract classes cannot be instantiated. They may contain both abstract methods (without implementations) and concrete methods.

abstract class Shape {
    abstract double area();
    void describe() { System.out.println("I am a shape"); }
}
class Circle extends Shape {
    double r;
    Circle(double r) { this.r = r; }
    @Override double area() { return Math.PI * r * r; }
}
Enter fullscreen mode Exit fullscreen mode

Interfaces

An interface specifies a contract that a class must implement.

interface Drawable { void draw(); }
class Rectangle implements Drawable {
    public void draw() { System.out.println("Drawing Rectangle"); }
}
Enter fullscreen mode Exit fullscreen mode

Summary of Keywords

Keyword Primary Function
this Refers to the current object instance.
super Refers to the immediate superclass.
static Defines class-level members.
final Declares constants, immutable methods, and non-inheritable classes.
abstract Declares abstract classes and methods.
interface Declares a contract of methods to be implemented.

Best Practices

  • Prefer composition over inheritance for flexibility.
  • Keep fields private; expose behavior through methods.
  • Avoid excessive reliance on static members.
  • Use @Override annotations for clarity and safety.

Exercises for Students

  1. Implement a Logger class with overloaded methods for different input types.
  2. Create a base class Payment and subclasses CardPayment and MobilePayment. Demonstrate overriding and dynamic method dispatch.
  3. Define an abstract class Storage and an interface KeyValueStore. Implement InMemoryStore to combine both.

Conclusion

Object-Oriented Programming in Java provides a robust framework for structuring programs in a modular and maintainable manner. Through the concepts of encapsulation, abstraction, inheritance, and polymorphism, developers can model complex systems effectively. Mastery of keywords such as this, super, static, final, and constructs such as abstract classes and interfaces, is crucial for academic and professional proficiency in Java programming.

Feel free to stay connected on LinkedIn ^_^

Top comments (0)