Skip to content

Commit bcb8a67

Browse files
committed
OOP: Abstraction
1 parent e4a1983 commit bcb8a67

17 files changed

Lines changed: 392 additions & 0 deletions
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* final and abstract cannot be used together.
3+
* WARNING:
4+
* Compile time error due to final abstract class and final abstract method.
5+
*
6+
* @author L.Gobinath
7+
*/
8+
public final abstract class AbstractWithFinal {
9+
public final abstract void doStuff();
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* private and abstract cannot be used together.
3+
* WARNING:
4+
* Compile time error due to private abstract method.
5+
*
6+
* @author L.Gobinath
7+
*/
8+
public abstract class AbstractWithPrivate {
9+
private abstract void doStuff();
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* static and abstract cannot be used together.
3+
* WARNING:
4+
* Compile time error due to static abstract method.
5+
*
6+
* @author L.Gobinath
7+
*/
8+
public abstract class AbstractWithStatic {
9+
public static abstract void doStuff();
10+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Sample project to explain the abstraction.
3+
* The abstract sub classes can decide on overriding.
4+
* If an abstract class overrides any abstract methods of their super class,
5+
* upcomping sub classes do not need to override those methods.
6+
*
7+
* @author L.Gobinath
8+
*/
9+
public class BankAccountDemo {
10+
public static void main(String[] args) {
11+
doStuff(new ChildrensSavingsAccount());
12+
doStuff(new GeneralSavingsAccount());
13+
doStuff(new CurrentAccount());
14+
}
15+
16+
public static void doStuff(Account acc) {
17+
System.out.println("Account name: " + acc.getName());
18+
System.out.println("Is cheque available: " + acc.isChequeAvailable());
19+
acc.interest();
20+
}
21+
}
22+
23+
/**
24+
* Abstract class Account with two abstract methods.
25+
*/
26+
abstract class Account {
27+
/**
28+
* Abstract method.
29+
*/
30+
public abstract void interest();
31+
32+
/**
33+
* Abstract method.
34+
*/
35+
public abstract boolean isChequeAvailable();
36+
37+
/**
38+
* Non-abstract method.
39+
* An abstract class can have any number of non-abstract methods.
40+
* @return The name of the account
41+
*/
42+
public String getName() {
43+
String accName = this.getClass().toString().replace("class ", "");
44+
return accName;
45+
}
46+
}
47+
48+
/**
49+
* Abstract sub class of Account.
50+
* Overrides only one abstract method: isChequeAvailable.
51+
* Sub classes do not need to override isChequeAvailable.
52+
*/
53+
abstract class SavingsAccount extends Account {
54+
@Override
55+
public boolean isChequeAvailable() {
56+
return false;
57+
}
58+
}
59+
60+
/**
61+
* Non-abstract sub class of SavingsAccount.
62+
* No need to override isChequeAvailable.
63+
* Must override the abstract method interest.
64+
*/
65+
class ChildrensSavingsAccount extends SavingsAccount {
66+
@Override
67+
public void interest() {
68+
System.out.println("Interest: 5.0%");
69+
}
70+
}
71+
72+
/**
73+
* Non-abstract sub class of SavingsAccount.
74+
* No need to override isChequeAvailable.
75+
* Must override the abstract method interest.
76+
*/
77+
class GeneralSavingsAccount extends SavingsAccount {
78+
@Override
79+
public void interest() {
80+
System.out.println("Interest: 4.0%");
81+
}
82+
}
83+
84+
/**
85+
* Non-abstract sub class of Account.
86+
* It must override both interest and isChequeAvailable.
87+
*/
88+
class CurrentAccount extends Account {
89+
@Override
90+
public void interest() {
91+
System.out.println("No interest");
92+
}
93+
94+
@Override
95+
public boolean isChequeAvailable() {
96+
return true;
97+
}
98+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Sample code used to explain Object Oriented Programming.
3+
* 1.0 - The orginal source code.
4+
* 1.1 - All the properties of Animals have been encapsulated.
5+
* 1.2 - Inheritance is applied by introducing a super class Animal.
6+
* 1.3 - The treatment logic has been moved to the Animals.
7+
*
8+
* @version 1.3
9+
* @author L.Gobinath
10+
*/
11+
public class DoctorDemo {
12+
public static void main(String[] args) {
13+
Cat cat = new Cat();
14+
Dog dog = new Dog();
15+
VeterinaryDoctor doctor = new VeterinaryDoctor();
16+
cat.setAge(4);
17+
dog.setAge(2);
18+
doctor.giveTreatment(cat);
19+
doctor.giveTreatment(dog);
20+
}
21+
}
22+
23+
class VeterinaryDoctor {
24+
/**
25+
* Give treatment to any Animals.
26+
* @param animal The animal.
27+
*/
28+
public void giveTreatment(Animal animal) {
29+
animal.treatment();
30+
}
31+
}
32+
/**
33+
* Super class Animal.
34+
*/
35+
class Animal {
36+
private int age;
37+
38+
public int getAge() {
39+
return this.age;
40+
}
41+
42+
public void setAge(int age) {
43+
this.age = age;
44+
}
45+
46+
public void treatment() {
47+
48+
}
49+
}
50+
51+
class Cat extends Animal {
52+
@Override
53+
public void treatment() {
54+
if (getAge() < 3) {
55+
System.out.println("Treatment C1");
56+
} else {
57+
System.out.println("Treatment C2");
58+
}
59+
}
60+
}
61+
62+
class Dog extends Animal {
63+
@Override
64+
public void treatment() {
65+
if (getAge() < 5) {
66+
System.out.println("Treatment D1");
67+
} else {
68+
System.out.println("Treatment D2");
69+
}
70+
}
71+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Sample Car class.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public class Car {
8+
public void drive() {
9+
System.out.println("Use steering wheel");
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Sample Motorbike class.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public class Motorbike {
8+
public void drive() {
9+
System.out.println("Use handlebars");
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Sample abstract Vehicle class.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public abstract class Vehicle {
8+
/**
9+
* Implementation of drive method is unknown for Vehicle.
10+
* Notice that the method is terminated with a semicolon.
11+
*/
12+
public abstract void drive();
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Testing class for Vehicle class.
3+
* WARNING:
4+
* This class gives compile time error.
5+
* Cannot instantiate the abstract class.
6+
*
7+
* @version 1.0
8+
* @author L.Gobinath
9+
*/
10+
public class VehicleDemo {
11+
public static void main(String[] args) {
12+
Vehicle vehicle = new Vehicle(); // Compile time error
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Car class is a subclass of Vehicle.
3+
*
4+
* @version 2.0
5+
* @author L.Gobinath
6+
*/
7+
public class Car extends Vehicle {
8+
/**
9+
* Car class is a non-abstract sub class of Vehicle.
10+
* Must override the drive method.
11+
*/
12+
public void drive() {
13+
System.out.println("Use steering wheel");
14+
}
15+
}

0 commit comments

Comments
 (0)