0% found this document useful (0 votes)
3 views10 pages

Java Activity 1

The document outlines the four pillars of Object-Oriented Programming (OOP) in Java: Encapsulation, Abstraction, Inheritance, and Polymorphism. Each concept is explained with definitions, benefits, and code examples to illustrate their implementation. The document emphasizes the importance of these principles in enhancing code security, maintainability, and reusability.

Uploaded by

pranavshinde7080
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Java Activity 1

The document outlines the four pillars of Object-Oriented Programming (OOP) in Java: Encapsulation, Abstraction, Inheritance, and Polymorphism. Each concept is explained with definitions, benefits, and code examples to illustrate their implementation. The document emphasizes the importance of these principles in enhancing code security, maintainability, and reusability.

Uploaded by

pranavshinde7080
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Four Pillars of OOP in Java

By :- Prajwal S Shinde
Class :- [Link](Computer Science)
Roll no :-323
Div :- B
Four Pillars of Object Oriented Programming
Encapulation
• Encapsulation is a fundamental concept in Object-Oriented
Programming (OOP) that involves bundling data (attributes) and
methods (functions) into a single unit, typically a class. It restricts
direct access to some of an object's components, ensuring better
control, security, and data integrity.
• Encapsulation ensures data security and controlled access.
• It hides the complexity of the implementation while exposing only the
necessary functionality.
• Real-life analogies like a medicine capsule, ATM machine, or car
help simplify the understanding of this concept.
class Account { private double balance; public void deposit(double amount)
{ balance += amount; } public double getBalance() { return balance; } }

Program :-

class Account {
private double balance;

public void deposit(double amount) {


balance += amount;
}

public double getBalance() {


return balance;
}
}
Abstraction
• Abstraction in Java is a fundamental concept in Object-Oriented Programming
(OOP) that focuses on hiding the implementation details of a system while
exposing only the essential features to the user. This allows developers to focus
on what an object does rather than how it does it, simplifying complex systems
and improving code maintainability.
• Hides Complexity: It conceals the internal workings of a class or method,
showing only the necessary details.
• Achieved Through Abstract Classes and Interfaces: Abstract classes provide
partial abstraction, while interfaces allow 100% abstraction.
• Improves Flexibility: Changes in implementation do not affect the code that
depends on the abstraction.
Abstraction Code Example
abstract class Vehicle {
abstract void start();
}

class Car extends Vehicle {


void start() {
[Link]("Car starts with key");
}
}
Inheritance
• Inheritance is one of the fundamental concepts of Object-Oriented
Programming (OOP) in Java. It allows a class (called the child class or subclass)
to inherit the properties and behaviors (fields and methods) of another class (called
the parent class or superclass). This promotes code reuse, hierarchical
classification, and polymorphism.
• Code Reusability: Common code can be written in the parent class and reused in
child classes.
• Extensibility: New functionality can be added to existing classes without
modifying them.
• Polymorphism: Enables dynamic method invocation (method overriding).
class Vehicall {
void sound() {
[Link](“Vehicles makes sound");
}
}

class Bikes extends Vehicle {


void sound() {
[Link](“Bikes Have Two wheels");
}
}
Polymorphism
Polymorphism in Java is the ability of an object to take multiple for
ms, allowing a single interface to represent different underlying forms (
data types or classes).
• Polymorphism increases code flexibility and maintainability
allowing the same interface to be used for different types of object
• Compile time polymorphism is resolved by the compiler, while
runtime polymorphism is resolved by the JVM at runtime.
• Method overloading apply within a class
while method overriding requires inheritance.
Program for Polymorphism :-
class Bird {
void fly() {
[Link]("Bird flies");
}
}

class Eagle extends Bird {


void fly() {
[Link]("Eagle soars high");
}
}

public class Test {


public static void main(String[] args) {
Bird b = new Eagle(); // Polymorphic reference
[Link](); // Output: Eagle soars high }
}

You might also like