-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpolymorphism_example.java
96 lines (74 loc) · 1.8 KB
/
polymorphism_example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package first_test;
class Cat{
public String name;
public int age;
//default constructor
public Cat() {
this.name = "İsim değeri girilmedi";
this.age = 0;
}
//params constructor
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
//getter
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
//setter
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void ioCat(){
System.out.println("Kedimizin adı: " + this.getName()
+ "\nKedimizin yaşı: " + this.getAge()
+ "\nKedimiz göz renkleri farklı değildir." + "\n");
}
}
class VanCat extends Cat{
private boolean eyeColor;
//params constructor
public VanCat(String name, int age, boolean eyeColor) {
super(name, age);
this.eyeColor = eyeColor;
}
//get eyeColor
public boolean getEyeColor() {
return this.eyeColor;
}
//set eyeColor
public void setEyeColor(boolean eyeColor) {
this.eyeColor = eyeColor;
}
public void ioCat() {
if(this.eyeColor == true) {
System.out.println("Kedimizin adı: " + this.getName()
+ "\nKedimizin yaşı: " + this.getAge()
+ "\nKedimiz göz renkleri farklıdır." + "\n");
}else {
System.out.println("Kedimizin adı: " + this.getName()
+ "\nKedimizin yaşı: " + this.getAge()
+ "\nKedimiz göz renkleri farklı değildir."
+ "\nNesne yanlış yerde tanımlanmıştır, lütfen ait olduğu sınıfı düzeltiniz." + "\n");
}
}
}
public class polymorphism_example {
public static void main(String[] args) {
// TODO polymorphism
Cat cat1, cat2, cat3;
cat1 = new Cat("Tarçın", 2);
cat2 = new VanCat("Mesir", 3, true);
cat3 = new VanCat("Macun", 4, false);
cat1.ioCat();
cat2.ioCat();
cat3.ioCat();
}
}