Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/com/thealgorithms/oops/AbstractionDemo.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/thealgorithms/oops/EncapsulationDemo.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
55 changes: 55 additions & 0 deletions src/main/java/com/thealgorithms/oops/InheritanceDemo.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/thealgorithms/oops/PolymorphismDemo.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading