diff options
| author | Kamal Wickramanayake <kamal@inbox.lk> | 2026-02-21 19:56:23 +0530 |
|---|---|---|
| committer | Kamal Wickramanayake <kamal@inbox.lk> | 2026-02-21 19:56:23 +0530 |
| commit | fab39307b045215fb465701009bb0d454788a4ca (patch) | |
| tree | be32da2848f862827b5c3552a3b60c32389fa35e | |
| parent | f0a5509cc57cbd6fc95728fb8566ce011d34cebd (diff) | |
Added simple OOP sample code.
35 files changed, 627 insertions, 0 deletions
diff --git a/oop/01-account/.gitignore b/oop/01-account/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/oop/01-account/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/oop/01-account/Account.java b/oop/01-account/Account.java new file mode 100644 index 0000000..03a9e9c --- /dev/null +++ b/oop/01-account/Account.java @@ -0,0 +1,26 @@ + +public class Account { + + /* Declaring fields - represent state */ + + int accountNumber; + double balance; + + + /* Declaring methods - represent operations */ + + public void withdraw(double amount) { + if (balance >= amount) { + balance = balance - amount; + } + } + + public void deposit(double amount) { + balance += amount; + } + + public double getBalance() { + return balance; + } + +} diff --git a/oop/01-account/AccountTest.java b/oop/01-account/AccountTest.java new file mode 100644 index 0000000..0409d86 --- /dev/null +++ b/oop/01-account/AccountTest.java @@ -0,0 +1,23 @@ + +public class AccountTest { + + /** + * @param args + */ + public static void main(String[] args) { + Account a1 = new Account(); // Instantiation of 'Account' class + Account a2 = new Account(); + + System.out.println(a1.getBalance()); + + a1.deposit(100.0); + System.out.println(a1.getBalance()); + + a1.withdraw(50.0); + System.out.println(a1.getBalance()); + + a2.deposit(100.0); + System.out.println(a2.getBalance()); + } + +} diff --git a/oop/02-non-static-methods/.gitignore b/oop/02-non-static-methods/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/oop/02-non-static-methods/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/oop/02-non-static-methods/Calculator.java b/oop/02-non-static-methods/Calculator.java new file mode 100644 index 0000000..ecc7234 --- /dev/null +++ b/oop/02-non-static-methods/Calculator.java @@ -0,0 +1,15 @@ + +public class Calculator { + + public int add(int a, int b) { + return a + b; + } + + public int subtract(int a, int b) { + return a - b; + } + + public double convertFromC2F(double cTemp) { + return cTemp*180/100 + 32.0; + } +} diff --git a/oop/02-non-static-methods/MyApp.java b/oop/02-non-static-methods/MyApp.java new file mode 100644 index 0000000..2ebdcae --- /dev/null +++ b/oop/02-non-static-methods/MyApp.java @@ -0,0 +1,13 @@ + +public class MyApp { + + /** + * @param args + */ + public static void main(String[] args) { + Calculator cal = new Calculator(); + + System.out.println(cal.convertFromC2F(100.0)); + } + +} diff --git a/oop/03-static-methods/.gitignore b/oop/03-static-methods/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/oop/03-static-methods/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/oop/03-static-methods/Calculator.java b/oop/03-static-methods/Calculator.java new file mode 100644 index 0000000..7ec2851 --- /dev/null +++ b/oop/03-static-methods/Calculator.java @@ -0,0 +1,29 @@ + +public class Calculator { + public static int x; // static (class) field + public int y; // instance field + + public static int add(int a, int b) { // static (class) method + return a + b; + } + + public static int subtract(int a, int b) { // static (class) method + return a - b; + } + + public static double convertFromC2F(double cTemp) { // static (class) method + return cTemp*180/100 + 32.0; + } + + public void setY(int y) { // instance method + this.y = y; + } + + public int getXxY() { // instance method + return x * y; + } + +// public static int getXxY2() { +// return x * y; +// } +} diff --git a/oop/03-static-methods/MyApp.java b/oop/03-static-methods/MyApp.java new file mode 100644 index 0000000..0137433 --- /dev/null +++ b/oop/03-static-methods/MyApp.java @@ -0,0 +1,25 @@ + +public class MyApp { + + /** + * @param args + */ + public static void main(String[] args) { + System.out.println(Calculator.convertFromC2F(100.0)); + + Calculator.x = 5; + + Calculator cal1 = new Calculator(); + cal1.setY(10); + System.out.println(cal1.getXxY()); // prints 50 + + Calculator cal2 = new Calculator(); + cal2.setY(11); + + Calculator.x = 6; + + System.out.println(cal1.getXxY()); // 60 + System.out.println(cal2.getXxY()); // 66 + } + +} diff --git a/oop/04-constructors/.gitignore b/oop/04-constructors/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/oop/04-constructors/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/oop/04-constructors/Account.java b/oop/04-constructors/Account.java new file mode 100644 index 0000000..82875be --- /dev/null +++ b/oop/04-constructors/Account.java @@ -0,0 +1,47 @@ + +public class Account { + + /* Declaring fields - represent state */ + + int accountNumber; + double balance; + + // No argument constructor + public Account() { + System.out.println("No argument constructor"); + } + + // A constructor with an argument + public Account(double initialBalance) { + balance = initialBalance; + System.out.println("Argument constructor"); + } + + /* Declaring methods - represent operations */ + + public void withdraw(double amount) { + if (balance >= amount) { + balance = balance - amount; + } + } + + // Overloaded method + public void deposit(double amount) { + balance += amount; + } + + // Overloaded method + public void deposit(int amount) { + balance += amount; + } + + + public double getBalance() { + return balance; + } + + +// public int getBalance() { +// return 1; +// } +} diff --git a/oop/04-constructors/AccountTest.java b/oop/04-constructors/AccountTest.java new file mode 100644 index 0000000..9774e84 --- /dev/null +++ b/oop/04-constructors/AccountTest.java @@ -0,0 +1,23 @@ + +public class AccountTest { + + /** + * @param args + */ + public static void main(String[] args) { + Account a1 = new Account(); // Instantiation of 'Account' class + Account a2 = new Account(10.0); + + System.out.println(a1.getBalance()); + + a1.deposit(100.0); + System.out.println(a1.getBalance()); + + a1.withdraw(50.0); + System.out.println(a1.getBalance()); + + a2.deposit(100.0); + System.out.println(a2.getBalance()); + } + +} diff --git a/oop/05-destructor/.gitignore b/oop/05-destructor/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/oop/05-destructor/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/oop/05-destructor/Account.java b/oop/05-destructor/Account.java new file mode 100644 index 0000000..a5baab7 --- /dev/null +++ b/oop/05-destructor/Account.java @@ -0,0 +1,33 @@ + +public class Account { + + /* Declaring fields - represent state */ + + int accountNumber; + double balance; + + /* Declaring methods - represent operations */ + + /* + * Here is a useless destructor just for testing. + */ + public void finalize() { + System.out.println("Object is being descarded"); + System.exit(0); + } + + public void withdraw(double amount) { + if (balance >= amount) { + balance = balance - amount; + } + } + + public void deposit(double amount) { + balance += amount; + } + + public double getBalance() { + return balance; + } + +} diff --git a/oop/05-destructor/AccountTest.java b/oop/05-destructor/AccountTest.java new file mode 100644 index 0000000..8c9f474 --- /dev/null +++ b/oop/05-destructor/AccountTest.java @@ -0,0 +1,19 @@ + +public class AccountTest { + + /** + * @param args + */ + public static void main(String[] args) { + Account a1 = new Account(); + //.... + //.... + a1 = new Account(); + + while (true) { + new Account(); + } + + } + +} diff --git a/oop/06-encapsulation/.gitignore b/oop/06-encapsulation/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/oop/06-encapsulation/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/oop/06-encapsulation/Account.java b/oop/06-encapsulation/Account.java new file mode 100644 index 0000000..55d99d0 --- /dev/null +++ b/oop/06-encapsulation/Account.java @@ -0,0 +1,29 @@ + +public class Account { + + /* Declaring fields - represent state */ + + private int accountNumber; + private double balance; + + + /* Declaring methods - represent operations */ + + public void withdraw(double amount) { + if (balance >= amount) { + balance = balance - amount; + } + } + + public void deposit(double amount) { + balance += amount; + } + + public double getBalance() { + return balance; + } + + public void setBalance(double newBalance) { + balance = newBalance; + } +} diff --git a/oop/06-encapsulation/AccountTest.java b/oop/06-encapsulation/AccountTest.java new file mode 100644 index 0000000..9242cea --- /dev/null +++ b/oop/06-encapsulation/AccountTest.java @@ -0,0 +1,16 @@ + +public class AccountTest { + + /** + * @param args + */ + public static void main(String[] args) { + Account a1 = new Account(); // Instantiation of 'Account' class + + a1.setBalance(200.0); + +// a1.deposit(100.0); +// a1.withdraw(10.0); + } + +} diff --git a/oop/07-inheritance/.gitignore b/oop/07-inheritance/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/oop/07-inheritance/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/oop/07-inheritance/Account.java b/oop/07-inheritance/Account.java new file mode 100644 index 0000000..91acb95 --- /dev/null +++ b/oop/07-inheritance/Account.java @@ -0,0 +1,24 @@ + +public abstract class Account { + + /* Declaring fields - represent state */ + protected int accountNumber; + protected double balance; + + public Account() { + /// + } + + /* Declaring methods - represent operations */ + + public abstract void withdraw(double amount); + + public void deposit(double amount) { + balance += amount; + } + + public double getBalance() { + return balance; + } + +} diff --git a/oop/07-inheritance/AccountTest.java b/oop/07-inheritance/AccountTest.java new file mode 100644 index 0000000..cc5056b --- /dev/null +++ b/oop/07-inheritance/AccountTest.java @@ -0,0 +1,22 @@ + +public class AccountTest { + + /** + * @param args + */ + public static void main(String[] args) { + SavingsAccount a1 = new SavingsAccount(); + + Account a3 = new SavingsAccount(); + + Account a2 = new CheckingAccount(); + + + + CheckingAccount a4 = new CheckingAccount(); + + +// CheckingAccount a5 = new SavingsAccount(); // not working + } + +} diff --git a/oop/07-inheritance/CheckingAccount.java b/oop/07-inheritance/CheckingAccount.java new file mode 100644 index 0000000..a7c19b8 --- /dev/null +++ b/oop/07-inheritance/CheckingAccount.java @@ -0,0 +1,20 @@ + +public class CheckingAccount extends Account { + + + protected double overdraftLimit; + + + public void withdraw(double amount) { + double finalBalance = balance - amount; + if (finalBalance < 0) { + if (-finalBalance <= overdraftLimit) { + balance = finalBalance; + } else { + // throw exception + } + } else { + balance = finalBalance; + } + } +} diff --git a/oop/07-inheritance/SavingsAccount.java b/oop/07-inheritance/SavingsAccount.java new file mode 100644 index 0000000..497fb3d --- /dev/null +++ b/oop/07-inheritance/SavingsAccount.java @@ -0,0 +1,9 @@ + +public class SavingsAccount extends Account { + + public void withdraw(double amount) { + if (balance >= amount) { + balance = balance - amount; + } + } +} diff --git a/oop/08-pass-by-value-reference/.gitignore b/oop/08-pass-by-value-reference/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/oop/08-pass-by-value-reference/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/oop/08-pass-by-value-reference/Customer.java b/oop/08-pass-by-value-reference/Customer.java new file mode 100644 index 0000000..d092a3d --- /dev/null +++ b/oop/08-pass-by-value-reference/Customer.java @@ -0,0 +1,12 @@ +public class Customer { + private int number; + + public int getNumber() { + return number; + } + + public void setNumber(int number) { + this.number = number; + } + +} diff --git a/oop/08-pass-by-value-reference/PassByReference.java b/oop/08-pass-by-value-reference/PassByReference.java new file mode 100644 index 0000000..8eb577a --- /dev/null +++ b/oop/08-pass-by-value-reference/PassByReference.java @@ -0,0 +1,19 @@ +public class PassByReference { + + /** + * @param args + */ + public static void main(String[] args) { + Customer c = new Customer(); + c.setNumber(5); + System.out.println(c.getNumber()); // 5 + methodX(c); + System.out.println(c.getNumber()); // ? + } + + public static void methodX(Customer c2) { + System.out.println(c2.getNumber()); // 5 + c2.setNumber(6); + System.out.println(c2.getNumber()); // 6 + } +} diff --git a/oop/08-pass-by-value-reference/PassByValue.java b/oop/08-pass-by-value-reference/PassByValue.java new file mode 100644 index 0000000..2500d03 --- /dev/null +++ b/oop/08-pass-by-value-reference/PassByValue.java @@ -0,0 +1,17 @@ +public class PassByValue { + + /** + * @param args + */ + public static void main(String[] args) { + int x = 4; + System.out.println(x); // 4 + method(x, 2); + System.out.println(x); // ? + } + + public static void method(int x, int b) { + x = x + 1; + System.out.println(x); // 5 + } +} diff --git a/oop/09-polymorphism/.gitignore b/oop/09-polymorphism/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/oop/09-polymorphism/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/oop/09-polymorphism/Customer.java b/oop/09-polymorphism/Customer.java new file mode 100644 index 0000000..56b0f7b --- /dev/null +++ b/oop/09-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/09-polymorphism/CustomerApp.java b/oop/09-polymorphism/CustomerApp.java new file mode 100644 index 0000000..79e1c31 --- /dev/null +++ b/oop/09-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/09-polymorphism/CustomerDetailsFormatter.java b/oop/09-polymorphism/CustomerDetailsFormatter.java new file mode 100644 index 0000000..d2640e0 --- /dev/null +++ b/oop/09-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/09-polymorphism/PremiumCustomer.java b/oop/09-polymorphism/PremiumCustomer.java new file mode 100644 index 0000000..a098af5 --- /dev/null +++ b/oop/09-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; + } + + +} diff --git a/oop/10-nested-classes/.gitignore b/oop/10-nested-classes/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/oop/10-nested-classes/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/oop/10-nested-classes/CreateInner.java b/oop/10-nested-classes/CreateInner.java new file mode 100644 index 0000000..ab3830e --- /dev/null +++ b/oop/10-nested-classes/CreateInner.java @@ -0,0 +1,16 @@ +public class CreateInner { + + public static void main(String[] args) { + MyOuter mo = new MyOuter(); + MyOuter.MyInner inner = mo.new MyInner(); + inner.seeOuter(); + } + + /* Here's another way (same thing!) + public static void main(String[] args) { + MyOuter.MyInner inner = new MyOuter().new MyInner(); + inner.seeOuter(); + } + */ + +} diff --git a/oop/10-nested-classes/MyOuter.java b/oop/10-nested-classes/MyOuter.java new file mode 100644 index 0000000..6821b98 --- /dev/null +++ b/oop/10-nested-classes/MyOuter.java @@ -0,0 +1,14 @@ +class MyOuter { + private int x = 7; + + public void makeInner() { + MyInner in = new MyInner(); + in.seeOuter(); + } + + class MyInner { + public void seeOuter() { + System.out.println("Outer x is " + x); + } + } +}
\ No newline at end of file |
