Skip to content

Commit d96bc07

Browse files
committed
OOP: Polymorphism
1 parent 97d5098 commit d96bc07

9 files changed

Lines changed: 228 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Sample class BankAccount which is used as the super class of CurrentAcoount and SavingsAccount.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public class BankAccount {
8+
private String name;
9+
private double balance;
10+
11+
public String getName() {
12+
return this.name;
13+
}
14+
15+
public void setName(String name) {
16+
this.name = name;
17+
}
18+
19+
public void deposite(double amount) {
20+
this.balance += amount;
21+
}
22+
23+
public boolean withdraw(double amount) {
24+
if (balance > amount) {
25+
balance -= amount;
26+
return true;
27+
} else {
28+
return false;
29+
}
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Testing class for BankAccounts.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public class BankDemo {
8+
public static void main(String[] args) {
9+
CurrentAccount c = new CurrentAccount();
10+
SavingsAccount s = new SavingsAccount();
11+
c.deposite(500.00);
12+
s.deposite(500.00);
13+
doWithdrawal(c); // Withdraw succeed.
14+
doWithdrawal(s); // Withdraw failed.
15+
}
16+
17+
public static void doWithdrawal(BankAccount acc) {
18+
boolean result = acc.withdraw(1000.00);
19+
if (result) {
20+
System.out.println("Withdraw succeed.");
21+
} else {
22+
System.out.println("Withdraw failed.");
23+
}
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Sample Calculator class to demonstrate Method Overloading.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public class CalculatorDemo {
8+
public static void main(String[] args) {
9+
Calculator calc = new Calculator();
10+
calc.addLong(10L, 30L);
11+
calc.addFloat(10.5f, 0.5f);
12+
}
13+
}
14+
15+
class Calculator {
16+
public void addLong(long x, long y) {
17+
long result = x + y;
18+
System.out.println("Long addition: " + result);
19+
}
20+
21+
public void addFloat(float x, float y) {
22+
float result = x + y;
23+
System.out.println("Float addition: " + result);
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Sample Calculator class to demonstrate Method Overloading.
3+
*
4+
* @version 2.0
5+
* @author L.Gobinath
6+
*/
7+
public class CalculatorDemo {
8+
public static void main(String[] args) {
9+
Calculator calc = new Calculator();
10+
calc.add(10L, 30L);
11+
calc.add(10.5f, 0.5f);
12+
}
13+
}
14+
15+
/**
16+
* Calculator class has two methods add(long, long) and add(float, float).
17+
*/
18+
class Calculator {
19+
public void add(long x, long y) {
20+
long result = x + y;
21+
System.out.println("Long addition: " + result);
22+
}
23+
24+
public void add(float x, float y) {
25+
float result = x + y;
26+
System.out.println("Float addition: " + result);
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Sample Calculator class to demonstrate Method Overloading.
3+
*
4+
* @version 3.0
5+
* @author L.Gobinath
6+
*/
7+
public class CalculatorDemo {
8+
public static void main(String[] args) {
9+
Calculator calc = new Calculator();
10+
// Passing two integer values
11+
calc.add(10, 30); // add(long, long)
12+
// Passing two long values
13+
calc.add(10L, 30L); // add(long, long)
14+
}
15+
}
16+
17+
/**
18+
* Calculator class has two methods add(long, long) and add(float, float).
19+
*/
20+
class Calculator {
21+
public void add(long x, long y) {
22+
long result = x + y;
23+
System.out.println("Long addition: " + result);
24+
}
25+
26+
public void add(float x, float y) {
27+
float result = x + y;
28+
System.out.println("Float addition: " + result);
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Sample Calculator class to demonstrate Method Overloading.
3+
*
4+
* @version 4.0
5+
* @author L.Gobinath
6+
*/
7+
public class CalculatorDemo {
8+
public static void main(String[] args) {
9+
Calculator calc = new Calculator();
10+
// Passing two integer values
11+
calc.add(10, 30); // add(int, int)
12+
// Passing two long values
13+
calc.add(10L, 30L); // add(long, long)
14+
}
15+
}
16+
17+
/**
18+
* Calculator class has two methods add(long, long) and add(int, int).
19+
*/
20+
class Calculator {
21+
public void add(long x, long y) {
22+
long result = x + y;
23+
System.out.println("Long addition: " + result);
24+
}
25+
26+
public void add(int x, int y) {
27+
int result = x + y;
28+
System.out.println("Integer addition: " + result);
29+
}
30+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Covariant return example.
3+
* Overriding method returns a subtype of overridden method's return type.
4+
*
5+
* @version 1.0
6+
* @author L.Gobinath
7+
*/
8+
public class CovariantReturn {
9+
public static void main(String[] args) {
10+
create(new CircleFactory());
11+
}
12+
13+
public static void create(ShapeFactory factory) {
14+
Shape shape = factory.getShape();
15+
// Circle circle = factory.getShape(); // Compile time error
16+
shape.draw(); // Circle
17+
}
18+
}
19+
20+
class Shape {
21+
public void draw() {
22+
System.out.println("Shape");
23+
}
24+
}
25+
class Circle extends Shape {
26+
public void draw() {
27+
System.out.println("Circle");
28+
}
29+
}
30+
31+
class ShapeFactory {
32+
public Shape getShape() {
33+
return new Shape();
34+
}
35+
}
36+
37+
class CircleFactory extends ShapeFactory {
38+
public Circle getShape() {
39+
return new Circle();
40+
}
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* CurrentAccount allows to have negative balance.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public class CurrentAccount extends BankAccount {
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* SavingsAccount does not have any significant differences.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public class SavingsAccount extends BankAccount {
8+
9+
}

0 commit comments

Comments
 (0)