-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOOps example
More file actions
356 lines (302 loc) · 8.15 KB
/
OOps example
File metadata and controls
356 lines (302 loc) · 8.15 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
abstract class buildingStructure{
abstract void plan();
}
class tower1 extends buildingStructure{
@Override
void plan() {
System.out.println("Executing the plan for tower1");
}
}
public class abstractionDemo {
public static void main(String[] args) {
buildingStructure structure;
}
}
=================================================================
package inheritanceDemo;
class CEO{}
class DeputyManager extends CEO{}
class territoryManager extends DeputyManager{}
class staff extends territoryManager{
}
public class Hierarchical {
}
===========================================
package inheritanceDemo;
class A{
public A()
{
System.out.println("inside A constructor");
}
}
class B extends A{
public B() {
super();
System.out.println("inside B constructor");
}
}
class C extends B{
public C() {
System.out.println("inside C constructor");
}
}
public class multilevel {
public static void main(String[] args) {
C cobj=new C();
}
}
=====================================================
package inheritanceDemo;
interface furniture{
public void woodfurniture();
}
interface sofaChair{
public void sit();
}
class chair extends Base implements furniture,sofaChair{
@Override
public void woodfurniture() {
System.out.println("inside woodfurniture");
}
@Override
public void sit() {
System.out.println("inside the sit method");
}
public void dinningChair()
{
System.out.println("Inside dining chair...");
}
}
public class multipleInheritance {
public static void main(String[] args) {
chair chaoj=new chair();
chaoj.sit();
chaoj.dinningChair();
chaoj.woodfurniture();
}
}
==============================================
package inheritanceDemo;
class Base{
protected int id;
public void BaseMethod()
{ id=8;
System.out.println("base class id is" +id);
}
}
class child extends Base{
public void childmethod()
{
id=67;
System.out.println("child class id is" +id);
}
}
public class singleInheritance {
public static void main(String[] args) {
child obj=new child();
Base bobj=new Base();
obj.BaseMethod();
obj.childmethod();
}
}
=========================================================================
abstract class buildingStructure{
abstract void plan();
}
class tower1 extends buildingStructure{
@Override
void plan() {
System.out.println("Executing the plan for tower1");
}
}
public class abstractionDemo {
public static void main(String[] args) {
buildingStructure structure;
}
}
============================================================
public class constructorDemo {
String name="Sunita";
int x;
public constructorDemo()
{
System.out.println("Default constructor...");
}
public constructorDemo(int x,String n)
{
this.x=x;
this.name=n;
System.out.println("Hello inside the constructor..." + x + "" +n);
}
public static void main(String[] args) {
constructorDemo cst1=new constructorDemo();
// constructorDemo cst2=new constructorDemo(2);
constructorDemo cst3=new constructorDemo(4,"Nidhi");
constructorDemo cst4=new constructorDemo(5,"Neeta");
constructorDemo cst5=new constructorDemo(6,"Ritu");
constructorDemo s=new constructorDemo(22,"Deep");
System.out.println("before the assignment");
System.out.println(s.hashCode());
// System.out.println(t.hashCode());
System.out.println(s.x + s.name);
// System.out.println(t.x + t.name);
constructorDemo t=s;
System.out.println("after the assignment");
System.out.println(s.hashCode());
System.out.println(t.hashCode());
System.out.println(s.x + s.name);
System.out.println(t.x + t.name);
t=new constructorDemo(111,"Mittal");
System.out.println("after allocating memory for t..");
System.out.println(t.hashCode());
System.out.println(s.x + s.name);
System.out.println(t.x + t.name);
}
}
=================================================================
class Account{
int acno;
private double balance=7000.45;
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
private void display()
{
System.out.println(balance);
}
}
public class encapsulation {
public static void main(String[] args) {
Account acob=new Account();
//acob.display();
acob.setBalance(5000);
System.out.println(acob.getBalance());
}
}
===================================================================================
import java.util.Scanner;
class Book
{
int x;
int mfgid;
String barcode;
int noofpages;
String title;
String author;
double price;
public void Addbook_to_library()
{
Scanner sc=new Scanner(System.in);
System.out.println("do you want to add the books to library? yes or no");
String choice=sc.next();
if(choice.equals("yes")) {
System.out.println("Enter the book details, mfgid,barcode,noofpages,title,author,price");
System.out.println("Enter the mfid");
mfgid = sc.nextInt();
System.out.println("Enter the barcode");
barcode = sc.next();
System.out.println("Enter the noofpages");
noofpages = sc.nextInt();
System.out.println("Enter the title");
title=sc.next();
System.out.println("Enter the author");
author=sc.next();
System.out.println("Enter the price");
price=sc.nextDouble();
System.out.println("The Book details are ");
System.out.println(mfgid);
System.out.println(barcode);
System.out.println(noofpages);
System.out.println(title);
System.out.println(author);
System.out.println(price);
}
else{
System.out.println("no books to add");
}
}
public void listlibrarybook()
{
System.out.println(mfgid);
System.out.println(barcode);
System.out.println(noofpages);
System.out.println(title);
System.out.println(author);
System.out.println(price);
}
public void show(int x)
{
System.out.println(x);
}
public void addBook_to_Sale()
{
}
}
public class OOPsExample {
public static void main(String[] args) {
Book bobj=new Book();
bobj.Addbook_to_library();
bobj.listlibrarybook();
bobj.show(89);
System.out.println(bobj.x);
}
}
===========================================================
class Product
{
double price;
public Product()
{
}
public Product(double pr)
{
this.price=pr;
}
public void setPrice(double price)
{
System.out.println("price is:" + price);
}
}
public class passbyValue {
public static void changePrice(Product price)
{
price=new Product(555.45);
System.out.println(price.price);
}
public static void modifiedPrice(Product pr)
{
pr.setPrice(1000.33);
System.out.println(pr.price);
}
public static void main(String[] args) {
Product probj=new Product();
double p;
probj.setPrice(400.78);
passbyValue.changePrice(probj);
}
}
==============================================================
package tstJava;
public class staticDemo {
int x;
static int y;
public staticDemo()
{
x++;
System.out.println("value of nonstatic x is" + x);
y++;
System.out.println("value of static y is" + y);
}
public static void main(String[] args) {
staticDemo dmobj1=new staticDemo();
staticDemo dmobj2=new staticDemo();
//staticDemo dmobj3=new staticDemo();
//staticDemo dmobj4=new staticDemo();
System.out.println(dmobj1.x);
System.out.println(dmobj1.y);
}
}
======================================================================================================