-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOOPS_Concepts.java
160 lines (119 loc) · 3.24 KB
/
OOPS_Concepts.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
class Pen{
public int tip;
public String color;
private String password;
void setTip(int newTipSize){
this.tip = newTipSize; // this.tip refers the tip at the 1st line of the class
}
String getColor(){
return this.color;
}
void setPassword(String newPass){
this.password = newPass;
}
String getPassword(){
return this.password;
}
}
class Student {
int roll;
String name;
int[] marks;
Student (){
int[] marks = new int[3];
System.out.println("Constructor is called...");
}
Student (int s_roll){
int[] marks = new int[3];
this.roll = s_roll;
}
Student (String s_name){
int[] marks = new int[3];
this.name = s_name;
}
// //Shallow copy
// Student (Student s1){
// int[] marks = new int[3];
// this.marks = s1.marks;
//
// // Also see deep copy
// }
}
class Animals {
String type;
void eat(){
System.out.println("Eats.. > called from class Animals");
}
}
class Mammals extends Animals {
void give_birth(){
System.out.println("Reproduce... > called from class Mammals");
}
}
class Dogs extends Mammals {
String breed;
void bark(){
System.out.println("Bhow! Bhow!");
}
}
class Humans extends Animals {
void breath(){
System.out.println("Let's Breath!");
}
}
// Interfaces
interface chessPlayer{
public void moves();
}
class Queen implements chessPlayer{
public void moves() {
System.out.println("Moves of queen");
}
}
// Multiple inheritance
interface Herbivores{
public void eats_plant();
}
interface Carnivores{
public void eats_meat();
}
class Bear implements Herbivores, Carnivores{
public void eats_plant(){
System.out.println("Bear eat plants");
}
public void eats_meat(){
System.out.println("Bear eats meat");
}
}
public class OOPS_Concepts {
public static void main(String[] args) {
Pen p1 = new Pen(); // Default Constructors
p1.setTip(5);
System.out.println(p1.tip); // because the variable is public
// System.out.println(p1.password); // error because password is private
p1.setPassword("Hello");
System.out.println(p1.getPassword()); // get method can be used to access the private variable
// Non parametric constructors
Student s1 = new Student();
// Parameteric Constructors & Constructor overloading
Student s2 = new Student(210);
Student s3 = new Student("Parth");
// s1.marks[0] = 90;
// s1.marks[1] = 80;
// s1.marks[2] = 100;
// // Shallow copy
// Student shallow_student = new Student(s1);
// for (int i=0; i<3; i++){
// System.out.print(shallow_student.marks[i]);
// }
// Inheritance
Dogs shadow = new Dogs();
shadow.breed = "German Shepherd";
shadow.bark();
shadow.give_birth();
shadow.eat();
Bear b1 = new Bear();
b1.eats_meat();
b1.eats_plant();
}
}