summaryrefslogtreecommitdiff
path: root/oop/05-polymorphism
diff options
context:
space:
mode:
authorKamal Wickramanayake <kamal@inbox.lk>2026-02-22 19:43:58 +0530
committerKamal Wickramanayake <kamal@inbox.lk>2026-02-22 19:43:58 +0530
commitbaaba4a281b7d54e6b5d7767989c303b854b6932 (patch)
tree16201b64e9c8f20d7fa394c715f4ca5df51e9e08 /oop/05-polymorphism
parentae4cf7c9f0a0b90d02166b43402e67b7d4ad4dca (diff)
Directory rename
Diffstat (limited to 'oop/05-polymorphism')
-rw-r--r--oop/05-polymorphism/.gitignore1
-rw-r--r--oop/05-polymorphism/Customer.java60
-rw-r--r--oop/05-polymorphism/CustomerApp.java46
-rw-r--r--oop/05-polymorphism/CustomerDetailsFormatter.java17
-rw-r--r--oop/05-polymorphism/PremiumCustomer.java43
5 files changed, 167 insertions, 0 deletions
diff --git a/oop/05-polymorphism/.gitignore b/oop/05-polymorphism/.gitignore
new file mode 100644
index 0000000..6b468b6
--- /dev/null
+++ b/oop/05-polymorphism/.gitignore
@@ -0,0 +1 @@
+*.class
diff --git a/oop/05-polymorphism/Customer.java b/oop/05-polymorphism/Customer.java
new file mode 100644
index 0000000..56b0f7b
--- /dev/null
+++ b/oop/05-polymorphism/Customer.java
@@ -0,0 +1,60 @@
+public class Customer {
+ private int customerNumber;
+ private String name;
+ private String address;
+ private String phone;
+ private String mobile;
+
+ public Customer() {
+ }
+
+ public Customer(int customerNumber, String name, String address,
+ String phone, String mobile) {
+ this.customerNumber = customerNumber;
+ this.name = name;
+ this.address = address;
+ this.phone = phone;
+ this.mobile = mobile;
+ }
+
+ public int getCustomerNumber() {
+ return customerNumber;
+ }
+
+ public void setCustomerNumber(int customerNumber) {
+ this.customerNumber = customerNumber;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getAddress() {
+ return address;
+ }
+
+ public void setAddress(String address) {
+ this.address = address;
+ }
+
+ public String getPhone() {
+ return phone;
+ }
+
+ public void setPhone(String phone) {
+ this.phone = phone;
+ }
+
+ public String getMobile() {
+ return mobile;
+ }
+
+ public void setMobile(String mobile) {
+ this.mobile = mobile;
+ }
+
+}
diff --git a/oop/05-polymorphism/CustomerApp.java b/oop/05-polymorphism/CustomerApp.java
new file mode 100644
index 0000000..79e1c31
--- /dev/null
+++ b/oop/05-polymorphism/CustomerApp.java
@@ -0,0 +1,46 @@
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+public class CustomerApp {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ List list = new LinkedList();
+
+ Customer c = new Customer(1, "Kamal", "Nugegoda", "123456", "654321");
+ list.add(c);
+
+ c = new Customer(2, "Nimal", "Colombo", "2234343", "2343433");
+ list.add(c);
+
+ c = new PremiumCustomer(1, "Sunil", "Kohuwala", "23443434", "9888234",
+ "a", "Give him whatever he ask");
+ list.add(c);
+
+ list.add(new Date());
+
+ Iterator itr = list.iterator();
+
+ CustomerDetailsFormatter formatter = new CustomerDetailsFormatter();
+
+ while (itr.hasNext()) {
+ Customer customer = (Customer) itr.next(); // Convert "java.lang.Object" to "Customer"
+ String formattedString = formatter.getFormattedDescription(customer);
+ System.out.println(formattedString);
+ }
+
+ /*
+ Account a1 = new SavingsAccount(); // Coercion (implicit conversion)
+ a1.accountClassMethod();
+
+ Account a1 = someMethod();
+ SavingsAccount sa = (SavingsAccount) a1; // Down casting
+ sa.savingsAccountClassMethod();
+ */
+ }
+}
diff --git a/oop/05-polymorphism/CustomerDetailsFormatter.java b/oop/05-polymorphism/CustomerDetailsFormatter.java
new file mode 100644
index 0000000..d2640e0
--- /dev/null
+++ b/oop/05-polymorphism/CustomerDetailsFormatter.java
@@ -0,0 +1,17 @@
+public class CustomerDetailsFormatter {
+
+ /**
+ * Format the customer object according to some needs
+ * @param c
+ * @return
+ */
+ public String getFormattedDescription(Customer c) {
+ String buffer = "Customer description: \n";
+ buffer += " Number: " + c.getCustomerNumber();
+ buffer += " Name: " + c.getName();
+ buffer += " Address: " + c.getAddress();
+ buffer += "\n";
+
+ return buffer;
+ }
+}
diff --git a/oop/05-polymorphism/PremiumCustomer.java b/oop/05-polymorphism/PremiumCustomer.java
new file mode 100644
index 0000000..a098af5
--- /dev/null
+++ b/oop/05-polymorphism/PremiumCustomer.java
@@ -0,0 +1,43 @@
+public class PremiumCustomer extends Customer {
+
+ private String category;
+ private String desction;
+
+
+ public PremiumCustomer() {
+ }
+
+ public PremiumCustomer(int customerNumber, String name, String address,
+ String phone, String mobile) {
+ super(customerNumber, name, address, phone, mobile);
+ }
+
+
+ public PremiumCustomer(int customerNumber, String name, String address,
+ String phone, String mobile, String category, String desction) {
+ super(customerNumber, name, address, phone, mobile);
+ this.category = category;
+ this.desction = desction;
+ }
+
+ public PremiumCustomer(String category, String desction) {
+ super();
+ this.category = category;
+ this.desction = desction;
+ }
+
+ public String getCategory() {
+ return category;
+ }
+ public void setCategory(String category) {
+ this.category = category;
+ }
+ public String getDesction() {
+ return desction;
+ }
+ public void setDesction(String desction) {
+ this.desction = desction;
+ }
+
+
+}