☕ Introduction to OOPs Concept in Java
Object-Oriented Programming (OOP) is one of the most important paradigms in modern software development. Java is a pure object-oriented language (except for primitive types) that supports concepts like encapsulation, inheritance, polymorphism, and abstraction. In this post, we’ll explore the principles of OOP, Java program structure, compilation, and execution — all explained with simple examples.
🔷 Object-Oriented Paradigm
An Object-Oriented Paradigm is a programming model based on the concept of “objects” that contain data and behavior.
- Object: Real-world entity (e.g., Car, Student, Employee).
- Class: Blueprint or template from which objects are created.
- Method: Function defined inside a class to perform actions.
- Message: Way objects communicate by calling methods.
In OOP, the focus is on data rather than procedure — objects interact to solve problems.
📦 Key OOPs Concepts
1️⃣ Data Abstraction
Abstraction means hiding the internal implementation details and showing only essential features of an object.
Example: When you drive a car, you use the steering and pedals without knowing how the engine works internally.
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car starts with key");
}
}
2️⃣ Encapsulation
Encapsulation is the process of binding data (variables) and code (methods) together into a single unit (class), and restricting direct access to data using access modifiers like private.
class Student {
private String name;
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
}
✅ The name variable is hidden from outside access, ensuring data security.
3️⃣ Inheritance
Inheritance allows one class (child) to acquire properties and behavior of another class (parent). It promotes reusability and reduces redundancy.
class Animal {
void eat() { System.out.println("Eating..."); }
}
class Dog extends Animal {
void bark() { System.out.println("Barking..."); }
}
✅ Dog inherits the eat() method from Animal.
4️⃣ Polymorphism
Polymorphism means “many forms.” It allows the same method name to behave differently based on context.
Types:
- Compile-time (Method Overloading)
- Runtime (Method Overriding)
// Compile-time Polymorphism
class MathOperation {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
// Runtime Polymorphism
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
✅ The same method sound() acts differently for Animal and Dog.
5️⃣ Dynamic Binding
Dynamic binding (or late binding) occurs when the method to be executed is determined at runtime rather than compile time. It’s the foundation of runtime polymorphism.
Animal obj = new Dog(); // Reference of parent, object of child
obj.sound(); // Bark (decided at runtime)
6️⃣ Bytecode, JVM, and JDK
- Bytecode: Intermediate code generated by the Java compiler. It’s platform-independent and runs on any system with a JVM.
- JVM (Java Virtual Machine): Executes bytecode and provides a runtime environment.
- JDK (Java Development Kit): Complete package including JRE, compiler, and tools to develop Java applications.
Compilation Process:
Source Code (.java)
↓
Compiler (javac)
↓
Bytecode (.class)
↓
JVM Execution
🧩 Structure of a Java Program
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Explanation:
class HelloWorld– Declares a class.main()– Entry point of the program.System.out.println()– Prints output on the console.
⚙️ Compiling and Executing Java Program
Step 1: Write and save file as HelloWorld.java
Step 2: Compile:
javac HelloWorld.java
Step 3: Run:
java HelloWorld
✅ Output: Hello, World!
🔢 Datatypes in Java
Java supports two types of data:
1️⃣ Primitive Data Types
byte, short, int, long, float, double, char, boolean
2️⃣ Non-Primitive Data Types
String, Arrays, Classes, Interfaces, Enums
📦 Variables and Scope
Variables are named storage locations in memory. They must be declared before use.
Types of Variables:
- Local: Declared inside methods.
- Instance: Declared inside a class but outside methods.
- Static: Shared among all objects of a class.
Example:
class Example {
int x = 10; // Instance variable
static int y = 20; // Static variable
void show() {
int z = 30; // Local variable
System.out.println(x + y + z);
}
}
➕ Operators and Their Types
Java supports several types of operators:
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, >, <, >=, <=
- Logical: &&, ||, !
- Assignment: =, +=, -=, *=, /=
- Unary: ++, --
- Ternary: condition ? expr1 : expr2
🧮 Adding Methods and Parameters
Methods define the behavior of objects. Parameters allow data to be passed to methods.
class Calculator {
int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
Calculator c = new Calculator();
System.out.println("Sum = " + c.add(5, 10));
}
}
✅ Output: Sum = 15
🧱 Static and Final Variables & Methods
🔹 Static:
Belongs to the class rather than any specific object.
class Counter {
static int count = 0;
Counter() { count++; }
}
🔹 Final:
Used to declare constants or prevent method overriding.
final int MAX = 100;
🏗️ Class, Object, and new Operator
Class: Blueprint that defines the structure and behavior of objects.
Object: Instance of a class created using the new operator.
class Student {
int id;
String name;
void display() {
System.out.println(id + " " + name);
}
public static void main(String[] args) {
Student s1 = new Student(); // Object creation
s1.id = 101;
s1.name = "Ananya";
s1.display();
}
}
✅ Output: 101 Ananya
🧠 Summary
- OOP focuses on objects and data rather than logic.
- Core principles: Abstraction, Encapsulation, Inheritance, Polymorphism.
- Java uses bytecode and JVM for platform independence.
- Classes define structure; objects are instances of classes.
- Static and final are used for class-level data and constants.
Written by Saksham Shekher — MCA student and web developer, explaining Java OOP concepts in a simple and exam-friendly way.
