From 429bcc7b3d9fb2d5a1e234ce2e3935af0496a0bf Mon Sep 17 00:00:00 2001 From: Oleksii Aleshchenko Date: Thu, 2 Mar 2023 15:26:42 +0200 Subject: [PATCH] add polymorphism example --- .../polymorphism/ElectricEngine.java | 13 +++++++++ .../polymorphism/Engine.java | 9 ++++++ .../polymorphism/FuelEngine.java | 9 ++++++ .../polymorphism/Main.java | 26 +++++++++++++++++ .../polymorphism/enhanced/ElectricEngine.java | 19 +++++++++++++ .../polymorphism/enhanced/Engine.java | 9 ++++++ .../polymorphism/enhanced/FuelEngine.java | 10 +++++++ .../polymorphism/enhanced/Main.java | 28 +++++++++++++++++++ 8 files changed, 123 insertions(+) create mode 100644 src/encapsulationInheritancePolymorphism/polymorphism/ElectricEngine.java create mode 100644 src/encapsulationInheritancePolymorphism/polymorphism/Engine.java create mode 100644 src/encapsulationInheritancePolymorphism/polymorphism/FuelEngine.java create mode 100644 src/encapsulationInheritancePolymorphism/polymorphism/Main.java create mode 100644 src/encapsulationInheritancePolymorphism/polymorphism/enhanced/ElectricEngine.java create mode 100644 src/encapsulationInheritancePolymorphism/polymorphism/enhanced/Engine.java create mode 100644 src/encapsulationInheritancePolymorphism/polymorphism/enhanced/FuelEngine.java create mode 100644 src/encapsulationInheritancePolymorphism/polymorphism/enhanced/Main.java diff --git a/src/encapsulationInheritancePolymorphism/polymorphism/ElectricEngine.java b/src/encapsulationInheritancePolymorphism/polymorphism/ElectricEngine.java new file mode 100644 index 0000000..17341fd --- /dev/null +++ b/src/encapsulationInheritancePolymorphism/polymorphism/ElectricEngine.java @@ -0,0 +1,13 @@ +package encapsulationInheritancePolymorphism.polymorphism; + +public class ElectricEngine extends Engine { + private String batteryType; + + public int getPower() { + return 20; + } + + /*private class Engine { + private int power; + }*/ +} diff --git a/src/encapsulationInheritancePolymorphism/polymorphism/Engine.java b/src/encapsulationInheritancePolymorphism/polymorphism/Engine.java new file mode 100644 index 0000000..074855b --- /dev/null +++ b/src/encapsulationInheritancePolymorphism/polymorphism/Engine.java @@ -0,0 +1,9 @@ +package encapsulationInheritancePolymorphism.polymorphism; + +public class Engine { + private int power; + + public int getPower() { + return power; + } +} diff --git a/src/encapsulationInheritancePolymorphism/polymorphism/FuelEngine.java b/src/encapsulationInheritancePolymorphism/polymorphism/FuelEngine.java new file mode 100644 index 0000000..e31177e --- /dev/null +++ b/src/encapsulationInheritancePolymorphism/polymorphism/FuelEngine.java @@ -0,0 +1,9 @@ +package encapsulationInheritancePolymorphism.polymorphism; + +public class FuelEngine extends Engine { + private String fuelType; + + public int getPower() { + return 50; + } +} diff --git a/src/encapsulationInheritancePolymorphism/polymorphism/Main.java b/src/encapsulationInheritancePolymorphism/polymorphism/Main.java new file mode 100644 index 0000000..a9ec705 --- /dev/null +++ b/src/encapsulationInheritancePolymorphism/polymorphism/Main.java @@ -0,0 +1,26 @@ +package encapsulationInheritancePolymorphism.polymorphism; + +/*import encapsulationInheritancePolymorphism.inheritance.ElectricEngine; +import encapsulationInheritancePolymorphism.inheritance.FuelEngine;*/ + +public class Main { + public static void main(String[] args) { + /*encapsulationInheritancePolymorphism.inheritance.*/ElectricEngine electricEngine = new ElectricEngine(); // створення нового об'єкту (екземпляру) класу ElectricEngine + /*encapsulationInheritancePolymorphism.inheritance.*/FuelEngine fuelEngine = new FuelEngine(); + + Engine engine1 = fuelEngine; + + Engine[] engines = { + electricEngine, + fuelEngine + }; + + for (Engine engine : engines) { + System.out.println(engine.getPower()); + } + + /*for (int i = 0; i < engines.length; i++) { + System.out.println(engines[i].getPower()); + }*/ + } +} diff --git a/src/encapsulationInheritancePolymorphism/polymorphism/enhanced/ElectricEngine.java b/src/encapsulationInheritancePolymorphism/polymorphism/enhanced/ElectricEngine.java new file mode 100644 index 0000000..1d6db51 --- /dev/null +++ b/src/encapsulationInheritancePolymorphism/polymorphism/enhanced/ElectricEngine.java @@ -0,0 +1,19 @@ +package encapsulationInheritancePolymorphism.polymorphism.enhanced; + +public class ElectricEngine extends Engine { + private String batteryType; + private int chargeLevel = 9; + private int criticalChargeLevel = 10; + private float coefficientCriticalPowerCut = 0.1f; + + @Override + public int getPower() { + return chargeLevel > criticalChargeLevel + ? super.getPower() + : (int) (super.getPower() * coefficientCriticalPowerCut); + } + + /*private class Engine { + private int power; + }*/ +} diff --git a/src/encapsulationInheritancePolymorphism/polymorphism/enhanced/Engine.java b/src/encapsulationInheritancePolymorphism/polymorphism/enhanced/Engine.java new file mode 100644 index 0000000..310f9ce --- /dev/null +++ b/src/encapsulationInheritancePolymorphism/polymorphism/enhanced/Engine.java @@ -0,0 +1,9 @@ +package encapsulationInheritancePolymorphism.polymorphism.enhanced; + +public class Engine { + private int power = 100; + + public int getPower() { + return power; + } +} diff --git a/src/encapsulationInheritancePolymorphism/polymorphism/enhanced/FuelEngine.java b/src/encapsulationInheritancePolymorphism/polymorphism/enhanced/FuelEngine.java new file mode 100644 index 0000000..ad07d84 --- /dev/null +++ b/src/encapsulationInheritancePolymorphism/polymorphism/enhanced/FuelEngine.java @@ -0,0 +1,10 @@ +package encapsulationInheritancePolymorphism.polymorphism.enhanced; + +public class FuelEngine extends Engine { + private String fuelType; + + /*public int getPower() { + return 50; + }*/ + +} diff --git a/src/encapsulationInheritancePolymorphism/polymorphism/enhanced/Main.java b/src/encapsulationInheritancePolymorphism/polymorphism/enhanced/Main.java new file mode 100644 index 0000000..dcec9b1 --- /dev/null +++ b/src/encapsulationInheritancePolymorphism/polymorphism/enhanced/Main.java @@ -0,0 +1,28 @@ +package encapsulationInheritancePolymorphism.polymorphism.enhanced; + +/*import encapsulationInheritancePolymorphism.inheritance.ElectricEngine; +import encapsulationInheritancePolymorphism.inheritance.FuelEngine;*/ + +public class Main { + public static void main(String[] args) { + /*encapsulationInheritancePolymorphism.inheritance.*/ + ElectricEngine electricEngine = new ElectricEngine(); // створення нового об'єкту (екземпляру) класу ElectricEngine + /*encapsulationInheritancePolymorphism.inheritance.*/ + FuelEngine fuelEngine = new FuelEngine(); + + Engine engine1 = fuelEngine; + + Engine[] engines = { + electricEngine, + fuelEngine + }; + + for (Engine engine : engines) { + System.out.println(engine.getPower()); + } + + /*for (int i = 0; i < engines.length; i++) { + System.out.println(engines[i].getPower()); + }*/ + } +}