Skip to content

Commit 62a7c7e

Browse files
committed
OOP: Inheritance
1 parent b65ff2e commit 62a7c7e

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
*
7+
* @version 1.2
8+
* @author L.Gobinath
9+
*/
10+
public class DoctorDemo {
11+
public static void main(String[] args) {
12+
Cat cat = new Cat();
13+
Dog dog = new Dog();
14+
VeterinaryDoctor doctor = new VeterinaryDoctor();
15+
cat.setAge(4);
16+
dog.setAge(2);
17+
doctor.treatCat(cat);
18+
doctor.treatDog(dog);
19+
}
20+
}
21+
22+
class VeterinaryDoctor {
23+
/**
24+
* Give treatment to a cat.
25+
* @param cat The cat.
26+
*/
27+
public void treatCat(Cat cat) {
28+
if (cat.getAge() < 3) { // Changed to getAge()
29+
System.out.println("Treatment C1");
30+
} else {
31+
System.out.println("Treatment C2");
32+
}
33+
}
34+
35+
/**
36+
* Give treatment to a dog.
37+
* @param dog The dog.
38+
*/
39+
public void treatDog(Dog dog) {
40+
if (dog.getAge() < 5) { // Changed to getAge()
41+
System.out.println("Treatment D1");
42+
} else {
43+
System.out.println("Treatment D2");
44+
}
45+
}
46+
}
47+
/**
48+
* Super class Animal.
49+
*/
50+
class Animal {
51+
private int age;
52+
53+
public int getAge() {
54+
return this.age;
55+
}
56+
57+
public void setAge(int age) {
58+
this.age = age;
59+
}
60+
}
61+
62+
class Cat extends Animal {
63+
64+
}
65+
66+
class Dog extends Animal {
67+
68+
}

0 commit comments

Comments
 (0)