SlideShare a Scribd company logo
1 of 78
Download to read offline
JAVA PRACTICAL HANDBOOK 
1. Basic Java class 
class Ex1 
{ 
public static void main(String args[]) 
{ 
int width; 
int length; 
int height; 
width=5; 
length=12; 
height=3; 
int v; 
v=width*length*height; 
System.out.println("Volume is :"+v); 
} 
} 
Volume is :180 
JAVA PRACTICAL HANDBOOK Page 1
2. 
class Ex2 
{ 
public static void main(String args[]) 
{ 
int width; 
int length; 
int height; 
width=5; 
length=12; 
height=3; 
int v; 
v=width*length*height; 
System.out.println("Volume is :"+v); 
//Box 2 
int width1; 
int length1; 
int height1; 
width1=50; 
length1=120; 
height1=30; 
int v1; 
v1=width1*length1*height1; 
System.out.println("Volume is :"+v1); 
} 
} 
Volume is :180 
Volume is :180000 
JAVA PRACTICAL HANDBOOK Page 2
3. 
class Box 
{ 
int width; 
int length; 
int height; 
}c 
lass BoxDemo 
{ 
public static void main(String args[]) 
{ 
int x; 
x=100; 
System.out.println(x); 
Box b1; 
b1=new Box(); 
System.out.println(b1); 
b1.width=5; 
b1.length=12; 
b1.height=3; 
int v; 
v=b1.width*b1.length*b1.height; 
System.out.println("Volume is :"+v); 
} 
} 
100 
Box@119298d 
Volume is :180 
JAVA PRACTICAL HANDBOOK Page 3
4. 
class Box 
{ 
int width; 
int length; 
int height; 
} 
class BDemo 
{ 
public static void main(String args[]) 
{ 
Box b1=new Box(); 
b1.width=5; 
b1.length=12; 
b1.height=3; 
int v; 
v=b1.width*b1.length*b1.height; 
System.out.println("Volume is :"+v); 
//Box 2 
Box b2=new Box(); 
b2.width=50; 
b2.length=120; 
b2.height=30; 
int v1; 
v1=b2.width*b2.length*b2.height; 
System.out.println("Volume is :"+v1); 
} 
} 
Volume is :180 
Volume is :180000 
JAVA PRACTICAL HANDBOOK Page 4
5. Methods 
class Box 
{ 
//Variable declarations 
int width; 
int length; 
int height; 
//Creating a method to print Volume 
void printVolume() 
{ 
System.out.println("Volume is :"+(width*length*height)); 
} 
} 
class BDemo1 
{ 
public static void main(String args[]) 
{ 
Box b1=new Box(); 
b1.width=5; 
b1.length=12; 
b1.height=3; 
b1.printVolume(); 
//Box 2 
Box b2=new Box(); 
b2.width=50; 
b2.length=120; 
b2.height=30; 
b2.printVolume(); 
} 
} 
Volume is :180 
Volume is :180000 
JAVA PRACTICAL HANDBOOK Page 5
6. 
class Box 
{ 
int width; 
int length; 
int height; 
//Creating a method to print Volume 
void printVolume() 
{ 
int v; 
v=width*length*height; 
System.out.println("Volume is :"+v); 
} 
} 
class BDemo2 
{ 
public static void main(String args[]) 
{ 
Box b1=new Box(); 
b1.width=5; 
b1.length=12; 
b1.height=3; 
b1.printVolume(); 
//Box 2 
Box b2=new Box(); 
b2.width=50; 
b2.length=120; 
b2.height=30; 
b2.printVolume(); 
} 
} 
Volume is :180 
Volume is :180000 
JAVA PRACTICAL HANDBOOK Page 6
7. 
class Box 
{ 
int width; 
int length; 
int height; 
//Creating a method to print Volume 
void printVolume() 
{ 
int v; 
v=width*length*height; 
System.out.println("Volume is :"+v); 
}v 
oid setValues(int w, int l, int h) 
{ 
width=w; 
length=l; 
height=h; 
} 
} 
class BDemo3 
{ 
public static void main(String args[]) 
{ 
Box b1=new Box(); 
b1.setValues(5,12,3); 
b1.printVolume(); 
Box b2=new Box(); 
b2.setValues(50,120,30); 
b2.printVolume(); 
} 
} 
Volume is :180 
Volume is :180000 
JAVA PRACTICAL HANDBOOK Page 7
8. Methods with a return type 
class Box 
{ 
int width; 
int length; 
int height; 
//Creating a method to print Volume 
void printVolume() 
{ 
int v; 
v=width*length*height; 
System.out.println("Volume is :"+v); 
}v 
oid setValues(int w, int l, int h) 
{ 
width=w; 
length=l; 
height=h; 
}i 
nt getVolume() 
{ 
int v; 
v=width*length*height; 
return v; 
} 
}c 
lass BDemo4 
{ 
public static void main(String args[]) 
{ 
Box b1=new Box(); 
b1.setValues(5,12,3); 
int v1=b1.getVolume();//returs volume (int) 
v1+=100; 
System.out.println("Volume :"+v1); 
} 
} 
Volume :280 
JAVA PRACTICAL HANDBOOK Page 8
9. 
class A 
{ 
int tot(int i, int j) 
{ 
int k=i+j; 
System.out.println("tot :"+k); 
return k; 
} 
} 
class DemoA 
{ 
public static void main(String args[]) 
{ 
A a1=new A(); 
int t=a1.tot(10,20); 
t++; 
System.out.println("Tot now :"+t); 
} 
} 
tot :30 
Tot now :31 
JAVA PRACTICAL HANDBOOK Page 9
10. 
class A 
{ 
int tot(int i, int j) 
{ 
int k=i+j; 
System.out.println("tot :"+k); 
return k; 
} 
} 
class DemoA1 
{ 
public static void main(String args[]) 
{ 
A a1=new A(); 
a1.tot(10,20); 
} 
} 
tot :30 
JAVA PRACTICAL HANDBOOK Page 10
11. Default values of attributes 
class Box 
{ 
int width; 
int length; 
int height; 
//Creating a method to print Volume 
void printVolume() 
{ 
int v; 
v=width*length*height; 
System.out.println("Volume is :"+v); 
}v 
oid setValues(int w, int l, int h) 
{ 
width=w; 
length=l; 
height=h; 
}i 
nt getVolume() 
{ 
int v; 
v=width*length*height; 
return v; 
} 
}c 
lass BDemo5 
{ 
public static void main(String args[]) 
{ 
Box b1=new Box(); 
b1.printVolume(); 
} 
} 
Volume is :180 
Volume is :180000 
JAVA PRACTICAL HANDBOOK Page 11
12. 
class Box 
{ 
int width=10; 
int length=10; 
int height=10; 
//Creating a method to print Volume 
void printVolume() 
{ 
int v; 
v=width*length*height; 
System.out.println("Volume is :"+v); 
} 
void setValues(int w, int l, int h) 
{ 
width=w; 
length=l; 
height=h; 
} 
int getVolume() 
{ 
int v; 
v=width*length*height; 
return v; 
} 
} 
class BDemo6 
{ 
public static void main(String args[]) 
{ 
Box b1=new Box(); 
b1.printVolume(); 
} 
} 
Volume is :1000 
JAVA PRACTICAL HANDBOOK Page 12
13. Constructors 
class Box 
{ 
Box() 
{ 
System.out.println("a box is created.."); 
} 
}c 
lass BDemo7 
{ 
public static void main(String args[]) 
{ 
Box b1=new Box(); //Prints "a box is created.." calling Box 
constructor 
Box b2=new Box(); //Prints "a box is created.." 
} 
} 
a box is created.. 
a box is created.. 
JAVA PRACTICAL HANDBOOK Page 13
14. 
class Box 
{ 
int width=10; 
int length=10; 
int height=10; 
Box() 
{ 
width=2; 
length=2; 
height=2; 
}/ 
/Creating a method to print Volume 
void printVolume() 
{ 
int v; 
v=width*length*height; 
System.out.println("Volume is :"+v); 
} 
} 
class BDemo8 
{ 
public static void main(String args[]) 
{ 
Box b1=new Box(); 
b1.printVolume();//prints Volume is 8 calling Box 
Constructor 
} 
} 
Volume is :8 
JAVA PRACTICAL HANDBOOK Page 14
15. 
class Box 
{ 
int width; 
int length; 
int height; 
Box(int w, int l, int h) 
{ 
width=w; 
length=l; 
height=h; 
}/ 
/Creating a method to print Volume 
void printVolume() 
{ 
int v; 
v=width*length*height; 
System.out.println("Volume is :"+v); 
} 
}c 
lass BDemo9 
{ 
public static void main(String args[]) 
{ 
Box b1=new Box(5,12,3);//Calling constructor with parameters 
b1.printVolume(); 
Box b2=new Box(50,120,30); 
b2.printVolume(); 
} 
} 
Volume is :180 
Volume is :180000 
JAVA PRACTICAL HANDBOOK Page 15
16. Method Overloading 
class Box 
{ 
int width; 
int length; 
int height; 
//Creating a method to print Volume 
void printVolume() 
{ 
int v; 
v=width*length*height; 
System.out.println("Volume is :"+v); 
}v 
oid setValues(int w, int l, int h) 
{ 
width=w; 
length=l; 
height=h; 
}v 
oid setValues(int l) 
{ 
width=l; 
length=l; 
height=l; 
} 
}c 
lass BDemo10 
{ 
public static void main(String args[]) 
{ 
Box b1=new Box(); 
b1.setValues(2,3,6); 
b1.printVolume(); 
Box b2=new Box(); 
b2.setValues(5);//Call overloaded method "setValues(int)" 
b2.printVolume(); 
} 
} 
Volume is :36 
Volume is :125 
JAVA PRACTICAL HANDBOOK Page 16
17. Example for "this" 
class Black 
{ 
int a; 
void setA(int a) 
{ 
a=a; //call "this.a=a" 
}v 
oid setB(int a) 
{ 
this.a=a; 
} 
}c 
lass DemoBlack 
{ 
public static void main(String args[]) 
{ 
Black a1=new Black(); 
a1.setA(10); 
System.out.println("Without this "+a1.a); 
a1.setB(10); 
System.out.println("Using this "+a1.a); 
} 
} 
Without this 0 
Using this 10 
JAVA PRACTICAL HANDBOOK Page 17
18. Primitive VS Ref. Variables 
class Box 
{ 
int length; 
int width; 
int height; 
Box b1; 
}c 
lass BDemo11 
{ 
public static void main(String args[]) 
{ 
Box ob1=new Box(); 
System.out.println(ob1.length); 
System.out.println(ob1.b1); 
ob1.length=12; 
ob1.width=5; 
ob1.height=3; 
ob1.b1=new Box(); 
System.out.println(ob1.length); 
System.out.println(ob1.b1); 
} 
} 
0 null 
12 
Box@f72617 
JAVA PRACTICAL HANDBOOK Page 18
19. 
class Box 
{ 
int length; 
int width; 
int height; 
Box b1; 
Box() 
{ 
length=1; 
width=1; 
height=1; 
b1=this; 
}v 
oid setValues(int length, int width, int height) 
{ 
b1.length=length;//this.length=length; 
} 
}c 
lass BDemo12 
{ 
public static void main(String args[]) 
{ 
Box ob1=new Box(); 
System.out.println(ob1.length); 
System.out.println(ob1.b1); 
} 
} 
1 
Box@119298d 
JAVA PRACTICAL HANDBOOK Page 19
20. 
class A 
{ 
int a; 
X x1; 
Y y1; 
Z z1; 
}c 
lass BDemo13 
{ 
public static void main(String args[]) 
{ 
A a1=new A(); 
System.out.println(a1.a); 
System.out.println(a1.x1); 
System.out.println(a1.y1); 
System.out.println(a1.z1); 
System.out.println("********"); 
a1.a=100; 
a1.x1=new X(); 
a1.y1=new Y(); 
System.out.println(a1.a); 
System.out.println(a1.x1); 
System.out.println(a1.y1); 
System.out.println(a1.z1); 
} 
}c 
lass X{} 
class Y{} 
class Z{} 
0 null 
null 
null 
******** 
100 
X@1fc4bec 
Y@dc8569 
null 
JAVA PRACTICAL HANDBOOK Page 20
21. Call By Values 
class ADemo2 
{ 
public static void main(String args[]) 
{ 
int x=10; 
System.out.println("X :"+x); 
A a1=new A(); 
a1.incr(x); 
System.out.println("X :"+x); 
} 
}c 
lass A 
{ 
void incr(int x) 
{ 
x++; 
System.out.println("X :"+x); 
} 
} 
X :10 
X :11 
X :10 
JAVA PRACTICAL HANDBOOK Page 21
22. Call By Ref 
class BDemo14 
{ 
public static void main(String args[]) 
{ 
V v1=new V(); 
System.out.println("X :"+v1.x); 
A a1=new A(); 
a1.incr(v1); 
System.out.println("X :"+v1.x); 
} 
}c 
lass A 
{ 
void incr(V v1) 
{ 
v1.x++; 
System.out.println("X :"+v1.x); 
} 
}c 
lass V 
{ 
int x=10; 
} 
X :10 
X :11 
X :11 
JAVA PRACTICAL HANDBOOK Page 22
23. Call by values 
class BDemo15 
{ 
public static void main(String args[]) 
{ 
int x=10, y=20; 
System.out.println(x+" "+y); 
A a1=new A(); 
a1.change(x,y); 
System.out.println(x+" "+y); 
} 
}c 
lass A 
{ 
void change(int x, int y) 
{ 
int z; 
z=x; 
x=y; 
y=z; 
System.out.println(x+" "+y); 
} 
} 
10 20 
20 10 
10 20 
JAVA PRACTICAL HANDBOOK Page 23
24. Static variables 
class A 
{ 
int a; 
int b; 
} 
class BDemo16 
{ 
public static void main(String args[]) 
{ 
A a1=new A(); 
a1.a=10; 
a1.b=20; 
System.out.println("Object a1 a :"+a1.a+" b :"+a1.b); 
A a2=new A(); 
a2.a=100; 
a2.b=200; 
System.out.println("Object a1 a :"+a1.a+" b :"+a1.b); 
} 
} 
Object a1 a :10 b :20 
Object a1 a :10 b :20 
JAVA PRACTICAL HANDBOOK Page 24
25. 
class A 
{ 
int a; 
static int b; 
} 
class BDemo17 
{ 
public static void main(String args[]) 
{ 
A a1=new A(); 
a1.a=10; 
a1.b=20; 
System.out.println("Object a1 a :"+a1.a+" b :"+a1.b); 
A a2=new A(); 
a2.a=100; 
a2.b=200; 
System.out.println("Object a1 a :"+a1.a+" b :"+a1.b); 
} 
} 
Object a1 a :10 b :20 
Object a1 a :10 b :200 
JAVA PRACTICAL HANDBOOK Page 25
26. 
class A 
{ 
int a; 
static int b; 
} 
class BDemo18 
{ 
public static void main(String args[]) 
{ 
A a1=new A(); 
a1.a=10; 
a1.b=20; 
System.out.println("Object a1 a :"+a1.a+" b :"+a1.b); 
A a2=new A(); 
a2.a=100; 
a2.b=200; 
System.out.println("Object a1 a :"+a1.a+" b :"+a1.b); 
System.out.println("Object a2 a :"+a2.a+" b :"+a2.b); 
a1.a=10000; 
a1.b=20000; 
System.out.println("Object a2 a :"+a2.a+" b :"+a2.b); 
A a3=new A(); 
System.out.println("Object a3 a :"+a3.a+" b :"+a3.b); 
A.b=2000000;// 
System.out.println("a1.b :"+a1.b+" a2.b :"+a2.b+" a3.b 
:"+a3.b); 
} 
} 
Object a1 a :10 b :20 
Object a1 a :10 b :200 
Object a2 a :100 b :200 
Object a2 a :100 b :20000 
Object a3 a :0 b :20000 
a1.b :2000000 a2.b :2000000 a3.b :2000000 
JAVA PRACTICAL HANDBOOK Page 26
27. Static methods 
class A 
{ 
void m1() 
{ 
System.out.println("m1"); 
}s 
tatic void m2() 
{ 
System.out.println("m2"); 
} 
} 
class BDemo19 
{ 
public static void main(String args[]) 
{ 
A ob=new A(); 
ob.m1(); 
A.m2();// without creating object can call method 
} 
} 
m1 
m2 
JAVA PRACTICAL HANDBOOK Page 27
28. 
class A 
{ 
int x=10; 
static int y=20; 
void m1() 
{ 
System.out.println(x); 
System.out.println(y); 
} 
} 
class BDemo20 
{ 
public static void main(String args[]) 
{ 
new A().m1(); 
} 
} 
10 
20 
JAVA PRACTICAL HANDBOOK Page 28
29. 
class A 
{ 
int x=10; 
static int y=20; 
void m1() 
{ 
System.out.println(this.x); 
System.out.println(this.y); 
} 
} 
class BDemo21 
{ 
public static void main(String args[]) 
{ 
new A().m1(); 
} 
} 
10 
20 
JAVA PRACTICAL HANDBOOK Page 29
30. Object / static initializer 
class A 
{ 
static 
{ 
System.out.print("1 "); 
}{ 
System.out.print("2 "); 
}A 
() 
{ 
System.out.print("3 "); 
}v 
oid m1() 
{ 
System.out.print("4 "); 
}s 
tatic void m2() 
{ 
System.out.print("5 "); 
} 
} 
class BDemo22 
{ 
public static void main(String args[]) 
{ 
new A(); 
A.m2(); 
} 
} 
1 2 3 5 
JAVA PRACTICAL HANDBOOK Page 30
31. 
class A 
{ 
int a; 
void printA() 
{ 
System.out.println("a :"+a); 
} 
}c 
lass B extends A 
{ 
int b; 
void printB() 
{ 
System.out.println("b :"+b); 
}v 
oid printAB() 
{ 
System.out.println("a+b :"+(a+b)); 
}v 
oid callAB() 
{ 
printA(); 
printB(); 
} 
}c 
lass BDemo23 
{ 
public static void main(String args[]) 
{ 
B b1=new B(); 
b1.a=10; 
b1.b=20; 
b1.printA(); 
b1.printB(); 
b1.printAB(); 
b1.callAB(); 
} 
} 
a :10 
b :20 
a+b :30 
a :10 
b :20 
JAVA PRACTICAL HANDBOOK Page 31
32. super class Constructor 
class A 
{ 
int a; 
A() 
{ 
System.out.println("A()"); 
}A 
(int i) 
{ 
System.out.println("A(int)"); 
}v 
oid printA() 
{ 
System.out.println("a :"+a); 
} 
}c 
lass B extends A 
{ 
int b; 
B() 
{ 
System.out.println("B()"); 
}B 
(int i) 
{ 
System.out.println("B(int)"); 
}v 
oid printB() 
{ 
System.out.println("b :"+b); 
}v 
oid printAB() 
{ 
System.out.println("a+b :"+(a+b)); 
}v 
oid callAB() 
{ 
printA(); 
printB(); 
} 
}c 
lass BDemo24 
{ 
public static void main(String args[]) 
{ 
B b1=new B(); 
B b2=new B(100); 
} 
JAVA PRACTICAL HANDBOOK Page 32
} 
A() 
B() 
A() 
B(int) 
33. 
class A 
{ 
int a; 
A() 
{ 
System.out.println("A()"); 
}A 
(int i) 
{ 
System.out.println("A(int)"); 
}v 
oid printA() 
{ 
System.out.println("a :"+a); 
} 
} 
class B extends A 
{ 
int b; 
B() 
{ 
super(); 
System.out.println("B()"); 
}B 
(int i) 
{ 
super(); 
System.out.println("B(int)"); 
}v 
oid printB() 
{ 
System.out.println("b :"+b); 
}v 
oid printAB() 
{ 
System.out.println("a+b :"+(a+b)); 
}void callAB(){ 
JAVA PRACTICAL HANDBOOK Page 33
printA(); 
printB(); 
} 
}c 
lass BDemo25 
{ 
public static void main(String args[]) 
{ 
B b1=new B(); 
B b2=new B(100); 
} 
} 
A() 
B() 
A() 
B(int) 
JAVA PRACTICAL HANDBOOK Page 34
34. 
class A 
{ 
int a; 
A() 
{ 
System.out.println("A()"); 
}A 
(int i) 
{ 
System.out.println("A(int)"); 
}v 
oid printA() 
{ 
System.out.println("a :"+a); 
} 
}c 
lass B extends A 
{ 
int b; 
B() 
{ 
super();// 
System.out.println("B()"); 
}B 
(int i) 
{ 
super(i);//super(100); 
System.out.println("B(int)"); 
}v 
oid printB() 
{ 
System.out.println("b :"+b); 
}v 
oid printAB() 
{ 
System.out.println("a+b :"+(a+b)); 
}v 
oid callAB() 
{ 
printA(); 
printB(); 
} 
}c 
lass BDemo26 
{ 
public static void main(String args[]) 
{ 
B b1=new B(); 
JAVA PRACTICAL HANDBOOK Page 35
B b2=new B(100); 
} 
} 
A() 
B() 
A(int) 
B(int) 
JAVA PRACTICAL HANDBOOK Page 36
35. 
class A 
{ 
int a; 
A() 
{ 
System.out.println("A()"); 
}A 
(int i) 
{ 
System.out.println("A(int)"); 
}v 
oid printA() 
{ 
System.out.println("a :"+a); 
} 
}c 
lass B extends A 
{ 
int b; 
B() 
{ 
super();// 
System.out.println("B()"); 
}B 
(int i) 
{ 
super(i);//super(100); 
System.out.println("B(int)"); 
}v 
oid printB() 
{ 
System.out.println("b :"+b); 
}v 
oid printAB() 
{ 
System.out.println("a+b :"+(a+b)); 
}v 
oid callAB() 
{ 
printA(); 
printB(); 
} 
}c 
lass BDemo27 
{ 
public static void main(String args[]) 
{ 
A a1; 
JAVA PRACTICAL HANDBOOK Page 37
a1=new B(); 
//B b1=new A();//Error 
a1.a=10; 
// a1.b=20; Error 
a1.printA(); 
/* a1.printB();//Error 
a1.printAB();//Error 
a1.callAB();//Error */ 
} 
} 
A() 
B() 
a :10 
JAVA PRACTICAL HANDBOOK Page 38
36. Method Overriding 
class A 
{ 
int a; 
void printA() 
{ 
System.out.println("a :"+a); 
}v 
oid print() 
{ 
System.out.println("A.print"); 
} 
} 
class B extends A 
{ 
int b; 
void printB() 
{ 
System.out.println("b :"+b); 
}v 
oid printAB() 
{ 
System.out.println("a+b :"+(a+b)); 
}v 
oid callAB() 
{ 
printA(); 
printB(); 
}v 
oid print() 
{ 
System.out.println("B.print"); 
} 
} 
class BDemo28 
{ 
public static void main(String args[]) 
{ 
A a1=new B(); 
a1.print(); 
} 
} 
B.print 
JAVA PRACTICAL HANDBOOK Page 39
37. 
class Vehicle 
{ 
void park() 
{} 
}c 
lass Car extends Vehicle 
{ 
void park() 
{ 
System.out.println("Car park"); 
} 
}c 
lass Bus extends Vehicle 
{ 
void park() 
{ 
System.out.println("Bus park"); 
} 
}c 
lass MB extends Vehicle 
{ 
void park() 
{ 
System.out.println("MB park"); 
} 
}c 
lass BDemo29 
{ 
public static void main(String argsp[]) 
{ 
Vehicle ob; 
ob=new Car(); 
ob.park(); //print "Car park" 
ob=new Bus(); 
ob.park(); //print "Bus park" 
ob=new MB(); 
ob.park(); //print "MB park" 
} 
} 
Car park 
Bus park 
MB park 
JAVA PRACTICAL HANDBOOK Page 40
38. 
class A 
{ 
static 
{ 
System.out.print("1 "); 
}{ 
System.out.print("2 "); 
}A 
() 
{ 
System.out.print("3 "); 
}s 
tatic void mA() 
{ 
System.out.print("4 "); 
} 
}c 
lass B extends A 
{ 
static 
{ 
System.out.print("5 "); 
}{ 
System.out.print("6 "); 
}B 
() 
{ 
System.out.print("7 "); 
}s 
tatic void mB() 
{ 
System.out.print("8 "); 
} 
}c 
lass BDemo30 
{ 
public static void main(String args[]) 
{ 
new B(); 
} 
} 
1 5 2 3 6 7 
JAVA PRACTICAL HANDBOOK Page 41
39. 
class A 
{ 
static 
{ 
System.out.print("1 "); 
}{ 
System.out.print("2 "); 
}A 
() 
{ 
System.out.print("3 "); 
}s 
tatic void mA() 
{ 
System.out.print("4 "); 
} 
}c 
lass B extends A 
{ 
static 
{ 
System.out.print("5 "); 
}{ 
System.out.print("6 "); 
}B 
() 
{ 
System.out.print("7 "); 
}s 
tatic void mB() 
{ 
System.out.print("8 "); 
} 
}c 
lass BDemo31 
{ 
public static void main(String args[]) 
{ 
B.mA(); //Prints 1 4 
} 
} 
1 4 
JAVA PRACTICAL HANDBOOK Page 42
40. Way of invoking "super()" and "this()" 
class A 
{ 
A(int i) 
{ 
System.out.println("A(int) :"+i); 
} 
}c 
lass B extends A 
{ 
B(int i) 
{ 
super(i); 
System.out.println("B(int) :"+i); 
} 
}c 
lass BDemo32 
{ 
public static void main(String args[]) 
{ 
new B(100); 
} 
} 
A(int) :100 
B(int) :100 
JAVA PRACTICAL HANDBOOK Page 43
41. 
class A 
{ 
A(int i) 
{ 
System.out.println("A(int) :"+i); 
}A 
(int i, int j) 
{ 
System.out.println("A(int,int) :"+i+" "+j); 
} 
}c 
lass B extends A 
{ 
B(int i) 
{ 
super(100); 
System.out.println("B(int) :"+i); 
}B 
(int i, int j ) 
{ 
super(i, j); 
System.out.println("B(int, int) :"+i+" "+j); 
} 
}c 
lass BDemo33 
{ 
public static void main(String args[]) 
{ 
new B(100,200); 
} 
} 
A(int,int) :100 200 
B(int, int) :100 200 
JAVA PRACTICAL HANDBOOK Page 44
42. "calling this" 
class A 
{ 
int a=100; 
A() 
{ 
this(1); 
}A 
(int i) 
{ 
a=i; 
} 
}c 
lass B extends A 
{ 
int b=200; 
B(){ 
this(1); 
}B 
(int i) 
{ 
super(i); 
b=i; 
}B 
(int i, int j) 
{ 
super(i); 
b=j;// 
}v 
oid printAB() 
{ 
System.out.println("a b :"+a+" "+b); 
} 
}c 
lass BDemo34 
{ 
public static void main(String args[]) 
{ 
B b1=new B(); 
b1.printAB(); 
B b2=new B(10); 
b2.printAB(); 
B b3=new B(100,200); 
b3.printAB(); 
} 
} 
JAVA PRACTICAL HANDBOOK Page 45
a b :1 1 
a b :10 10 
a b :100 200 
JAVA PRACTICAL HANDBOOK Page 46
43. "this" VS "super" 
class A 
{ 
int a=100; 
A() 
{ 
this(1); 
}A 
(int i) 
{ 
a=i; 
} 
}c 
lass B extends A 
{ 
int b=200; 
B() 
{ 
this(1); 
}B 
(int i) 
{ 
super(i); 
b=i; 
}B 
(int i, int j) 
{ 
super(i); 
b=j;// 
}v 
oid printAB() 
{ 
System.out.println("a b :"+a+" "+b); 
} 
}c 
lass BDemo35 
{ 
public static void main(String args[]) 
{ 
B b1=new B(); 
b1.printAB(); 
B b2=new B(10); 
b2.printAB(); 
B b3=new B(100,200); 
b3.printAB(); 
} 
} 
JAVA PRACTICAL HANDBOOK Page 47
a b :1 1 
a b :10 10 
a b :100 200 
JAVA PRACTICAL HANDBOOK Page 48
44. 
class A 
{ 
void print() 
{ 
System.out.println("A.print"); 
} 
}c 
lass B extends A 
{ 
void print() 
{ 
System.out.println("B.print"); 
} 
}c 
lass BDemo36 
{ 
public static void main(String args[]){ 
B b1=new B(); 
b1.print(); //prints "B.print" 
} 
} 
B.print 
JAVA PRACTICAL HANDBOOK Page 49
45. 
class A 
{ 
void print() 
{ 
System.out.println("A.print"); 
} 
}c 
lass B extends A 
{ 
void print() 
{ 
System.out.println("B.print"); 
} 
}c 
lass BDemo37 
{ 
public static void main(String args[]) 
{ 
A a1=new B(); 
a1.print();//print "B.print" 
} 
} 
B.print 
JAVA PRACTICAL HANDBOOK Page 50
46. 
class A 
{ 
int a=100; 
}c 
lass B extends A 
{ 
int a=200; 
}c 
lass BDemo38 
{ 
public static void main(String args[]) 
{ 
B b1=new B(); 
int x=b1.a; 
System.out.println(x);//print "200" 
} 
} 
200 
JAVA PRACTICAL HANDBOOK Page 51
47. 
class A 
{ 
int a=100; 
}c 
lass B extends A 
{ 
int a=200; 
}c 
lass BDemo39 
{ 
public static void main(String args[]) 
{ 
A a1=new B(); 
int x=a1.a; 
System.out.println(x);//print "100" 
} 
} 
100 
JAVA PRACTICAL HANDBOOK Page 52
48. 
class A 
{ 
int a=100; 
}c 
lass B extends A 
{ 
int a=200; 
void m() 
{ 
System.out.println("a :"+a); 
} 
}c 
lass BDemo40 
{ 
public static void main(String args[]) 
{ 
B b1=new B(); 
b1.m(); //prints "a :200" 
} 
} 
200 
JAVA PRACTICAL HANDBOOK Page 53
49. super.a 
class A 
{ 
int a=100; 
}c 
lass B extends A 
{ 
int a=200; 
void m() 
{ 
System.out.println("a :"+super.a); 
} 
}c 
lass BDemo41 
{ 
public static void main(String args[]) 
{ 
B b1=new B(); 
b1.m(); 
} 
} 
a :100 
50. 
class A 
{ 
int a=100; 
}c 
lass B extends A 
{ 
int a=200; 
}c 
lass C extends B 
{ 
int a=300; 
void m() 
{ 
System.out.println("a :"+a); 
} 
}c 
lass BDemo42 
{ 
public static void main(String args[]) 
{ 
C c1=new C(); 
c1.m(); 
} 
JAVA PRACTICAL HANDBOOK Page 54
} 
a :300 
JAVA PRACTICAL HANDBOOK Page 55
51. 
class A 
{ 
void print() 
{ 
System.out.println("A.print"); 
} 
}c 
lass B extends A 
{ 
void print() 
{ 
System.out.println("B.print"); 
} 
}c 
lass C extends B 
{ 
void print() 
{ 
System.out.println("C.print"); 
}v 
oid m() 
{ 
print(); 
} 
}c 
lass BDemo43 
{ 
public static void main(String args[]) 
{ 
C c1=new C(); 
c1.m(); 
} 
} 
C.print 
JAVA PRACTICAL HANDBOOK Page 56
52. 
class A 
{ 
void print() 
{ 
System.out.println("A.print"); 
} 
}c 
lass B extends A 
{ 
void print() 
{ 
System.out.println("B.print"); 
} 
}c 
lass C extends B 
{ 
void print() 
{ 
System.out.println("C.print"); 
}v 
oid m() 
{ 
super.print(); 
} 
}c 
lass BDemo44 
{ 
public static void main(String args[]) 
{ 
C c1=new C(); 
c1.m(); 
} 
} 
B.print 
JAVA PRACTICAL HANDBOOK Page 57
53. 
class A 
{ 
A() 
{ 
print(); 
}v 
oid print() 
{ 
System.out.println("A.print"); 
} 
}c 
lass B extends A 
{ 
void print() 
{ 
System.out.println("B.print"); 
} 
} 
class BDemo45 
{ 
public static void main(String args[]) 
{ 
new B(); 
} 
} 
B.print 
JAVA PRACTICAL HANDBOOK Page 58
54. 
class A 
{ 
int a=100; 
A() 
{ 
print(); 
}v 
oid print() 
{ 
System.out.println("A.print "+a); 
} 
}c 
lass B extends A 
{ 
int a=200; 
void print() 
{ 
System.out.println("B.print "+a); 
} 
} 
class BDemo46 
{ 
public static void main(String args[]) 
{ 
new B(); 
} 
} 
B.print 0 
JAVA PRACTICAL HANDBOOK Page 59
55. 
class A 
{ 
int a=100; 
A() 
{ 
print(); 
}v 
oid print() 
{ 
System.out.println("A.print "+a); 
} 
}c 
lass B extends A 
{ 
//int a=200; 
void print() 
{ 
System.out.println("B.print "+a); 
} 
} 
class BDemo47 
{ 
public static void main(String args[]) 
{ 
new B(); 
} 
} 
B.print 100 
JAVA PRACTICAL HANDBOOK Page 60
56. 
class A 
{ 
int a=100; 
A() 
{ 
print(); 
}v 
oid print() 
{ 
System.out.println("A.print "+a); 
} 
}c 
lass B extends A 
{ 
static int a=200; 
void print() 
{ 
System.out.println("B.print "+a); 
} 
} 
class BDemo48 
{ 
public static void main(String args[]) 
{ 
new B(); 
} 
} 
B.print 200 
JAVA PRACTICAL HANDBOOK Page 61
57. 
class A 
{ 
A() 
{ 
print(); 
}s 
tatic void print() 
{ 
System.out.println("A.print "); 
} 
}c 
lass B extends A 
{ 
static void print() 
{ 
System.out.println("B.print "); 
} 
} 
class BDemo49 
{ 
public static void main(String args[]) 
{ 
new B(); 
} 
} 
A.print 
JAVA PRACTICAL HANDBOOK Page 62
58. Private methods 
class A 
{ 
void m1() 
{ 
System.out.println("A.m1"); 
}p 
rivate void m2() 
{ 
System.out.println("A.m2"); 
}v 
oid mA() 
{ 
m1(); 
m2(); 
} 
} 
class BDemo50 
{ 
public static void main(String asrgs[]) 
{ 
new A().mA(); 
new A().m1(); 
/* new A().m2(); error: m2() has private access in A new 
A().m2(); */ 
} 
} 
A.m1 
A.m2 
A.m1 
JAVA PRACTICAL HANDBOOK Page 63
59. 
class A 
{ 
A() 
{ 
print(); 
}p 
rivate void print() 
{ 
System.out.println("A.print "); 
} 
}c 
lass B extends A 
{ 
void print() 
{ 
System.out.println("B.print "); 
} 
} 
class BDemo51 
{ 
public static void main(String args[]){ 
new B(); 
} 
} 
A.print 
JAVA PRACTICAL HANDBOOK Page 64
60. 
class A 
{ 
X print() 
{ 
X x1=new X(); 
System.out.println("X.print "); 
return x1; 
} 
}c 
lass B extends A 
{ 
Y print() 
{ 
Y y1=new Y(); 
System.out.println("Y.print "); 
return y1; 
} 
} 
class BDemo52 
{ 
public static void main(String args[]) 
{ 
A a1=new B(); 
X ob= a1.print(); 
} 
}c 
lass X{} 
class Y extends X {} 
Y.print 
JAVA PRACTICAL HANDBOOK Page 65
61. class "Object" 
class A 
{}c 
lass BDemo53 
{ 
public static void main(String args[]) 
{ 
A a1=new A(); 
System.out.println(a1.toString()); 
System.out.println(a1.hashCode()); 
} 
} 
A@10385c1 
17008065 
JAVA PRACTICAL HANDBOOK Page 66
62. 
class A extends Object 
{}c 
lass BDemo54 
{ 
public static void main(String args[]) 
{ 
A a1=new A(); 
System.out.println(a1.toString()); 
System.out.println(a1.hashCode()); 
} 
} 
A@10385c1 
17008065 
63. Demonstrate methods "toString()" 
class A 
{ 
int a; 
A(int i) 
{ 
a=i; 
} 
}c 
lass BDemo55 
{ 
public static void main(String args[]) 
{ 
A a1=new A(100); 
System.out.println(a1.toString()); 
System.out.println(a1); 
} 
} 
A@10385c1 
A@10385c1 
JAVA PRACTICAL HANDBOOK Page 67
64. 
class A 
{ 
int a; 
A(int i) 
{ 
a=i; 
}p 
ublic String toString() 
{ 
return ""+a; 
} 
}c 
lass BDemo56 
{ 
public static void main(String args[]) 
{ 
A a1=new A(100); 
System.out.println(a1.toString()); 
System.out.println(a1); 
} 
} 
100 
100 
JAVA PRACTICAL HANDBOOK Page 68
65. 
class A 
{ 
public String toString() 
{ 
return "class A"; 
} 
}c 
lass B extends A 
{ 
public String toString() 
{ 
return "class B"; 
} 
}c 
lass C extends B 
{ 
public String toString() 
{ 
return "class C"; 
} 
}c 
lass BDemo57 
{ 
public static void main(String args[]) 
{ 
Object ob; 
ob=new A(); 
System.out.println(ob); 
ob=new B(); 
System.out.println(ob); 
ob=new C(); 
System.out.println(ob); 
} 
} 
class A 
class B 
class C 
JAVA PRACTICAL HANDBOOK Page 69
66. Demonstrate methods "equals(Object ob)" of class Object 
class A 
{ 
int a; 
A(int i) 
{ 
a=i; 
} 
}c 
lass BDemo58 
{ 
public static void main(String args[]) 
{ 
A a1=new A(100); 
A a2=new A(100); 
A a3=new A(200); 
A a4=a1; 
System.out.println(a1==a2); 
System.out.println(a1==a3); 
System.out.println(a1==a4); 
} 
} 
false 
false 
true 
JAVA PRACTICAL HANDBOOK Page 70
67. 
class A 
{ 
int a; 
A(int i) 
{ 
a=i; 
} 
}c 
lass BDemo59 
{ 
public static void main(String args[]) 
{ 
A a1=new A(100); 
A a2=new A(100); 
A a3=new A(200); 
A a4=a1; 
System.out.println(a1.equals(a2)); 
System.out.println(a1.equals(a3)); 
System.out.println(a1.equals(a4)); 
} 
} 
false 
false 
true 
JAVA PRACTICAL HANDBOOK Page 71
68. 
class A 
{ 
int a; 
A(int i) 
{ 
a=i; 
}p 
ublic boolean equals(Object ob) 
{ 
A a1=(A)ob; 
return a1.a==this.a ; 
} 
}c 
lass BDemo60 
{ 
public static void main(String args[]) 
{ 
A a1=new A(100); 
A a2=new A(100); 
A a3=new A(200); 
A a4=a1; 
System.out.println(a1.equals(a2)); 
System.out.println(a1.equals(a3)); 
System.out.println(a1.equals(a4)); 
} 
} 
true 
false 
true 
JAVA PRACTICAL HANDBOOK Page 72
69. Methods Overloading for class Type 
class BDemo61 
{ 
static void m(A a) 
{ 
System.out.println("m(A)"); 
}s 
tatic void m(B a) 
{ 
System.out.println("m(B)"); 
}s 
tatic void m(C a) 
{ 
System.out.println("m(C)"); 
}p 
ublic static void main(String args[]) 
{ 
A a1=new A(); 
B b1=new B(); 
C c1=new C(); 
BDemo61.m(a1); 
BDemo61.m(b1); 
BDemo61.m(c1); 
} 
}c 
lass A{} 
class B{} 
class C{} 
m(A) 
m(B) 
m(C) 
JAVA PRACTICAL HANDBOOK Page 73
70. 
class BDemo62 
{ 
static void m(A a) 
{ 
System.out.println("m(A)"); 
}s 
tatic void m(B a) 
{ 
System.out.println("m(B)"); 
}p 
ublic static void main(String args[]) 
{ 
A a1=new A(); 
B b1=new B(); 
C c1=new C(); 
BDemo62.m(a1); 
BDemo62.m(b1); 
BDemo62.m(c1); 
} 
}c 
lass A{} 
class B{} 
class C extends B{} 
m(A) 
m(B) 
m(B) 
JAVA PRACTICAL HANDBOOK Page 74
71. 
class BDemo63 
{ 
static void m(A a) 
{ 
System.out.println("m(A)"); 
}s 
tatic void m(B a) 
{ 
System.out.println("m(B)"); 
}p 
ublic static void main(String args[]) 
{ 
A a1=new A(); 
B b1=new B(); 
C c1=new C(); 
BDemo63.m(a1); 
BDemo63.m(b1); 
BDemo63.m(c1); 
} 
}c 
lass A{} 
class B extends A{} 
class C extends B{} 
m(A) 
m(B) 
m(B) 
JAVA PRACTICAL HANDBOOK Page 75
72. 
class BDemo64 
{ 
static void m(Object a) 
{ 
System.out.println("m(Object)"); 
} 
public static void main(String args[]) 
{ 
A a1=new A(); 
B b1=new B(); 
C c1=new C(); 
BDemo64.m(a1); 
BDemo64.m(b1); 
BDemo64.m(c1); 
} 
}c 
lass A{} 
class B extends A{ } 
class C extends B{} 
m(Object) 
m(Object) 
m(Object) 
JAVA PRACTICAL HANDBOOK Page 76
73. Abstract Methods 
abstract class Vehicle 
{ 
abstract void park(); 
}c 
lass BDemo65 
{ 
public static void main(String args[]) 
{ 
Vehicle ob; 
ob=new Car(); 
ob.park(); 
ob=new Bus(); 
ob.park(); 
ob=new MB(); 
ob.park(); 
} 
}c 
lass Car extends Vehicle 
{ 
void park() 
{ 
System.out.println("Car park"); 
} 
}c 
lass Bus extends Vehicle 
{ 
void park() 
{ 
System.out.println("Bus park"); 
} 
}c 
lass MB extends Vehicle 
{ 
void park() 
{ 
System.out.println("MB park"); 
} 
} 
Car park 
Bus park 
MB park 
JAVA PRACTICAL HANDBOOK Page 77
74. Creating an inner class object 
class A 
{ 
int i=100; 
void m() 
{ 
System.out.print(i); 
}c 
lass Inner 
{ 
int j=10; 
void mn() 
{ 
System.out.print(i+" "); 
System.out.print(j); 
} 
} 
}c 
lass DemoA2 
{ 
public static void main(String args[]) 
{ 
A ob=new A(); 
A.Inner inOb=ob.new Inner(); 
inOb.mn(); 
} 
} 
100 10 
JAVA PRACTICAL HANDBOOK Page 78

More Related Content

What's hot

Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMMario Fusco
 
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance PuzzlersDoug Hawkins
 
Java simple programs
Java simple programsJava simple programs
Java simple programsVEERA RAGAVAN
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iiiNiraj Bharambe
 
The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189Mahmoud Samir Fayed
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queuesIntro C# Book
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)Alok Kumar
 
JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksDoug Hawkins
 
Java Practical File Diploma
Java Practical File DiplomaJava Practical File Diploma
Java Practical File Diplomamustkeem khan
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 

What's hot (20)

Java programs
Java programsJava programs
Java programs
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
 
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance Puzzlers
 
Java simple programs
Java simple programsJava simple programs
Java simple programs
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iii
 
DCN Practical
DCN PracticalDCN Practical
DCN Practical
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java programs
Java programsJava programs
Java programs
 
The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's Tricks
 
Awt
AwtAwt
Awt
 
Java Practical File Diploma
Java Practical File DiplomaJava Practical File Diploma
Java Practical File Diploma
 
C#
C#C#
C#
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
srgoc
srgocsrgoc
srgoc
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 

Similar to Java Volume Calculator Handbook

Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical FileFahad Shaikh
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarROHIT JAISWAR
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)Chhom Karath
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...MaruMengesha
 
java experiments and programs
java experiments and programsjava experiments and programs
java experiments and programsKaruppaiyaa123
 
Basic program in java
Basic program in java Basic program in java
Basic program in java Sudeep Singh
 
Java Programs
Java ProgramsJava Programs
Java Programsvvpadhu
 
Please help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdfPlease help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdfJUSTSTYLISH3B2MOHALI
 
Constructor&method
Constructor&methodConstructor&method
Constructor&methodJani Harsh
 

Similar to Java Volume Calculator Handbook (20)

Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical File
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
662305 10
662305 10662305 10
662305 10
 
class object.pptx
class object.pptxclass object.pptx
class object.pptx
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
 
java experiments and programs
java experiments and programsjava experiments and programs
java experiments and programs
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
Java programs
Java programsJava programs
Java programs
 
JAVAPGMS.docx
JAVAPGMS.docxJAVAPGMS.docx
JAVAPGMS.docx
 
C++ programs
C++ programsC++ programs
C++ programs
 
Basic program in java
Basic program in java Basic program in java
Basic program in java
 
Dotnet 18
Dotnet 18Dotnet 18
Dotnet 18
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Please help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdfPlease help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdf
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
Constructor&method
Constructor&methodConstructor&method
Constructor&method
 

More from Manusha Dilan

Telco app development
Telco app developmentTelco app development
Telco app developmentManusha Dilan
 
Jade Application Wedding Planner (Groom Assist)
Jade Application Wedding Planner (Groom Assist)Jade Application Wedding Planner (Groom Assist)
Jade Application Wedding Planner (Groom Assist)Manusha Dilan
 
E commerce application using asp.net mvc4
E commerce application using asp.net mvc4E commerce application using asp.net mvc4
E commerce application using asp.net mvc4Manusha Dilan
 
Advanced python concepts
Advanced python conceptsAdvanced python concepts
Advanced python conceptsManusha Dilan
 
Ruhune maha wiharaya(රුහුණේ මහා විහාරය )
Ruhune maha wiharaya(රුහුණේ මහා විහාරය )Ruhune maha wiharaya(රුහුණේ මහා විහාරය )
Ruhune maha wiharaya(රුහුණේ මහා විහාරය )Manusha Dilan
 
Selective repeat protocol
Selective repeat protocolSelective repeat protocol
Selective repeat protocolManusha Dilan
 
HCI_chapter_09-Evaluation_techniques
HCI_chapter_09-Evaluation_techniquesHCI_chapter_09-Evaluation_techniques
HCI_chapter_09-Evaluation_techniquesManusha Dilan
 

More from Manusha Dilan (13)

Cell aging
Cell agingCell aging
Cell aging
 
Waterfall model
Waterfall modelWaterfall model
Waterfall model
 
Telco app development
Telco app developmentTelco app development
Telco app development
 
Jade Application Wedding Planner (Groom Assist)
Jade Application Wedding Planner (Groom Assist)Jade Application Wedding Planner (Groom Assist)
Jade Application Wedding Planner (Groom Assist)
 
E commerce application using asp.net mvc4
E commerce application using asp.net mvc4E commerce application using asp.net mvc4
E commerce application using asp.net mvc4
 
Advanced python concepts
Advanced python conceptsAdvanced python concepts
Advanced python concepts
 
Ruhune maha wiharaya(රුහුණේ මහා විහාරය )
Ruhune maha wiharaya(රුහුණේ මහා විහාරය )Ruhune maha wiharaya(රුහුණේ මහා විහාරය )
Ruhune maha wiharaya(රුහුණේ මහා විහාරය )
 
B2C Models
B2C ModelsB2C Models
B2C Models
 
Python collections
Python collectionsPython collections
Python collections
 
Selective repeat protocol
Selective repeat protocolSelective repeat protocol
Selective repeat protocol
 
Cellular concepts
Cellular conceptsCellular concepts
Cellular concepts
 
HCI_chapter_09-Evaluation_techniques
HCI_chapter_09-Evaluation_techniquesHCI_chapter_09-Evaluation_techniques
HCI_chapter_09-Evaluation_techniques
 
Lan technologies
Lan technologiesLan technologies
Lan technologies
 

Recently uploaded

Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...HetalPathak10
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptxmary850239
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfChristalin Nelson
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxryandux83rd
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...Nguyen Thanh Tu Collection
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPCeline George
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 

Recently uploaded (20)

Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Chi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical VariableChi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical Variable
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdf
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptx
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERP
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 

Java Volume Calculator Handbook

  • 1. JAVA PRACTICAL HANDBOOK 1. Basic Java class class Ex1 { public static void main(String args[]) { int width; int length; int height; width=5; length=12; height=3; int v; v=width*length*height; System.out.println("Volume is :"+v); } } Volume is :180 JAVA PRACTICAL HANDBOOK Page 1
  • 2. 2. class Ex2 { public static void main(String args[]) { int width; int length; int height; width=5; length=12; height=3; int v; v=width*length*height; System.out.println("Volume is :"+v); //Box 2 int width1; int length1; int height1; width1=50; length1=120; height1=30; int v1; v1=width1*length1*height1; System.out.println("Volume is :"+v1); } } Volume is :180 Volume is :180000 JAVA PRACTICAL HANDBOOK Page 2
  • 3. 3. class Box { int width; int length; int height; }c lass BoxDemo { public static void main(String args[]) { int x; x=100; System.out.println(x); Box b1; b1=new Box(); System.out.println(b1); b1.width=5; b1.length=12; b1.height=3; int v; v=b1.width*b1.length*b1.height; System.out.println("Volume is :"+v); } } 100 Box@119298d Volume is :180 JAVA PRACTICAL HANDBOOK Page 3
  • 4. 4. class Box { int width; int length; int height; } class BDemo { public static void main(String args[]) { Box b1=new Box(); b1.width=5; b1.length=12; b1.height=3; int v; v=b1.width*b1.length*b1.height; System.out.println("Volume is :"+v); //Box 2 Box b2=new Box(); b2.width=50; b2.length=120; b2.height=30; int v1; v1=b2.width*b2.length*b2.height; System.out.println("Volume is :"+v1); } } Volume is :180 Volume is :180000 JAVA PRACTICAL HANDBOOK Page 4
  • 5. 5. Methods class Box { //Variable declarations int width; int length; int height; //Creating a method to print Volume void printVolume() { System.out.println("Volume is :"+(width*length*height)); } } class BDemo1 { public static void main(String args[]) { Box b1=new Box(); b1.width=5; b1.length=12; b1.height=3; b1.printVolume(); //Box 2 Box b2=new Box(); b2.width=50; b2.length=120; b2.height=30; b2.printVolume(); } } Volume is :180 Volume is :180000 JAVA PRACTICAL HANDBOOK Page 5
  • 6. 6. class Box { int width; int length; int height; //Creating a method to print Volume void printVolume() { int v; v=width*length*height; System.out.println("Volume is :"+v); } } class BDemo2 { public static void main(String args[]) { Box b1=new Box(); b1.width=5; b1.length=12; b1.height=3; b1.printVolume(); //Box 2 Box b2=new Box(); b2.width=50; b2.length=120; b2.height=30; b2.printVolume(); } } Volume is :180 Volume is :180000 JAVA PRACTICAL HANDBOOK Page 6
  • 7. 7. class Box { int width; int length; int height; //Creating a method to print Volume void printVolume() { int v; v=width*length*height; System.out.println("Volume is :"+v); }v oid setValues(int w, int l, int h) { width=w; length=l; height=h; } } class BDemo3 { public static void main(String args[]) { Box b1=new Box(); b1.setValues(5,12,3); b1.printVolume(); Box b2=new Box(); b2.setValues(50,120,30); b2.printVolume(); } } Volume is :180 Volume is :180000 JAVA PRACTICAL HANDBOOK Page 7
  • 8. 8. Methods with a return type class Box { int width; int length; int height; //Creating a method to print Volume void printVolume() { int v; v=width*length*height; System.out.println("Volume is :"+v); }v oid setValues(int w, int l, int h) { width=w; length=l; height=h; }i nt getVolume() { int v; v=width*length*height; return v; } }c lass BDemo4 { public static void main(String args[]) { Box b1=new Box(); b1.setValues(5,12,3); int v1=b1.getVolume();//returs volume (int) v1+=100; System.out.println("Volume :"+v1); } } Volume :280 JAVA PRACTICAL HANDBOOK Page 8
  • 9. 9. class A { int tot(int i, int j) { int k=i+j; System.out.println("tot :"+k); return k; } } class DemoA { public static void main(String args[]) { A a1=new A(); int t=a1.tot(10,20); t++; System.out.println("Tot now :"+t); } } tot :30 Tot now :31 JAVA PRACTICAL HANDBOOK Page 9
  • 10. 10. class A { int tot(int i, int j) { int k=i+j; System.out.println("tot :"+k); return k; } } class DemoA1 { public static void main(String args[]) { A a1=new A(); a1.tot(10,20); } } tot :30 JAVA PRACTICAL HANDBOOK Page 10
  • 11. 11. Default values of attributes class Box { int width; int length; int height; //Creating a method to print Volume void printVolume() { int v; v=width*length*height; System.out.println("Volume is :"+v); }v oid setValues(int w, int l, int h) { width=w; length=l; height=h; }i nt getVolume() { int v; v=width*length*height; return v; } }c lass BDemo5 { public static void main(String args[]) { Box b1=new Box(); b1.printVolume(); } } Volume is :180 Volume is :180000 JAVA PRACTICAL HANDBOOK Page 11
  • 12. 12. class Box { int width=10; int length=10; int height=10; //Creating a method to print Volume void printVolume() { int v; v=width*length*height; System.out.println("Volume is :"+v); } void setValues(int w, int l, int h) { width=w; length=l; height=h; } int getVolume() { int v; v=width*length*height; return v; } } class BDemo6 { public static void main(String args[]) { Box b1=new Box(); b1.printVolume(); } } Volume is :1000 JAVA PRACTICAL HANDBOOK Page 12
  • 13. 13. Constructors class Box { Box() { System.out.println("a box is created.."); } }c lass BDemo7 { public static void main(String args[]) { Box b1=new Box(); //Prints "a box is created.." calling Box constructor Box b2=new Box(); //Prints "a box is created.." } } a box is created.. a box is created.. JAVA PRACTICAL HANDBOOK Page 13
  • 14. 14. class Box { int width=10; int length=10; int height=10; Box() { width=2; length=2; height=2; }/ /Creating a method to print Volume void printVolume() { int v; v=width*length*height; System.out.println("Volume is :"+v); } } class BDemo8 { public static void main(String args[]) { Box b1=new Box(); b1.printVolume();//prints Volume is 8 calling Box Constructor } } Volume is :8 JAVA PRACTICAL HANDBOOK Page 14
  • 15. 15. class Box { int width; int length; int height; Box(int w, int l, int h) { width=w; length=l; height=h; }/ /Creating a method to print Volume void printVolume() { int v; v=width*length*height; System.out.println("Volume is :"+v); } }c lass BDemo9 { public static void main(String args[]) { Box b1=new Box(5,12,3);//Calling constructor with parameters b1.printVolume(); Box b2=new Box(50,120,30); b2.printVolume(); } } Volume is :180 Volume is :180000 JAVA PRACTICAL HANDBOOK Page 15
  • 16. 16. Method Overloading class Box { int width; int length; int height; //Creating a method to print Volume void printVolume() { int v; v=width*length*height; System.out.println("Volume is :"+v); }v oid setValues(int w, int l, int h) { width=w; length=l; height=h; }v oid setValues(int l) { width=l; length=l; height=l; } }c lass BDemo10 { public static void main(String args[]) { Box b1=new Box(); b1.setValues(2,3,6); b1.printVolume(); Box b2=new Box(); b2.setValues(5);//Call overloaded method "setValues(int)" b2.printVolume(); } } Volume is :36 Volume is :125 JAVA PRACTICAL HANDBOOK Page 16
  • 17. 17. Example for "this" class Black { int a; void setA(int a) { a=a; //call "this.a=a" }v oid setB(int a) { this.a=a; } }c lass DemoBlack { public static void main(String args[]) { Black a1=new Black(); a1.setA(10); System.out.println("Without this "+a1.a); a1.setB(10); System.out.println("Using this "+a1.a); } } Without this 0 Using this 10 JAVA PRACTICAL HANDBOOK Page 17
  • 18. 18. Primitive VS Ref. Variables class Box { int length; int width; int height; Box b1; }c lass BDemo11 { public static void main(String args[]) { Box ob1=new Box(); System.out.println(ob1.length); System.out.println(ob1.b1); ob1.length=12; ob1.width=5; ob1.height=3; ob1.b1=new Box(); System.out.println(ob1.length); System.out.println(ob1.b1); } } 0 null 12 Box@f72617 JAVA PRACTICAL HANDBOOK Page 18
  • 19. 19. class Box { int length; int width; int height; Box b1; Box() { length=1; width=1; height=1; b1=this; }v oid setValues(int length, int width, int height) { b1.length=length;//this.length=length; } }c lass BDemo12 { public static void main(String args[]) { Box ob1=new Box(); System.out.println(ob1.length); System.out.println(ob1.b1); } } 1 Box@119298d JAVA PRACTICAL HANDBOOK Page 19
  • 20. 20. class A { int a; X x1; Y y1; Z z1; }c lass BDemo13 { public static void main(String args[]) { A a1=new A(); System.out.println(a1.a); System.out.println(a1.x1); System.out.println(a1.y1); System.out.println(a1.z1); System.out.println("********"); a1.a=100; a1.x1=new X(); a1.y1=new Y(); System.out.println(a1.a); System.out.println(a1.x1); System.out.println(a1.y1); System.out.println(a1.z1); } }c lass X{} class Y{} class Z{} 0 null null null ******** 100 X@1fc4bec Y@dc8569 null JAVA PRACTICAL HANDBOOK Page 20
  • 21. 21. Call By Values class ADemo2 { public static void main(String args[]) { int x=10; System.out.println("X :"+x); A a1=new A(); a1.incr(x); System.out.println("X :"+x); } }c lass A { void incr(int x) { x++; System.out.println("X :"+x); } } X :10 X :11 X :10 JAVA PRACTICAL HANDBOOK Page 21
  • 22. 22. Call By Ref class BDemo14 { public static void main(String args[]) { V v1=new V(); System.out.println("X :"+v1.x); A a1=new A(); a1.incr(v1); System.out.println("X :"+v1.x); } }c lass A { void incr(V v1) { v1.x++; System.out.println("X :"+v1.x); } }c lass V { int x=10; } X :10 X :11 X :11 JAVA PRACTICAL HANDBOOK Page 22
  • 23. 23. Call by values class BDemo15 { public static void main(String args[]) { int x=10, y=20; System.out.println(x+" "+y); A a1=new A(); a1.change(x,y); System.out.println(x+" "+y); } }c lass A { void change(int x, int y) { int z; z=x; x=y; y=z; System.out.println(x+" "+y); } } 10 20 20 10 10 20 JAVA PRACTICAL HANDBOOK Page 23
  • 24. 24. Static variables class A { int a; int b; } class BDemo16 { public static void main(String args[]) { A a1=new A(); a1.a=10; a1.b=20; System.out.println("Object a1 a :"+a1.a+" b :"+a1.b); A a2=new A(); a2.a=100; a2.b=200; System.out.println("Object a1 a :"+a1.a+" b :"+a1.b); } } Object a1 a :10 b :20 Object a1 a :10 b :20 JAVA PRACTICAL HANDBOOK Page 24
  • 25. 25. class A { int a; static int b; } class BDemo17 { public static void main(String args[]) { A a1=new A(); a1.a=10; a1.b=20; System.out.println("Object a1 a :"+a1.a+" b :"+a1.b); A a2=new A(); a2.a=100; a2.b=200; System.out.println("Object a1 a :"+a1.a+" b :"+a1.b); } } Object a1 a :10 b :20 Object a1 a :10 b :200 JAVA PRACTICAL HANDBOOK Page 25
  • 26. 26. class A { int a; static int b; } class BDemo18 { public static void main(String args[]) { A a1=new A(); a1.a=10; a1.b=20; System.out.println("Object a1 a :"+a1.a+" b :"+a1.b); A a2=new A(); a2.a=100; a2.b=200; System.out.println("Object a1 a :"+a1.a+" b :"+a1.b); System.out.println("Object a2 a :"+a2.a+" b :"+a2.b); a1.a=10000; a1.b=20000; System.out.println("Object a2 a :"+a2.a+" b :"+a2.b); A a3=new A(); System.out.println("Object a3 a :"+a3.a+" b :"+a3.b); A.b=2000000;// System.out.println("a1.b :"+a1.b+" a2.b :"+a2.b+" a3.b :"+a3.b); } } Object a1 a :10 b :20 Object a1 a :10 b :200 Object a2 a :100 b :200 Object a2 a :100 b :20000 Object a3 a :0 b :20000 a1.b :2000000 a2.b :2000000 a3.b :2000000 JAVA PRACTICAL HANDBOOK Page 26
  • 27. 27. Static methods class A { void m1() { System.out.println("m1"); }s tatic void m2() { System.out.println("m2"); } } class BDemo19 { public static void main(String args[]) { A ob=new A(); ob.m1(); A.m2();// without creating object can call method } } m1 m2 JAVA PRACTICAL HANDBOOK Page 27
  • 28. 28. class A { int x=10; static int y=20; void m1() { System.out.println(x); System.out.println(y); } } class BDemo20 { public static void main(String args[]) { new A().m1(); } } 10 20 JAVA PRACTICAL HANDBOOK Page 28
  • 29. 29. class A { int x=10; static int y=20; void m1() { System.out.println(this.x); System.out.println(this.y); } } class BDemo21 { public static void main(String args[]) { new A().m1(); } } 10 20 JAVA PRACTICAL HANDBOOK Page 29
  • 30. 30. Object / static initializer class A { static { System.out.print("1 "); }{ System.out.print("2 "); }A () { System.out.print("3 "); }v oid m1() { System.out.print("4 "); }s tatic void m2() { System.out.print("5 "); } } class BDemo22 { public static void main(String args[]) { new A(); A.m2(); } } 1 2 3 5 JAVA PRACTICAL HANDBOOK Page 30
  • 31. 31. class A { int a; void printA() { System.out.println("a :"+a); } }c lass B extends A { int b; void printB() { System.out.println("b :"+b); }v oid printAB() { System.out.println("a+b :"+(a+b)); }v oid callAB() { printA(); printB(); } }c lass BDemo23 { public static void main(String args[]) { B b1=new B(); b1.a=10; b1.b=20; b1.printA(); b1.printB(); b1.printAB(); b1.callAB(); } } a :10 b :20 a+b :30 a :10 b :20 JAVA PRACTICAL HANDBOOK Page 31
  • 32. 32. super class Constructor class A { int a; A() { System.out.println("A()"); }A (int i) { System.out.println("A(int)"); }v oid printA() { System.out.println("a :"+a); } }c lass B extends A { int b; B() { System.out.println("B()"); }B (int i) { System.out.println("B(int)"); }v oid printB() { System.out.println("b :"+b); }v oid printAB() { System.out.println("a+b :"+(a+b)); }v oid callAB() { printA(); printB(); } }c lass BDemo24 { public static void main(String args[]) { B b1=new B(); B b2=new B(100); } JAVA PRACTICAL HANDBOOK Page 32
  • 33. } A() B() A() B(int) 33. class A { int a; A() { System.out.println("A()"); }A (int i) { System.out.println("A(int)"); }v oid printA() { System.out.println("a :"+a); } } class B extends A { int b; B() { super(); System.out.println("B()"); }B (int i) { super(); System.out.println("B(int)"); }v oid printB() { System.out.println("b :"+b); }v oid printAB() { System.out.println("a+b :"+(a+b)); }void callAB(){ JAVA PRACTICAL HANDBOOK Page 33
  • 34. printA(); printB(); } }c lass BDemo25 { public static void main(String args[]) { B b1=new B(); B b2=new B(100); } } A() B() A() B(int) JAVA PRACTICAL HANDBOOK Page 34
  • 35. 34. class A { int a; A() { System.out.println("A()"); }A (int i) { System.out.println("A(int)"); }v oid printA() { System.out.println("a :"+a); } }c lass B extends A { int b; B() { super();// System.out.println("B()"); }B (int i) { super(i);//super(100); System.out.println("B(int)"); }v oid printB() { System.out.println("b :"+b); }v oid printAB() { System.out.println("a+b :"+(a+b)); }v oid callAB() { printA(); printB(); } }c lass BDemo26 { public static void main(String args[]) { B b1=new B(); JAVA PRACTICAL HANDBOOK Page 35
  • 36. B b2=new B(100); } } A() B() A(int) B(int) JAVA PRACTICAL HANDBOOK Page 36
  • 37. 35. class A { int a; A() { System.out.println("A()"); }A (int i) { System.out.println("A(int)"); }v oid printA() { System.out.println("a :"+a); } }c lass B extends A { int b; B() { super();// System.out.println("B()"); }B (int i) { super(i);//super(100); System.out.println("B(int)"); }v oid printB() { System.out.println("b :"+b); }v oid printAB() { System.out.println("a+b :"+(a+b)); }v oid callAB() { printA(); printB(); } }c lass BDemo27 { public static void main(String args[]) { A a1; JAVA PRACTICAL HANDBOOK Page 37
  • 38. a1=new B(); //B b1=new A();//Error a1.a=10; // a1.b=20; Error a1.printA(); /* a1.printB();//Error a1.printAB();//Error a1.callAB();//Error */ } } A() B() a :10 JAVA PRACTICAL HANDBOOK Page 38
  • 39. 36. Method Overriding class A { int a; void printA() { System.out.println("a :"+a); }v oid print() { System.out.println("A.print"); } } class B extends A { int b; void printB() { System.out.println("b :"+b); }v oid printAB() { System.out.println("a+b :"+(a+b)); }v oid callAB() { printA(); printB(); }v oid print() { System.out.println("B.print"); } } class BDemo28 { public static void main(String args[]) { A a1=new B(); a1.print(); } } B.print JAVA PRACTICAL HANDBOOK Page 39
  • 40. 37. class Vehicle { void park() {} }c lass Car extends Vehicle { void park() { System.out.println("Car park"); } }c lass Bus extends Vehicle { void park() { System.out.println("Bus park"); } }c lass MB extends Vehicle { void park() { System.out.println("MB park"); } }c lass BDemo29 { public static void main(String argsp[]) { Vehicle ob; ob=new Car(); ob.park(); //print "Car park" ob=new Bus(); ob.park(); //print "Bus park" ob=new MB(); ob.park(); //print "MB park" } } Car park Bus park MB park JAVA PRACTICAL HANDBOOK Page 40
  • 41. 38. class A { static { System.out.print("1 "); }{ System.out.print("2 "); }A () { System.out.print("3 "); }s tatic void mA() { System.out.print("4 "); } }c lass B extends A { static { System.out.print("5 "); }{ System.out.print("6 "); }B () { System.out.print("7 "); }s tatic void mB() { System.out.print("8 "); } }c lass BDemo30 { public static void main(String args[]) { new B(); } } 1 5 2 3 6 7 JAVA PRACTICAL HANDBOOK Page 41
  • 42. 39. class A { static { System.out.print("1 "); }{ System.out.print("2 "); }A () { System.out.print("3 "); }s tatic void mA() { System.out.print("4 "); } }c lass B extends A { static { System.out.print("5 "); }{ System.out.print("6 "); }B () { System.out.print("7 "); }s tatic void mB() { System.out.print("8 "); } }c lass BDemo31 { public static void main(String args[]) { B.mA(); //Prints 1 4 } } 1 4 JAVA PRACTICAL HANDBOOK Page 42
  • 43. 40. Way of invoking "super()" and "this()" class A { A(int i) { System.out.println("A(int) :"+i); } }c lass B extends A { B(int i) { super(i); System.out.println("B(int) :"+i); } }c lass BDemo32 { public static void main(String args[]) { new B(100); } } A(int) :100 B(int) :100 JAVA PRACTICAL HANDBOOK Page 43
  • 44. 41. class A { A(int i) { System.out.println("A(int) :"+i); }A (int i, int j) { System.out.println("A(int,int) :"+i+" "+j); } }c lass B extends A { B(int i) { super(100); System.out.println("B(int) :"+i); }B (int i, int j ) { super(i, j); System.out.println("B(int, int) :"+i+" "+j); } }c lass BDemo33 { public static void main(String args[]) { new B(100,200); } } A(int,int) :100 200 B(int, int) :100 200 JAVA PRACTICAL HANDBOOK Page 44
  • 45. 42. "calling this" class A { int a=100; A() { this(1); }A (int i) { a=i; } }c lass B extends A { int b=200; B(){ this(1); }B (int i) { super(i); b=i; }B (int i, int j) { super(i); b=j;// }v oid printAB() { System.out.println("a b :"+a+" "+b); } }c lass BDemo34 { public static void main(String args[]) { B b1=new B(); b1.printAB(); B b2=new B(10); b2.printAB(); B b3=new B(100,200); b3.printAB(); } } JAVA PRACTICAL HANDBOOK Page 45
  • 46. a b :1 1 a b :10 10 a b :100 200 JAVA PRACTICAL HANDBOOK Page 46
  • 47. 43. "this" VS "super" class A { int a=100; A() { this(1); }A (int i) { a=i; } }c lass B extends A { int b=200; B() { this(1); }B (int i) { super(i); b=i; }B (int i, int j) { super(i); b=j;// }v oid printAB() { System.out.println("a b :"+a+" "+b); } }c lass BDemo35 { public static void main(String args[]) { B b1=new B(); b1.printAB(); B b2=new B(10); b2.printAB(); B b3=new B(100,200); b3.printAB(); } } JAVA PRACTICAL HANDBOOK Page 47
  • 48. a b :1 1 a b :10 10 a b :100 200 JAVA PRACTICAL HANDBOOK Page 48
  • 49. 44. class A { void print() { System.out.println("A.print"); } }c lass B extends A { void print() { System.out.println("B.print"); } }c lass BDemo36 { public static void main(String args[]){ B b1=new B(); b1.print(); //prints "B.print" } } B.print JAVA PRACTICAL HANDBOOK Page 49
  • 50. 45. class A { void print() { System.out.println("A.print"); } }c lass B extends A { void print() { System.out.println("B.print"); } }c lass BDemo37 { public static void main(String args[]) { A a1=new B(); a1.print();//print "B.print" } } B.print JAVA PRACTICAL HANDBOOK Page 50
  • 51. 46. class A { int a=100; }c lass B extends A { int a=200; }c lass BDemo38 { public static void main(String args[]) { B b1=new B(); int x=b1.a; System.out.println(x);//print "200" } } 200 JAVA PRACTICAL HANDBOOK Page 51
  • 52. 47. class A { int a=100; }c lass B extends A { int a=200; }c lass BDemo39 { public static void main(String args[]) { A a1=new B(); int x=a1.a; System.out.println(x);//print "100" } } 100 JAVA PRACTICAL HANDBOOK Page 52
  • 53. 48. class A { int a=100; }c lass B extends A { int a=200; void m() { System.out.println("a :"+a); } }c lass BDemo40 { public static void main(String args[]) { B b1=new B(); b1.m(); //prints "a :200" } } 200 JAVA PRACTICAL HANDBOOK Page 53
  • 54. 49. super.a class A { int a=100; }c lass B extends A { int a=200; void m() { System.out.println("a :"+super.a); } }c lass BDemo41 { public static void main(String args[]) { B b1=new B(); b1.m(); } } a :100 50. class A { int a=100; }c lass B extends A { int a=200; }c lass C extends B { int a=300; void m() { System.out.println("a :"+a); } }c lass BDemo42 { public static void main(String args[]) { C c1=new C(); c1.m(); } JAVA PRACTICAL HANDBOOK Page 54
  • 55. } a :300 JAVA PRACTICAL HANDBOOK Page 55
  • 56. 51. class A { void print() { System.out.println("A.print"); } }c lass B extends A { void print() { System.out.println("B.print"); } }c lass C extends B { void print() { System.out.println("C.print"); }v oid m() { print(); } }c lass BDemo43 { public static void main(String args[]) { C c1=new C(); c1.m(); } } C.print JAVA PRACTICAL HANDBOOK Page 56
  • 57. 52. class A { void print() { System.out.println("A.print"); } }c lass B extends A { void print() { System.out.println("B.print"); } }c lass C extends B { void print() { System.out.println("C.print"); }v oid m() { super.print(); } }c lass BDemo44 { public static void main(String args[]) { C c1=new C(); c1.m(); } } B.print JAVA PRACTICAL HANDBOOK Page 57
  • 58. 53. class A { A() { print(); }v oid print() { System.out.println("A.print"); } }c lass B extends A { void print() { System.out.println("B.print"); } } class BDemo45 { public static void main(String args[]) { new B(); } } B.print JAVA PRACTICAL HANDBOOK Page 58
  • 59. 54. class A { int a=100; A() { print(); }v oid print() { System.out.println("A.print "+a); } }c lass B extends A { int a=200; void print() { System.out.println("B.print "+a); } } class BDemo46 { public static void main(String args[]) { new B(); } } B.print 0 JAVA PRACTICAL HANDBOOK Page 59
  • 60. 55. class A { int a=100; A() { print(); }v oid print() { System.out.println("A.print "+a); } }c lass B extends A { //int a=200; void print() { System.out.println("B.print "+a); } } class BDemo47 { public static void main(String args[]) { new B(); } } B.print 100 JAVA PRACTICAL HANDBOOK Page 60
  • 61. 56. class A { int a=100; A() { print(); }v oid print() { System.out.println("A.print "+a); } }c lass B extends A { static int a=200; void print() { System.out.println("B.print "+a); } } class BDemo48 { public static void main(String args[]) { new B(); } } B.print 200 JAVA PRACTICAL HANDBOOK Page 61
  • 62. 57. class A { A() { print(); }s tatic void print() { System.out.println("A.print "); } }c lass B extends A { static void print() { System.out.println("B.print "); } } class BDemo49 { public static void main(String args[]) { new B(); } } A.print JAVA PRACTICAL HANDBOOK Page 62
  • 63. 58. Private methods class A { void m1() { System.out.println("A.m1"); }p rivate void m2() { System.out.println("A.m2"); }v oid mA() { m1(); m2(); } } class BDemo50 { public static void main(String asrgs[]) { new A().mA(); new A().m1(); /* new A().m2(); error: m2() has private access in A new A().m2(); */ } } A.m1 A.m2 A.m1 JAVA PRACTICAL HANDBOOK Page 63
  • 64. 59. class A { A() { print(); }p rivate void print() { System.out.println("A.print "); } }c lass B extends A { void print() { System.out.println("B.print "); } } class BDemo51 { public static void main(String args[]){ new B(); } } A.print JAVA PRACTICAL HANDBOOK Page 64
  • 65. 60. class A { X print() { X x1=new X(); System.out.println("X.print "); return x1; } }c lass B extends A { Y print() { Y y1=new Y(); System.out.println("Y.print "); return y1; } } class BDemo52 { public static void main(String args[]) { A a1=new B(); X ob= a1.print(); } }c lass X{} class Y extends X {} Y.print JAVA PRACTICAL HANDBOOK Page 65
  • 66. 61. class "Object" class A {}c lass BDemo53 { public static void main(String args[]) { A a1=new A(); System.out.println(a1.toString()); System.out.println(a1.hashCode()); } } A@10385c1 17008065 JAVA PRACTICAL HANDBOOK Page 66
  • 67. 62. class A extends Object {}c lass BDemo54 { public static void main(String args[]) { A a1=new A(); System.out.println(a1.toString()); System.out.println(a1.hashCode()); } } A@10385c1 17008065 63. Demonstrate methods "toString()" class A { int a; A(int i) { a=i; } }c lass BDemo55 { public static void main(String args[]) { A a1=new A(100); System.out.println(a1.toString()); System.out.println(a1); } } A@10385c1 A@10385c1 JAVA PRACTICAL HANDBOOK Page 67
  • 68. 64. class A { int a; A(int i) { a=i; }p ublic String toString() { return ""+a; } }c lass BDemo56 { public static void main(String args[]) { A a1=new A(100); System.out.println(a1.toString()); System.out.println(a1); } } 100 100 JAVA PRACTICAL HANDBOOK Page 68
  • 69. 65. class A { public String toString() { return "class A"; } }c lass B extends A { public String toString() { return "class B"; } }c lass C extends B { public String toString() { return "class C"; } }c lass BDemo57 { public static void main(String args[]) { Object ob; ob=new A(); System.out.println(ob); ob=new B(); System.out.println(ob); ob=new C(); System.out.println(ob); } } class A class B class C JAVA PRACTICAL HANDBOOK Page 69
  • 70. 66. Demonstrate methods "equals(Object ob)" of class Object class A { int a; A(int i) { a=i; } }c lass BDemo58 { public static void main(String args[]) { A a1=new A(100); A a2=new A(100); A a3=new A(200); A a4=a1; System.out.println(a1==a2); System.out.println(a1==a3); System.out.println(a1==a4); } } false false true JAVA PRACTICAL HANDBOOK Page 70
  • 71. 67. class A { int a; A(int i) { a=i; } }c lass BDemo59 { public static void main(String args[]) { A a1=new A(100); A a2=new A(100); A a3=new A(200); A a4=a1; System.out.println(a1.equals(a2)); System.out.println(a1.equals(a3)); System.out.println(a1.equals(a4)); } } false false true JAVA PRACTICAL HANDBOOK Page 71
  • 72. 68. class A { int a; A(int i) { a=i; }p ublic boolean equals(Object ob) { A a1=(A)ob; return a1.a==this.a ; } }c lass BDemo60 { public static void main(String args[]) { A a1=new A(100); A a2=new A(100); A a3=new A(200); A a4=a1; System.out.println(a1.equals(a2)); System.out.println(a1.equals(a3)); System.out.println(a1.equals(a4)); } } true false true JAVA PRACTICAL HANDBOOK Page 72
  • 73. 69. Methods Overloading for class Type class BDemo61 { static void m(A a) { System.out.println("m(A)"); }s tatic void m(B a) { System.out.println("m(B)"); }s tatic void m(C a) { System.out.println("m(C)"); }p ublic static void main(String args[]) { A a1=new A(); B b1=new B(); C c1=new C(); BDemo61.m(a1); BDemo61.m(b1); BDemo61.m(c1); } }c lass A{} class B{} class C{} m(A) m(B) m(C) JAVA PRACTICAL HANDBOOK Page 73
  • 74. 70. class BDemo62 { static void m(A a) { System.out.println("m(A)"); }s tatic void m(B a) { System.out.println("m(B)"); }p ublic static void main(String args[]) { A a1=new A(); B b1=new B(); C c1=new C(); BDemo62.m(a1); BDemo62.m(b1); BDemo62.m(c1); } }c lass A{} class B{} class C extends B{} m(A) m(B) m(B) JAVA PRACTICAL HANDBOOK Page 74
  • 75. 71. class BDemo63 { static void m(A a) { System.out.println("m(A)"); }s tatic void m(B a) { System.out.println("m(B)"); }p ublic static void main(String args[]) { A a1=new A(); B b1=new B(); C c1=new C(); BDemo63.m(a1); BDemo63.m(b1); BDemo63.m(c1); } }c lass A{} class B extends A{} class C extends B{} m(A) m(B) m(B) JAVA PRACTICAL HANDBOOK Page 75
  • 76. 72. class BDemo64 { static void m(Object a) { System.out.println("m(Object)"); } public static void main(String args[]) { A a1=new A(); B b1=new B(); C c1=new C(); BDemo64.m(a1); BDemo64.m(b1); BDemo64.m(c1); } }c lass A{} class B extends A{ } class C extends B{} m(Object) m(Object) m(Object) JAVA PRACTICAL HANDBOOK Page 76
  • 77. 73. Abstract Methods abstract class Vehicle { abstract void park(); }c lass BDemo65 { public static void main(String args[]) { Vehicle ob; ob=new Car(); ob.park(); ob=new Bus(); ob.park(); ob=new MB(); ob.park(); } }c lass Car extends Vehicle { void park() { System.out.println("Car park"); } }c lass Bus extends Vehicle { void park() { System.out.println("Bus park"); } }c lass MB extends Vehicle { void park() { System.out.println("MB park"); } } Car park Bus park MB park JAVA PRACTICAL HANDBOOK Page 77
  • 78. 74. Creating an inner class object class A { int i=100; void m() { System.out.print(i); }c lass Inner { int j=10; void mn() { System.out.print(i+" "); System.out.print(j); } } }c lass DemoA2 { public static void main(String args[]) { A ob=new A(); A.Inner inOb=ob.new Inner(); inOb.mn(); } } 100 10 JAVA PRACTICAL HANDBOOK Page 78