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
- Introduction to OOP in Java
- Constructor and Method Overloading
- Method Overriding
- Inheritance
- Access Modifiers
- Returning Objects from Methods
- The
superKeyword - The
thisKeyword - The
staticKeyword - The
finalKeyword - Dynamic Method Dispatch
- Abstract Classes
- Interfaces
- Summary of Keywords
- Which Topic belongs to which Components of OOP?
- Best Practices
- Exercises for Students
- 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.
✅ 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); }
}
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"); }
}
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;
}
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
- Private: For encapsulation; restricts access to within the class.
- Default: For package-level access; no keyword needed.
- Protected: For inheritance; accessible in subclasses and same package.
- 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);
}
}
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); }
}
The super Keyword
super refers to the immediate parent class. It is used to:
- Invoke parent constructors.
- 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);
}
}
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); }
}
The static Keyword
Static members belong to the class rather than an instance.
class Counter {
static int count = 0;
Counter() { count++; }
}
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); }
}
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
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; }
}
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"); }
}
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. |
Which Topic belongs to which Components of OOP?
1️⃣ Inheritance
Inheritance is about reusing code from parent classes in child classes.
- Inheritance ✅ (obviously)
-
The
superkeyword ✅ (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
thiskeyword ✅ (helps in distinguishing instance variables, indirectly supports encapsulation) - Access Modifiers ✅ (access modifiers like private/public, which are classic examples of encapsulation)
-
The
statickeyword ❌ (not directly part of encapsulation, more about class-level variables) -
The
finalkeyword ❌ (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)
Best Practices
- Prefer composition over inheritance for flexibility.
- Keep fields private; expose behavior through methods.
- Avoid excessive reliance on static members.
- Use
@Overrideannotations for clarity and safety.
Exercises for Students
- Implement a
Loggerclass with overloaded methods for different input types. - Create a base class
Paymentand subclassesCardPaymentandMobilePayment. Demonstrate overriding and dynamic method dispatch. - Define an abstract class
Storageand an interfaceKeyValueStore. ImplementInMemoryStoreto 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)