-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOops.java
More file actions
274 lines (246 loc) · 6.41 KB
/
Oops.java
File metadata and controls
274 lines (246 loc) · 6.41 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
public class Oops {
/* concept of oops
1. Class
2. Object
3. Inheritance
4. Polymorphism
5. Encapsulation
6. Abstraction
*/
// class and objects
public static void main(String args[] ){
//constructor
pen p1 =new pen();
//created a pen object called p1
p1.setColor("blue");
System.out.println(p1.getcolor());
p1.setTip(3);
System.out.println(p1.tip);
// inheritance
// Fish shark = new Fish();
// shark.eat();
//multilevel inheritance
Dogs dobby = new Dogs();
dobby.eat();
dobby.legs=4;
System.out.println(dobby.legs);
// polymorphism
Calculator calc = new Calculator();
System.out.println(calc.sum(3, 6));
System.out.println(calc.sum(1.45,5.73));
System.out.println(calc.sum(6,5));
System.out.println(calc.sum(9,8, 7));
Thar t = new Thar();
t.fuel();
//abstraction :
Horse h= new Horse();
h.khana();
h.sound();
System.out.println(h.color);
//hierarchy---animal_abstract-->horse-->horse ka child
Horse_ka_child myhorse = new Horse_ka_child();
Lion l =new Lion();
l.khana();
l.sound();
//interface:
Queen q = new Queen();
q.moves();
//static keyword
Student s1 = new Student();
s1.schoolName="GYANODAYA";
Student s2 = new Student();
System.out.println(s2.schoolName);
Student s3 = new Student();
s3.schoolName="convent";
}
}
//object
class pen{
//private
private String color;
//public
int tip;
//getter
String getcolor(){
return this.color;
}
int getTip(){
return this.tip;
}
//setter
void setColor(String newcolor){
color= newcolor;
}
void setTip(int newTip){
tip = newTip;
//this tip = tip;
}
}
class student{
String name ;
int age;
float percentage;//cgpa
void calcPercentage(int phy, int chem, int maths){
percentage =(phy+chem +maths)/3;
}
}
// inheritance
//type- 1.single level inheritance--->animal --fish
// 2. multilevel inheritance----->animal---mammles----dogs
// 3. hierarchial inheritance--->animal---fish,bird,mammles
// 4.hybrid inheritance---->animal-->fish, bird, mammal--->fish---tuna,shark--->>bird--- peacock, crow--->>mammal--dogs ,cats, human
// 5. Multiple inheritance---cats , dogs---->
//mammals although its not possible by class. we use interface for this inheritance
// base class
class Animal{
String color;
void eat(){
System.out.println("eats");
}
void breath(){
System.out.println("breath");
}
}
// derive class /subclass
// class Fish extends Animal{
// int fins;
// void swim(){
// System.out.println("swim");
// }
// }
class Mammals extends Animal{
int legs;
void walk(){
System.out.println("mammles can walks ");
}
}
class fish extends Animal{
int fins;
void walk(){
System.out.println("fish can swim ");
}
}
class bird extends Animal{
int feather;
void walk(){
System.out.println("birds can fly ");
}
}
class Dogs extends Mammals{
String breed;
}
// polymorphism - multiple form with a one form
//1. compile time -- method overloading(static)>>multiple function with the same name with different parameter.
class Calculator{
int sum (int a,int b){
return a+b;
}
double sum(double d, double e) {
return d+e;
}
float sum(float a, float b){
return a+b;
}
int sum(int a ,int b, int c){
return a+b+c;
}
}
//2. run time--------method overriding(dynamic)>>parent and child classes both contain the same function with a different definition.
class Car{
void fuel(){
System.out.println(" I required fuel for running");
}
}
class Thar extends Car{
void high_fuel(){
System.out.println(" I'm legend will use more fuel");
}
}
/* abstraction: Hiding all the unnecessary details and showing only the important parts to the user.
--->abstract classes ---->interface
>>>>cannot create an instance of abstract class
>>>>can have abstract/non-abstract methods.
>>>> can have constructors
*/
abstract class Animal_abstract{
String color;
//constructor
Animal_abstract(){//class
//color="brown";
System.out.println("animal_abstract construct callled");
}
void khana(){
System.out.println("animals are eating");
}
abstract void sound();
}
class Horse extends Animal_abstract{
//constructor
Horse(){
System.out.println("Horse constructor called");
}
void changeColor(){
color = "dark black";
}
void sound(){
System.out.println(" sound is produce by horse");
}
}
class Horse_ka_child extends Horse{
Horse_ka_child(){
System.out.println("horse ka child and constructor");
}
}
class Lion extends Animal_abstract{
void sound(){
System.out.println("sound is produce by lion ");
}
}
/*interface:interface is a blueprint of a class.
* blueprint(interface: feature of car like wheels ,speed,engine)>>>>blueprint(class: type of car suppose maruti800)>>>object:maruti800(car1,car2,car3....)
* Work of interfaces:
* all method are public abstract and without implementation.
* applying multiple inheritance
* to achieve total abstract class
* variables in the interface are final ,public and static.
*/
interface ChessPlayer{
void moves();
}
class Queen implements ChessPlayer{
public void moves(){
System.out.println("up, down ,right, diagonal(in the all four direction)");
}
}
class Rook implements ChessPlayer{
public void moves(){
System.out.println("Up, down, Left, Right");
}
}
class King implements ChessPlayer{
public void moves(){
System.out.println("up, down, left, right, diagonal (by 1 step)");
}
}
//
/*static keyword: fstatic keyword in java is used to share the same variable or methods of a given class
---properties
---function
---block
---nested classes
*/
class Student{
String name;
int roll;
static String schoolName;
void setName(String name){
this.name = name;
}
String getName(String name){
return this.name;
}
}
//super keyword:super keyword is us ed to refer immediate parent
//--access parents properties
//to access parent function
// to access parents constructor.