Java OOPS (Object-Oriented Programming System) – Detailed Notes
1. Introduction to OOPS in Java
Object-Oriented Programming System (OOPS) is a programming paradigm that organizes software design around data, or objects, rather than functions and logic.
Java is a fully object-oriented language that structures applications using OOPS concepts, making code more reusable, scalable, and maintainable.
2. Core Concepts of OOPS
A. Object
Definition: Real-world entity, such as a pen, car, or employee.
Structure: Every object in Java has:
State: Attributes/properties/fields (e.g., color, price).
Behavior: What it does (methods/functions, e.g., write(), drive()).
Identity: Unique address in memory.
Example:
class Car {
String color;
int speed;
void drive() {
System.out.println("Driving...");
}
}
Car myCar = new Car();
myCar.color = "Red";
myCar.drive();
B. Class
Definition: Blueprint or template for creating objects.
Contents: Fields (variables), methods, constructors, blocks, nested classes.
Example:
class Student {
String name;
int rollNo;
void study() {
System.out.println(name + " is studying.");
}
}
3. Four Pillars of OOPS
1. Encapsulation
Concept: Wrapping up data (variables) and code (methods) together as a single unit.
Benefits: Data hiding, control over modification, increased flexibility, maintainability.
Implementation in Java: Use private access specifier, provide public getters and setters.
Example:
class Account {
private double balance;
public double getBalance() { return balance; }
public void setBalance(double bal) {
if(bal >= 0) balance = bal;
}
}
Outside classes cannot directly access or modify balance .
2. Inheritance
Concept: Acquiring properties and behaviors of one class (superclass) by another (subclass).
Benefits: Code reuse, hierarchical classification.
Java Supports: Single inheritance via classes, multiple inheritance via interfaces.
Syntax: class Sub extends Super {}
Example:
class Animal {
void eat() { System.out.println("eating..."); }
}
class Dog extends Animal {
void bark() { System.out.println("barking..."); }
}
Dog d = new Dog();
d.eat(); // inherited
d.bark(); // specific
3. Polymorphism
Concept: One interface, multiple implementations (many forms).
Types:
Compile-Time (Method Overloading):
Same method name, different parameters.
Example:
class MathUtil {
int add(int a, int b) { return a+b; }
double add(double a, double b) { return a+b; }
}
Run-Time (Method Overriding):
Subclass provides a specific implementation of a method already defined in the parent.
Example:
class Animal {
void sound() { System.out.println("animal sound"); }
}
class Cat extends Animal {
void sound() { System.out.println("meow"); }
}
Animal obj = new Cat();
obj.sound(); // Output: meow
Benefits: Flexibility, interfaces, dynamic method binding.
4. Abstraction
Concept: Hiding implementation details and showing only the essential features.
Benefits: Security, reduces complexity, improves maintainability.
Achieved By:
Abstract Classes
Can have both abstract methods (without body) and concrete methods.
Cannot be instantiated.
Example:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() { System.out.println("Drawing Circle"); }
}
Interfaces
All methods abstract by default (till Java 7).
Supports multiple inheritance.
Example:
interface Drawable {
void draw();
}
class Rectangle implements Drawable {
public void draw() { System.out.println("Drawing Rectangle"); }
}
4. Important OOPS Keywords & Features
Constructors
Special methods called when an object is created.
Used to initialize state.
Can be overloaded.
Example:
class Employee {
String name;
Employee(String n) { name = n; }
}
Employee emp = new Employee("Vineet");
this Keyword
Refers to the current object instance.
Used to resolve variable shadowing, invoke current class methods, or constructors.
Example:
class Box {
int size;
Box(int size) { this.size = size; }
}
super Keyword
Refers to parent class’s methods, variables, and constructors.
Example:
class SuperClass {
void show() { System.out.println("SuperClass"); }
}
class SubClass extends SuperClass {
void show() {
super.show(); // invokes parent
System.out.println("SubClass");
}
}
static Keyword
Belongs to the class, shared by all instances.
Used for variables, methods, nested classes.
Example:
class Counter {
static int count = 0;
Counter() { count++; }
}
final Keyword
Used with:
Variable: Value cannot change
Method: Cannot be overridden
Class: Cannot be inherited
Example:
final class Constants {
final int MAX = 100;
}
Access Modifiers
Modifier Within Class Within Package Outside Package Subclass Outside Package
private Yes No No No
default Yes Yes No No
protected Yes Yes Yes No
public Yes Yes Yes Yes
5. Relationships in Java OOPS
1. Association
Loose relationship; "uses-a".
Example: Teacher uses Student.
2. Aggregation
"Has-a" relationship, weak association, child can exist independently.
Example: Department has Employees.
Example:
class Employee {}
class Department {
Employee emp; // Aggregation
}
3. Composition
"Part-of" relationship, strong association, child cannot exist independently.
Example: House has Rooms.
Example:
class Room {}
class House {
private Room room; // Composition
}
6. More Code Examples Covering OOPS
Abstract Classes and Inheritance:
abstract class Employee {
abstract void work();
void lunchBreak() { System.out.println("Taking lunch"); }
}
class Developer extends Employee {
void work() { System.out.println("Writing code"); }
}
Interface and Implementation:
interface Animal {
void eat();
}
class Cow implements Animal {
public void eat() { System.out.println("Cow eats grass"); }
}
Polymorphism via Runtime Binding:
class Vehicle {
void run() { System.out.println("Vehicle is running"); }
}
class Bike extends Vehicle {
void run() { System.out.println("Bike is running safely"); }
}
public class TestPolymorphism {
public static void main(String[] args) {
Vehicle v = new Bike();
v.run(); // Output: Bike is running safely
}
}
Method Overloading:
class Print {
void show(int a) {
System.out.println(a);
}
void show(String a) {
System.out.println(a);
}
}
7. OOPS Pillars Summary Table
Pillar Definition Java Implementation Example
Encapsulation Bundling of data & methods Private + getters/setters private int age; public int getAge()
Inheritance Acquiring features of another class extends , implements class B extends A
Polymorphism Many forms of a single interface Overloading/overriding void add(int, int) , void add(double, double)
Abstraction Hiding complexity, essential info abstract , interface abstract void draw()
JAVA OOPS BY VINEET