From 9bba1a33c25d12f7eb0e268e6734367cc08a3a58 Mon Sep 17 00:00:00 2001 From: Suryanshu Agrawal Date: Fri, 17 Oct 2025 12:04:49 +0530 Subject: [PATCH] Added OOPs concepts demos --- .../thealgorithms/oops/AbstractionDemo.java | 26 +++++++++ .../thealgorithms/oops/EncapsulationDemo.java | 36 ++++++++++++ .../thealgorithms/oops/InheritanceDemo.java | 55 +++++++++++++++++++ .../thealgorithms/oops/PolymorphismDemo.java | 33 +++++++++++ 4 files changed, 150 insertions(+) create mode 100644 src/main/java/com/thealgorithms/oops/AbstractionDemo.java create mode 100644 src/main/java/com/thealgorithms/oops/EncapsulationDemo.java create mode 100644 src/main/java/com/thealgorithms/oops/InheritanceDemo.java create mode 100644 src/main/java/com/thealgorithms/oops/PolymorphismDemo.java diff --git a/src/main/java/com/thealgorithms/oops/AbstractionDemo.java b/src/main/java/com/thealgorithms/oops/AbstractionDemo.java new file mode 100644 index 000000000000..afda302e78d4 --- /dev/null +++ b/src/main/java/com/thealgorithms/oops/AbstractionDemo.java @@ -0,0 +1,26 @@ +package com.thealgorithms.oopconcepts; + +abstract class Vehicle { + abstract void start(); + abstract void stop(); +} + +class Car extends Vehicle { + @Override + void start() { + System.out.println("Car engine started"); + } + + @Override + void stop() { + System.out.println("Car stopped"); + } +} + +public class AbstractionDemo { + public static void main(String[] args) { + Vehicle v = new Car(); + v.start(); + v.stop(); + } +} diff --git a/src/main/java/com/thealgorithms/oops/EncapsulationDemo.java b/src/main/java/com/thealgorithms/oops/EncapsulationDemo.java new file mode 100644 index 000000000000..91154aeae9c0 --- /dev/null +++ b/src/main/java/com/thealgorithms/oops/EncapsulationDemo.java @@ -0,0 +1,36 @@ +package com.thealgorithms.oopconcepts; + +class Account { + private String holder; + private double balance; + + public String getHolder() { + return holder; + } + + public void setHolder(String holder) { + this.holder = holder; + } + + public double getBalance() { + return balance; + } + + public void deposit(double amount) { + if (amount > 0) balance += amount; + } + + public void withdraw(double amount) { + if (amount > 0 && amount <= balance) balance -= amount; + } +} + +public class EncapsulationTest { + public static void main(String[] args) { + Account acc = new Account(); + acc.setHolder("Suryanshu"); + acc.deposit(50000); + acc.withdraw(12000); + System.out.println(acc.getHolder() + " has balance ₹" + acc.getBalance()); + } +} diff --git a/src/main/java/com/thealgorithms/oops/InheritanceDemo.java b/src/main/java/com/thealgorithms/oops/InheritanceDemo.java new file mode 100644 index 000000000000..d826c5f8a2d2 --- /dev/null +++ b/src/main/java/com/thealgorithms/oops/InheritanceDemo.java @@ -0,0 +1,55 @@ +package com.thealgorithms.oopconcepts; +class Vehicle { + int tyres; + String colour; + String companyName; + + void setTyres(int tyres) { + this.tyres = tyres; + System.out.println("Number of tyres: " + tyres); + } + + void setColour(String colour) { + this.colour = colour; + System.out.println("Colour of vehicle: " + colour); + } + + void setCompanyName(String companyName) { + this.companyName = companyName; + System.out.println("Company name: " + companyName); + } +} + +class Truck extends Vehicle { + int wheels = 8, headlights = 2; + String model; + + Truck(String model) { + this.model = model; + System.out.println("This is a truck: " + model); + } +} + +class Motorcycle extends Vehicle { + int wheels = 2, headlights = 1; + String model; + + Motorcycle(String model) { + this.model = model; + System.out.println("This is a motorcycle: " + model); + } +} + +public class InheritanceDemo { + public static void main(String[] args) { + Motorcycle mc = new Motorcycle("Dream"); + mc.setColour("Black"); + mc.setTyres(2); + mc.setCompanyName("Honda"); + + Truck tk = new Truck("Tata Ultra"); + tk.setTyres(8); + tk.setColour("White"); + tk.setCompanyName("Tata"); + } +} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/oops/PolymorphismDemo.java b/src/main/java/com/thealgorithms/oops/PolymorphismDemo.java new file mode 100644 index 000000000000..53f12d7f5e6a --- /dev/null +++ b/src/main/java/com/thealgorithms/oops/PolymorphismDemo.java @@ -0,0 +1,33 @@ +package com.thealgorithms.oopconcepts; + +class Payment { + void pay() { + System.out.println("Processing generic payment"); + } +} + +class CreditCardPayment extends Payment { + @Override + void pay() { + System.out.println("Payment done via Credit Card"); + } +} + +class UpiPayment extends Payment { + @Override + void pay() { + System.out.println("Payment done via UPI"); + } +} + +public class PolymorphismExample { + public static void main(String[] args) { + Payment p1 = new CreditCardPayment(); + Payment p2 = new UpiPayment(); + Payment p3 = new Payment(); + + p1.pay(); + p2.pay(); + p3.pay(); + } +}