This document contains code examples demonstrating basic Java concepts like classes, objects, methods, constructors, static variables, and more. The examples show how to define Box classes with width, length and height attributes to calculate and print the volume. Later examples demonstrate method overloading, the use of this keyword, call by value vs reference, and default values of attributes. Constructors are used to initialize object attribute values.
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
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
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