Contents
Denion o OOP (Objec-Oriened Programming)................................................................................ 1
Class and Objecs in Java......................................................................................................................... 2
Consrucors in Java ................................................................................................................................ 5
Objec Class in Java ...............................................................................................................................12
Absracon in Java ................................................................................................................................14
Encapsulaon in Java ............................................................................................................................17
Inheriance in Java ................................................................................................................................20
Access Speciers in Java........................................................................................................................25
Polymorphism in Java............................................................................................................................28
Definiton of OOP (Objec-Oriened Programming)
Objec-Oriened Programming (OOP) is a programming paradigm based on he concep o "objecs,"
which can conain daa (in he orm o elds or atribues) and code (in he orm o mehods or
uncons). OOP emphasizes he principles o encapsulaon, inheriance, absracon, and
polymorphism o design and build modular, reusable, and mainainable sofware sysems.
Key Principles of OOP
1. Encapsulaon:
Bundling daa (atribues) and mehods (uncons) ha operae on he daa ino a single uni
(class). I also involves resricng direc access o some componens using access modiers
(privae, proeced, public).
2. Inheriance:
A mechanism where one class (child) can acquire properes and behaviors o anoher class
(paren). This promoes code reuse and esablishes a hierarchical relaonship beween
classes.
3. Polymorphism:
The abiliy o a single uncon or objec o ake on mulple orms. This is ypically achieved
hrough mehod overloading (compile-me polymorphism) and mehod overriding (runme
polymorphism).
4. Absracon:
The process o hiding implemenaon deails and exposing only he necessary unconaliy.
Absrac classes and ineraces are ools used o achieve absracon.
Advanages of OOP
1. Modulariy: Code is organized ino classes, making i easy o undersand and manage.
2. Reusabiliy: Exisng classes can be reused and exended via inheriance.
3. Scalabiliy: Easier o scale and mainain applicaons as complexiy grows.
4. Daa Securiy: Encapsulaon helps proec sensive daa by resricng access.
5. Flexibiliy: Polymorphism allows mehods o handle dieren ypes o daa or behavior.
Real-World Example
A "Car" class in OOP could include:
• Atribues: brand, color, speed.
• Mehods: sarEngine(), accelerae(), brake().
Class and Objecs in Java
Denion:
• Class: A class is a blueprin or prooype rom which objecs are creaed. I denes properes
(variables) and behaviors (mehods) ha he objecs creaed rom he class will have.
• Objec: An objec is an insance o a class. When a class is dened, no memory is allocaed
unl an objec o ha class is creaed.
Class Synax:
class ClassName {
// Fields (Properes)
in eld1;
Sring eld2;
// Consrucor
public ClassName(in eld1, Sring eld2) {
his.eld1 = eld1;
his.eld2 = eld2;
// Mehod (Behavior)
public void display() {
Sys[Link].prinln("Field1: " + eld1 + ", Field2: " + eld2);
Creang Objecs:
To creae an objec o a class, you use he new keyword ollowed by he consrucor o he class.
ClassName objecName = new ClassName(argumens);
Example Code: Class and Objecs
// Class denion
class Car {
// Properes (Fields)
Sring model;
in year;
Sring color;
// Consrucor o inialize he properes
public Car(Sring model, in year, Sring color) {
[Link] = model;
[Link] = year;
[Link] = color;
// Mehod o display he deails o he car
public void displayDeails() {
Sys[Link].prinln("Car Model: " + model);
Sys[Link].prinln("Car Year: " + year);
Sys[Link].prinln("Car Color: " + color);
// Mehod o sar he car
public void sar() {
Sys[Link].prinln(model + " is sarng...");
public class Main {
public sac void main(Sring[] args) {
// Creang objecs (insances) o he Car class
Car car1 = new Car("Toyoa Corolla", 2020, "Red");
Car car2 = new Car("Honda Civic", 2021, "Blue");
// Calling mehods on car1 and car2 objecs
[Link]ails(); // Displaying deails o car1
car1.sar(); // Sarng car1
Sys[Link].prinln(); // Prin a blank line or separaon
[Link]ails(); // Displaying deails o car2
car2.sar(); // Sarng car2
Oupu:
yaml
Car Model: Toyoa Corolla
Car Year: 2020
Car Color: Red
Toyoa Corolla is sarng...
Car Model: Honda Civic
Car Year: 2021
Car Color: Blue
Honda Civic is sarng...
Consrucors in Java
Denion:
A consrucor in Java is a special mehod used o inialize objecs. I is called auomacally when an
objec o a class is creaed. The consrucor's primary purpose is o assign inial values o he elds
o a class.
Characeriscs of Consrucors:
1. Name: A consrucor has he same name as he class.
2. No Reurn Type: I does no have a reurn ype (no even void).
3. Auomac Invocaon: Called auomacally when an objec is creaed using he new
keyword.
4. Overloading: Mulple consrucors can be dened in a class (consrucor overloading) by
varying he number or ypes o parameers.
Types of Consrucors in Java:
1. Defaul Consrucor:
o A consrucor wihou any parameers.
o Auomacally provided by Java i no oher consrucor is dened.
o Inializes elds o heir deaul values (e.g., 0 or numbers, null or objecs, ec.).
Example:
class Suden {
Sring name;
in age;
// Deaul Consrucor
Suden() {
Sys[Link].prinln("Deaul consrucor called");
name = "Unknown";
age = 0;
void displayIno() {
Sys[Link].prinln("Name: " + name);
Sys[Link].prinln("Age: " + age);
public class Main {
public sac void main(Sring[] args) {
Suden suden = new Suden(); // Deaul consrucor called
suden.displayIno();
Oupu:
Deaul consrucor called
Name: Unknown
Age: 0
2. Parameerized Consrucor:
o A consrucor ha acceps parameers o inialize elds wih specic values.
o Useul or creang objecs wih dieren inial saes.
Example:
class Suden {
Sring name;
in age;
// Parameerized Consrucor
Suden(Sring name, in age) {
[Link] = name;
[Link] = age;
void displayIno() {
Sys[Link].prinln("Name: " + name);
Sys[Link].prinln("Age: " + age);
public class Main {
public sac void main(Sring[] args) {
Suden suden = new Suden("Anil", 20); // Parameerized consrucor called
suden.displayIno();
Oupu:
Name: Anil
Age: 20
3. Copy Consrucor:
o A consrucor ha creaes a new objec as a copy o an exisng objec.
o Java does no provide a deaul copy consrucor; you mus implemen i explicily.
Example:
class Suden {
Sring name;
in age;
// Parameerized Consrucor
Suden(Sring name, in age) {
[Link] = name;
[Link] = age;
}
// Copy Consrucor
Suden(Suden oher) {
[Link] = o[Link];
[Link] = o[Link];
void displayIno() {
Sys[Link].prinln("Name: " + name);
Sys[Link].prinln("Age: " + age);
public class Main {
public sac void main(Sring[] args) {
Suden suden1 = new Suden("Anil", 20);
Suden suden2 = new Suden(suden1); // Copy consrucor called
suden[Link]o();
Oupu:
Name: Anil
Age: 20
Key Poins o Remember:
1. his():
Used o call one consrucor rom anoher wihin he same class.
Example:
class Suden {
Sring name;
in age;
Suden() {
his("Deaul Name", 0); // Calls parameerized consrucor
Suden(Sring name, in age) {
[Link] = name;
[Link] = age;
void displayIno() {
Sys[Link].prinln("Name: " + name);
Sys[Link].prinln("Age: " + age);
public class Main {
public sac void main(Sring[] args) {
Suden suden = new Suden(); // Calls deaul consrucor
suden.displayIno();
2. super():
Used o call he paren class consrucor.
Example:
class Paren {
Paren() {
Sys[Link].prinln("Paren class consrucor called");
class Child exends Paren {
Child() {
super(); // Calls he Paren class consrucor
Sys[Link].prinln("Child class consrucor called");
public class Main {
public sac void main(Sring[] args) {
Child child = new Child();
Oupu:
kolin
Copy code
Paren class consrucor called
Child class consrucor called
Consrucor Overloading:
Java suppors dening mulple consrucors wih dieren parameer liss.
Example:
class Recangle {
in lengh, breadh;
// Consrucor wih no parameers
Recangle() {
[Link]h = 0;
[Link]h = 0;
// Consrucor wih one parameer
Recangle(in side) {
[Link]h = side;
[Link]h = side;
// Consrucor wih wo parameers
Recangle(in lengh, in breadh) {
[Link]h = lengh;
[Link]h = breadh;
in area() {
reurn lengh * breadh;
public class Main {
public sac void main(Sring[] args) {
Recangle r1 = new Recangle();
Recangle r2 = new Recangle(5);
Recangle r3 = new Recangle(4, 6);
Sys[Link].prinln("Area o r1: " + [Link]());
Sys[Link].prinln("Area o r2: " + [Link]());
Sys[Link].prinln("Area o r3: " + [Link]());
Oupu:
mahemaca
Copy code
Area o r1: 0
Area o r2: 25
Area o r3: 24
Objec Class in Java
Denion:
The Objec class in Java is he roo class o he Java class hierarchy. Every class in Java, eiher direcly
or indirecly, inheris rom he Objec class. This means ha all Java classes auomacally inheri he
mehods o he Objec class unless explicily overridden.
Key Characeriscs:
1. Roo Class: All classes in Java exend Objec implicily.
2. Common Mehods: Provides mehods ha are available o all Java objecs, such as
oSring(), equals(), hashCode(), ec.
3. Universal Paren: Even user-dened classes are derived rom he Objec class.
Mehods of he Objec Class
Mehod Descripon
oSring() Reurns a sring represenaon o he objec.
equals(Objec
Compares wo objecs or equaliy.
obj)
hashCode() Reurns an ineger hash code or he objec.
geClass() Reurns he runme class o he objec.
clone() Creaes and reurns a copy o he objec. (Used wih Cloneable inerace.)
nalize() Called by he garbage collecor when here are no more reerences o he objec.
Causes he curren hread o wai unl anoher hread invokes noy() or
wai()
noyAll() on he objec.
noy() Wakes up a single hread waing on he objec's monior.
noyAll() Wakes up all hreads waing on he objec's monior.
oSring():
• Reurns a sring represenaon o he objec.
• Deaul implemenaon: ClassName@HashCodeInHexadecimal.
• Typically overridden in cusom classes or meaningul oupu.
Example:
class Suden{
in rollno;
Sring name;
Sring ciy;
Suden(in rollno, Sring name, Sring ciy){
[Link]=rollno;
[Link]=name;
[Link]y=ciy;
public Sring oSring(){//overriding he oSring() mehod
reurn rollno+" "+name+" "+ciy;
public sac void main(Sring args[]){
Suden s1=new Suden(101,"Raj","lucknow");
Suden s2=new Suden(102,"Vijay","ghaziabad");
Sys[Link].prinln(s1);//compiler wries here s1.oSring()
Sys[Link].prinln(s2);//compiler wries here s2.oSring()
Oupu:
101 Raj lucknow
102 Vijay ghaziabad
Absracton in Java
Denion:
Absracon in Java is he process o hiding implemenaon deails and showing only essenal
eaures o an objec or a sysem. I ocuses on what an objec does insead o how i does i.
Absracon is achieved in Java hrough absrac classes and ineraces.
Key Characeriscs:
1. Purpose: Simplies complexiy by exposing only necessary unconaliy o he user.
2. Focus: Denes unconaliy raher han implemenaon.
3. Achieved Through:
o Absrac Classes.
o Ineraces.
Deails
1. Absrac Class:
• A class declared wih he absrac keyword.
• Can have boh absrac mehods (wihou implemenaon) and concree mehods (wih
implemenaon).
• Canno be insanaed direcly.
Synax:
absrac class AbsracClass {
// Absrac mehod (no body)
absrac void absracMehod();
// Concree mehod (has body)
void concreeMehod() {
Sys[Link].prinln("This is a concree mehod.");
Example:
absrac class Shape {
absrac void draw(); // Absrac mehod
void display() { // Concree mehod
Sys[Link].prinln("This is a shape.");
class Circle exends Shape {
@Override
void draw() {
Sys[Link].prinln("Drawing a circle.");
public class Main {
public sac void main(Sring[] args) {
Shape shape = new Circle(); // Polymorphism
[Link]();
[Link]();
Oupu:
Drawing a circle.
This is a shape.
2. Inerface:
• A blueprin or a class conaining only absrac mehods (unl Java 7).
• From Java 8 onwards, can also include:
o Deaul mehods (mehods wih implemenaon).
o Sac mehods.
• Suppors mulple inheriance by allowing a class o implemen mulple ineraces.
Synax:
inerace IneraceName {
void absracMehod(); // Absrac mehod
deaul void deaulMehod() {
Sys[Link].prinln("This is a deaul mehod.");
Example:
inerace Animal {
void sound(); // Absrac mehod
deaul void breahe() { // Deaul mehod
Sys[Link].prinln("Animals breahe oxygen.");
class Dog implemens Animal {
@Override
public void sound() {
Sys[Link].prinln("Dog barks.");
public class Main {
public sac void main(Sring[] args) {
Animal dog = new Dog();
[Link]();
[Link]he();
}
Oupu:
Dog barks.
Animals breahe oxygen.
Key Dierences Beween Absrac Classes and Inerfaces
Aspec Absrac Class Inerface
Declaraon Declared using he absrac keyword. Declared using he inerace keyword.
Can have boh absrac and concree Can have absrac, deaul, and sac
Mehods
mehods. mehods.
Can only have sac nal variables
Fields Can have insance variables.
(consans).
Mulple A class can exend only one absrac A class can implemen mulple
Inheriance class. ineraces.
Access Modiers Can have any access modier. Mehods are public by deaul.
Benes of Absracon:
1. Improved Code Mainainabiliy: By exposing only high-level unconaliy, changes in
implemenaon do no aec he users o he absracon.
2. Enhanced Securiy: Implemenaon deails are hidden rom he user.
3. Code Reusabiliy: Absrac classes and ineraces allow sharing common unconaliy among
relaed classes.
4. Scalabiliy: Easier o exend or modiy absraced sysems.
Encapsulaton in Java
Denion:
Encapsulaon is one o he undamenal principles o Objec-Oriened Programming (OOP). I reers
o he process o wrapping daa (elds) and mehods (uncons) ha operae on he daa ino a
single uni (class) and resricng direc access o he elds. Encapsulaon ensures ha he inernal
represenaon o an objec is hidden rom he ouside world and accessed only hrough well-dened
mehods.
Key Characeriscs:
1. Daa Hiding: Fields (daa) o a class are kep privae o preven unauhorized access.
2. Conrolled Access: Access o he elds is provided hrough public geter and seter mehods.
3. Improved Securiy: Only auhorized mehods can modiy he daa.
4. Increased Mainainabiliy: Code changes in one par o he class do no aec oher pars o
he applicaon.
Deails of Encapsulaon
Componens of Encapsulaon:
1. Privae Fields: Daa members o a class are declared as privae o resric access.
2. Public Mehods: Public geter and seter mehods are used o access and modiy privae
elds.
3. Validaon Logic: Seters can include validaon rules o ensure daa inegriy.
Code Implemenaon
Example 1: Basic Encapsulaon
class Suden {
privae Sring name; // Privae eld
privae in age; // Privae eld
// Geter or name
public Sring geName() {
reurn name;
// Seter or name
public void seName(Sring name) {
[Link] = name;
// Geter or age
public in geAge() {
reurn age;
}
// Seter or age wih validaon
public void seAge(in age) {
i (age > 0) {
[Link] = age;
} else {
Sys[Link].prinln("Age mus be posive.");
public class Main {
public sac void main(Sring[] args) {
Suden suden = new Suden();
suden.seName("Anil");
suden.seAge(20);
Sys[Link].prinln("Name: " + suden.geName());
Sys[Link].prinln("Age: " + suden.geAge());
Oupu:
Name: Anil
Age: 20
Inheriance in Java
Denion:
Inheriance is an Objec-Oriened Programming (OOP) concep where one class (child class or
subclass) can acquire he properes (elds) and behaviors (mehods) o anoher class (paren class
or superclass). I allows or code reuse and he creaon o a hierarchical relaonship beween
classes.
Key Characeriscs:
1. Code Reusabiliy: Enables a subclass o reuse he code rom a paren class.
2. Hierarchy: Creaes a relaonship beween general and specic classes.
3. Exensibiliy: Faciliaes he addion o new eaures o exisng classes.
4. Types of Inheriance:
o Single Inheriance.
o Mullevel Inheriance.
o Hierarchical Inheriance.
Use Cases:
1. Reusing exisng unconaliy in new classes.
2. Implemenng polymorphism (mehod overriding).
3. Creang a hierarchical srucure o relaed classes.
Deails
Synax:
class ParenClass {
// Paren class members
class ChildClass exends ParenClass {
// Child class members
Code Implemenaon
Example 1: Single Inheriance
class Animal {
void ea() {
Sys[Link].prinln("This animal eas ood.");
class Dog exends Animal {
void bark() {
Sys[Link].prinln("The dog barks.");
public class Main {
public sac void main(Sring[] args) {
Dog dog = new Dog();
[Link](); // Inheried mehod
[Link](); // Mehod o Dog class
Oupu:
This animal eas ood.
The dog barks.
Example 2: Mullevel Inheriance
class Animal {
void ea() {
Sys[Link].prinln("This animal eas ood.");
}
class Mammal exends Animal {
void walk() {
Sys[Link].prinln("This mammal walks.");
class Dog exends Mammal {
void bark() {
Sys[Link].prinln("The dog barks.");
public class Main {
public sac void main(Sring[] args) {
Dog dog = new Dog();
[Link](); // Inheried rom Animal
[Link](); // Inheried rom Mammal
[Link](); // Dened in Dog
Oupu:
This animal eas ood.
This mammal walks.
The dog barks.
Example 3: Hierarchical Inheriance
class Animal {
void ea() {
Sys[Link].prinln("This animal eas ood.");
}
class Dog exends Animal {
void bark() {
Sys[Link].prinln("The dog barks.");
class Ca exends Animal {
void meow() {
Sys[Link].prinln("The ca meows.");
public class Main {
public sac void main(Sring[] args) {
Dog dog = new Dog();
[Link]();
[Link]();
Ca ca = new Ca();
ca.ea();
ca.meow();
Oupu:
This animal eas ood.
The dog barks.
This animal eas ood.
The ca meows.
Types of Inheriance in Java:
1. Single Inheriance: A subclass inheris rom a single paren class.
2. Mullevel Inheriance: A class inheris rom a subclass, orming a chain.
3. Hierarchical Inheriance: Mulple subclasses inheri rom a single paren class.
Mehod Overriding in Inheriance
When a subclass provides a specic implemenaon o a mehod ha is already dened in is paren
class, i is known as mehod overriding. This is a key eaure o polymorphism.
Example:
java
Copy code
class Animal {
void sound() {
Sys[Link].prinln("Animal makes a sound.");
class Dog exends Animal {
@Override
void sound() {
Sys[Link].prinln("Dog barks.");
public class Main {
public sac void main(Sring[] args) {
Animal animal = new Dog(); // Polymorphism
[Link]();
Oupu:
Dog barks.
Access Specifiers in Java
Denion:
Access speciers in Java dene he visibiliy and accessibiliy o classes, mehods, and variables. They
deermine which pars o a program can access a parcular class member.
Java provides four ypes of access speciers:
1. Public
2. Privae
3. Proeced
4. Defaul (no keyword)
Types of Access Speciers
1. Public
• Denion: The public keyword makes a class, mehod, or eld accessible rom anywhere in
he program.
• Scope: Visible o all classes, even ouside he package.
• Use Case: When mehods or variables need o be accessible across all packages.
Example:
class PublicExample {
public void show() {
Sys[Link].prinln("This is a public mehod.");
public class Main {
public sac void main(Sring[] args) {
PublicExample obj = new PublicExample();
[Link](); // Accessible
Oupu:
This is a public mehod.
2. Privae
• Denion: The privae keyword resrics access o he class isel.
• Scope:
o Visible only wihin he class where i is dened.
o No accessible by subclasses or oher classes.
• Use Case: When daa or mehods should no be accessed direcly or securiy or
encapsulaon.
Example:
class PrivaeExample {
privae in number = 10;
privae void display() {
Sys[Link].prinln("This is a privae mehod.");
public void accessPrivae() {
display(); // Privae mehod accessed wihin he class
public class Main {
public sac void main(Sring[] args) {
PrivaeExample obj = new PrivaeExample();
[Link]e(); // Indirec access o he privae mehod
Oupu:
This is a privae mehod.
3. Proeced
• Denion: The proeced keyword allows access wihin he same package and o subclasses,
even i hey are in dieren packages.
• Scope:
o Accessible in he same package.
o Accessible in subclasses hrough inheriance (even in dieren packages).
• Use Case: When exending classes and allowing conrolled access o variables or mehods.
Example:
class Paren {
proeced void display() {
Sys[Link].prinln("This is a proeced mehod.");
class Child exends Paren {
void show() {
display(); // Proeced mehod accessed in subclass
public class Main {
public sac void main(Sring[] args) {
Child obj = new Child();
[Link]();
Oupu:
This is a proeced mehod.
Comparison Table
Access Specier Wihin Class Same Package Subclass (Dieren Package) Ouside Package
Public Yes Yes Yes Yes
Privae Yes No No No
Proeced Yes Yes Yes (i subclassed) No
Defaul Yes Yes No No
Key Poins:
1. Public: Mos open, accessible everywhere.
2. Privae: Mos resricve, visible only wihin he same class.
3. Proeced: Balanced access or inheriance and package-level use.
4. Defaul: Package-privae access, no visibiliy ouside he package.
Polymorphism in Java
Denion:
Polymorphism is one o he core conceps o Objec-Oriened Programming (OOP) ha allows
objecs o dieren classes o be reaed as objecs o a common superclass. The erm polymorphism
means "many shapes" and i allows a single acon o behave dierenly based on he objec ha i is
acng upon. In Java, polymorphism can be achieved in wo ways:
1. Compile-me Polymorphism (Mehod Overloading)
2. Runme Polymorphism (Mehod Overriding)
Types of Polymorphism:
1. Compile-me Polymorphism (Mehod Overloading):
• Denion: Mehod overloading occurs when wo or more mehods in he same class have
he same name bu dieren parameers (eiher in number or ype).
• How i works: The mehod is seleced a compile me based on he mehod signaure
(number, ype, and order o parameers).
• Use Case: Used o perorm similar operaons wih dieren ypes or numbers o argumens.
Example:
class Calculaor {
// Overloaded mehod wih wo ineger parameers
public in add(in a, in b) {
reurn a + b;
// Overloaded mehod wih hree ineger parameers
public in add(in a, in b, in c) {
reurn a + b + c;
public class Main {
public sac void main(Sring[] args) {
Calculaor calc = new Calculaor();
Sys[Link].prinln([Link](5, 10)); // Calls mehod wih wo parameers
Sys[Link].prinln([Link](5, 10, 15)); // Calls mehod wih hree parameers
Oupu:
15
30
2. Runme Polymorphism (Mehod Overriding):
• Denion: Mehod overriding occurs when a subclass provides a specic implemenaon or
a mehod ha is already dened in is superclass. The mehod is called a runme, and he
subclass mehod is invoked based on he ype o objec being reerred o, no he reerence
ype.
• How i works: The mehod is seleced a runme based on he objec's ype.
• Use Case: Used o implemen dynamic mehod dispach, especially when you wan o
change or exend he behavior o mehods in he superclass.
Example:
class Animal {
// Mehod in superclass
public void sound() {
Sys[Link].prinln("Animal makes a sound");
class Dog exends Animal {
// Overridden mehod in subclass
@Override
public void sound() {
Sys[Link].prinln("Dog barks");
class Ca exends Animal {
// Overridden mehod in subclass
@Override
public void sound() {
Sys[Link].prinln("Ca meows");
public class Main {
public sac void main(Sring[] args) {
Animal myAnimal = new Animal(); // Paren class reerence
Animal myDog = new Dog(); // Child class reerence
Animal myCa = new Ca(); // Child class reerence
[Link](); // Calls Animal's sound mehod
[Link](); // Calls Dog's overridden sound mehod
myCa.sound(); // Calls Ca's overridden sound mehod
Oupu:
Animal makes a sound
Dog barks
Ca meows