OBJECT ORIENTED PROGRAMMING LANGUAGE
OOP is a Programming Paradigm
Paradigm means a model, pattern
Functional Programming (FP)
Functional programming is based on functions only (no objects)
- Focuses on what to do, not how
- Avoids changing data (immutable)
- writing programs using functions and avoiding data changes
Object-Oriented Programming (OOP)
OOP is based on objects and classes
- Class → blueprint
- Object → real-world entity
- organizing code using objects (real-world modeling)
Object is a real instance of a class with data and behavior
Easy example:
Car example:
- Class → Car
- Objects → BMW, Audi, Tesla
Each object has:
- Attributes → color, speed
- Methods → drive(), brake()
Attributes :
Eg scenario : considering a dog
It has a attribute of white fur
Instance Variable
A variable that belongs to an object (instance)
Class Variable (Static Variable)
A variable that belongs to the class, not objects
Declared using static
Methods:
It barkes (barkes())
Object is a real instance of a class with data and behavior
1. Instance Method
Method that belongs to an object
2. Static Method (Class Method)
Method that belongs to the class
Uses static
Example code
// Class
class Student {
// Instance variable (belongs to object)
String name;
// Static variable (common for all objects)
static String college = “Sairam”;
// Instance method (needs object)
void display() {
System.out.println(“Name: “ + name);
}
// Static method (no object needed)
static void showCollege() {
System.out.println(“College: “ + college);
}
}
public class Main {
public static void main(String[] args) {
// Object creation
Student s1 = new Student();
// Assign value to instance variable
s1.name = “Subashini”;
// Calling instance method
s1.display();
// Calling static method (no object needed)
Student.showCollege();
}
}
ADVANTAGES OF OOPS
- Code Reusability
- Data Hiding (Security)
- Modularity
- Easy Maintenance
- Flexibility (Polymorphism)
- Abstraction (Less Complexity)
- Real-world Modeling
Pillars of OOPS
Encapsulation :
Binding the Attributes and Methods
Example: Bank Account
- Your balance is hidden
- You access it using methods like deposit/withdraw
class Bank {
private int balance; // private data
void setBalance(int b) { balance = b; } // setter
int getBalance() { return balance; } // getter
}
class Main {
public static void main(String[] args) {
Bank b = new Bank();
b.setBalance(1000); // set value
System.out.println(b.getBalance()); // get value
}
}
Inheritance (Reuse)
Child class gets features from parent class
Example: Dog inherits from Animals
class Animal { // Parent class
void eat() {
System.out.println(“Eating”);
}
}
class Dog extends Animal { // Child class
void bark() {
System.out.println(“Barking”);
}
}
class Main {
public static void main(String[] args) {
Dog d = new Dog(); // object
d.eat(); // inherited from Animal
d.bark(); // own method
}
}
Example:
- Parent:
Car - Child:
BMW
BMW automatically gets car features
1] Single Inheritance:
A class inherits from only one superclass
- One subclass and one superclass
- Uses
extendskeyword
class Animal {
void eat() {
System.out.println(“Animal eats”);
}
}
class Dog extends Animal {
}
class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
}
}
2] Multiple Inheritance:
A class inherits from more than one superclass
- Achieved via
implements and interfaces - Avoids diamond problem
interface Animal {
void eat();
}
interface Pet {
void play();
}
class Dog implements Animal, Pet {
public void eat() {
System.out.println(“Dog eats”);
}
public void play() {
System.out.println(“Dog plays”);
}
}
class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.play();
}
}
What is a Diamond Problem?
The Diamond Problem is a situation in multiple inheritance where a class inherits from two parent classes that have the same method, causing confusion (ambiguity).
3] Multilevel Inheritance:
A class inherits from a subclass, forming a chain of inheritance
Inheritance happens level by level
class Animal {
void eat() {
System.out.println(“Animal eats”);
}
}
class Dog extends Animal {
void bark() {
System.out.println(“Dog barks”);
}
}
class Puppy extends Dog {
}
class Main {
public static void main(String[] args) {
Puppy p = new Puppy();
p.eat();
p.bark();
}
}
4] Hierarchical Inheritance:
Get Subashini Mannuraja’s stories in your inbox
Join Medium for free to get updates from this writer.
Multiple subclasses inherit from a single superclass
- One parent → multiple children
- Promotes code reuse
class Animal {
void eat() {
System.out.println(“Animal eats”);
}
}
class Dog extends Animal {
}
class Cat extends Animal {
}
class Main {
public static void main(String[] args) {
Dog d = new Dog();
Cat c = new Cat();
d.eat();
c.eat();
}
}
5] Hybrid Inheritance:
A combination of two or more inheritance types
Combination of Single, Hierarchical and Multiple Inheritance
class Animal {
void eat() {
System.out.println(“Animal eats”);
}
}
interface Pet {
void play();
}
class Dog extends Animal implements Pet {
public void play() {
System.out.println(“Dog plays”);
}
}
class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.play();
}
}
Polymorphism:
Polymorphism = “Many forms”
One method (same name) behaves in different ways
Example:
Think of a person
- As a student → studies
- As a friend → helps
- As a daughter → shows love to their parents
Same person, different behavior
class Animal {
void sound() {
System.out.println(“Animal makes sound”);
}
}
class Dog extends Animal {
void sound() {
System.out.println(“Dog barks”);
}
}
class Main {
public static void main(String[] args) {
Animal a = new Dog(); // polymorphism
a.sound();
}
}
Types of Polymorphism
1] Compile time polymorphism (Method Overloading)
Same method name, different parameters
Same method name add()
But:
- One takes 2 inputs
- One takes 3 inputs
class Add {
int sum(int a, int b) {
return a + b;
}
int sum(int a, int b, int c) {
return a + b + c;
}
}
class Main {
public static void main(String[] args) {
Add obj = new Add();
System.out.println(obj.sum(2, 3)); // 2 params
System.out.println(obj.sum(2, 3, 4)); // 3 params
}
}
2] Run time polymorphism (Method Overriding)
Same method name and Same parameters
But different implementation (body)
class Animal {
void sound() {
System.out.println(“Animal sound”);
}
}
class Dog extends Animal {
void sound() {
System.out.println(“Dog barks”);
}
}
class Main {
public static void main(String[] args) {
Animal a = new Dog(); // parent reference
a.sound(); // calls Dog’s method
}
}
Abstraction
Abstraction is the process of hiding implementation details and exposing only the essential features of an object.
Example: Car
You drive without knowing engine details
abstract class Animal {
abstract void sound(); // abstract method (no body)
void eat() { // normal method
System.out.println(“Animal eats”);
}
}
class Dog extends Animal {
void sound() {
System.out.println(“Dog barks”);
}
}
class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound(); // implemented method
d.eat(); // inherited method
}
}
In Object-Oriented Programming, abstraction is achieved using:
- Abstract classes
- Interfaces
Example: Charger
An abstract class is a class that cannot be instantiated (no object creation) and can have:
Abstract methods (no body)
Concrete methods (with body)
1] Abstract Methods (no body)
These are methods without implementation
2] Concrete Methods (with body)
These are normal methods with code
abstract class Animal {
// Abstract method (no body)
abstract void sound();
// Concrete method (with body)
void eat() {
System.out.println(“Animal eats”);
}
}
class Dog extends Animal {
// Implementing abstract method
void sound() {
System.out.println(“Dog barks”);
}
}
class Main {
public static void main(String[] args) {
Dog d = new Dog(); // object of child class
d.sound(); // abstract method implemented
d.eat(); // concrete method from parent
}
}
Interface
An interface is a place where you write only method names (no body)
interface Animal {
void sound(); // abstract method (no body)
}
class Dog implements Animal {
public void sound() {
System.out.println(“Dog barks”);
}
}
class Main {
public static void main(String[] args) {
Dog d = new Dog(); // object
d.sound(); // calling method
}
}
When to use Extends and Implements keyword?
Use extends when:
- You are using a class (parent → child)
- Or an abstract class
- It represents an “IS-A” relationship
- Dog IS A Animal
- Reuses code from parent class
- Used for inheritance
- Abstract class → use
extends
Use implements when:
- You are using an interface
- It represents a “CAN-DO”
- Must implement all methods
- Used to define rules
CONSTRUCTOR
A constructor is a special method used to initialize an object when it is created.
Example:
When a student joins a college:
- You immediately store:
- Name
- Roll number
- Department
This “initial setup” = constructor
class Student {
String name;
int rollNo;
// Constructor
Student(String n, int r) {
name = n;
rollNo = r;
}
void display() {
System.out.println(“Name: “ + name);
System.out.println(“Roll No: “ + rollNo);
}
}
class Main {
public static void main(String[] args) {
// Object creation + constructor call
Student s1 = new Student(“Subashini”, 101);
s1.display();
}
}






