1)
/*
0
0
*/
---------------------------------------------------------------------------
2)
/*
Test
10
20
*/
---------------------------------------------------------------------------
3)
/*
Test2
Test2
Test3
10
Test1
Test1
*/
---------------------------------------------------------------------------
4)
all 0
---------------------------------------------------------------------------
5)
/*
K()
------
K()
L()
------
*/
---------------------------------------------------------------------------
6)
/*
M()
------
M()
N()
------
M()
N()
O()
------
*/
---------------------------------------------------------------------------
7)
---------------------------------------------------------------------------
8)
/*
A()
------
A(int)
------
A()
B()
------
A()
B(int)
------
*/
---------------------------------------------------------------------------
9)
/*
E()
------
E()
F(int)
*/
---------------------------------------------------------------------------
10)
/*H.java:11: cannot find symbol
symbol : constructor G()
location: class G
{
^
1 error
------------------
G(int)
------
G(int)
H()
*/
---------------------------------------------------------------------------
11)
/*
I(double)
----------
I(double)
J()
----------
I(double)
J(double)
*/
---------------------------------------------------------------------------
12)
/*
K-IIB
K()
----------
K-IIB
K()
L-IIB
L()
----------
*/
---------------------------------------------------------------------------
13)
/*
M-IIB-1
M-IIB-2
M()
----------
M-IIB-1
M-IIB-2
M()
N-IIB-1
N-IIB-2
N()
----------
*/
---------------------------------------------------------------------------
14)
/*
O-IIB
O()
------
O-IIB
O()
P-IIB
P()
------
O-IIB
O()
P-IIB
P()
P(int)
------
*/
class A
{
int i;
}
class B extends A
{
int j;
public static void main(String[] args)
{
B obj1=new B();
System.out.println(obj1.i);
System.out.println(obj1.j);
}
}
OP:/*
0
0
*/
---------------------------------------------------------------------------
2)
//inside class D there are 3 members
class C
{
int i;
void test()
{
System.out.println("Test");
}
}
class D extends C
{
int j;
}
class E
{
public static void main(String[] args)
{
D d1=new D();
d1.i=10;
d1.j=20;
d1.test();
System.out.println(d1.i);
System.out.println(d1.j);
}
}
OP:/*
Test
10
20
*/
---------------------------------------------------------------------------
3)
class F
{
int i;
static int j;
void test1()
{
System.out.println("Test1");
}
static void test2()
{
System.out.println("Test2");
}
}
class G extends F
{
int k;
static void test3()
{
System.out.println("Test3");
}
public static void main(String[] args)
{
F.j=10;
F.test2();
G.test2();
G.test3();
System.out.println(G.j);
F f1=new F();
f1.i=20;
f1.test1();
G g1=new G();
g1.i=30;
g1.k=50;
g1.test1();
}
}
OP:/*
Test2
Test2
Test3
10
Test1
Test1
*/
---------------------------------------------------------------------------
4)
class H
{
int x;
}
class I extends H
{
int y;
}
class J extends I
{
int z;
}
class Manager
{
public static void main(String[] args)
{
H h1=new H();
I i1=new I();
J j1=new J();
System.out.println(h1.x);
System.out.println(i1.x);
System.out.println(j1.x);
System.out.println(i1.y);
System.out.println(j1.y);
System.out.println(j1.z);
}
}
//while creating the object of J all class H,I and J will load in memory
OP:all 0
---------------------------------------------------------------------------
5)
class K
{
K()
{
//super(); //compiler keep super internally
System.out.println("K()");
}
}
class L extends K
{
L()
{
//super(); //compiler keep super internally
System.out.println("L()");
}
}
class Manager
{
public static void main(String[] args)
{
K k1=new K();
System.out.println("------");
L l1=new L();
System.out.println("------");
}
}
//constructor and initialization block (SIB,IIB) are not inherit to sub class
//every constructor should have the first statement either super() or this() calling statement
//if we are not keeping the super() or this() as a first statement in constructor ,compiler will keep super().
//even a parent class have super() statement to Object class.
OP:/*
K()
------
K()
L()
------
*/
---------------------------------------------------------------------------
6)
class M extends Object
{
M()
{
super();
System.out.println("M()");
}
}
class N extends M
{
N()
{
super();
System.out.println("N()");
}
}
class O extends N
{
O()
{
super();
System.out.println("O()");
}
}
class Manager2
{
public static void main(String[] args)
{
M m1=new M();
System.out.println("------");
N l1=new N();
System.out.println("------");
O o1=new O();
System.out.println("------");
}
}
//total 4 constructor are executing in this program
OP:/*
M()
------
M()
N()
------
M()
N()
O()
------
*/
---------------------------------------------------------------------------
7)
class P
{
P()
{
System.out.println("P()");
}
}
class Q extends P
{
Q()
{
System.out.println("Q()");
}
}
class R extends Q
{
R()
{
System.out.println("R()");
}
}
class S extends R
{
S()
{
System.out.println("S()");
}
}
class T extends S
{
T()
{
System.out.println("T()");
}
public static void main(String[] args)
{
P m1=new P();
System.out.println("------");
Q l1=new Q();
System.out.println("------");
R o1=new R();
System.out.println("------");
S s1=new S();
System.out.println("------");
T t1=new T();
System.out.println("------");
}
}
---------------------------------------------------------------------------
8)
class A
{
A()
{
System.out.println("A()");
}
A(int i)
{
System.out.println("A(int)");
}
}
class B extends A
{
B()
{
System.out.println("B()");
}
B(int i)
{
System.out.println("B(int)");
}
}
class Manager
{
public static void main(String[] args)
{
A m1=new A();
System.out.println("------");
A l1=new A(20);
System.out.println("------");
B o1=new B();
System.out.println("------");
B s1=new B(30);
System.out.println("------");
}
}
OP:/*
A()
------
A(int)
------
A()
B()
------
A()
B(int)
------
*/
---------------------------------------------------------------------------
9)
class E
{
E()
{
System.out.println("E()");
}
}
class F extends E
{
F(int i)
{
System.out.println("F(int)");
}
}
class Manager2
{
public static void main(String[] args)
{
E m1=new E();
System.out.println("------");
F l1=new F(20);
}
}
OP:/*
E()
------
E()
F(int)
*/
---------------------------------------------------------------------------
10)
class G
{
G(int i)
{
System.out.println("G(int)");
}
}
class H extends G
{
H()
{
//super(10); if remove this super(10) will give compiler error
System.out.println("H()");
}
}
class Manager3
{
public static void main(String[] args)
{
G g1=new G(90);
System.out.println("------");
H h1=new H();
}
}
OP:/*H.java:11: cannot find symbol
symbol : constructor G()
location: class G
{
^
1 error
------------------
G(int)
------
G(int)
H()
*/
---------------------------------------------------------------------------
11)
class I
{
I(double j)
{
System.out.println("I(double)");
}
}
class J extends I
{
J()
{
super(9.9);
System.out.println("J()");
}
J(double d){
super(d);
System.out.println("J(double)");
}
}
class Manager4
{
public static void main(String[] args)
{
I i1=new I(9.9);
System.out.println("----------");
J j1=new J();
System.out.println("----------");
J j2=new J(3.4);
}
}
OP:/*
I(double)
----------
I(double)
J()
----------
I(double)
J(double)
*/
---------------------------------------------------------------------------
12)
class K
{
K()
{
System.out.println("K()");
}
{
System.out.println("K-IIB");
}
}
class L extends K
{
L()
{
System.out.println("L()");
}
{
System.out.println("L-IIB");
}
}
class Manager5
{
public static void main(String[] args)
{
K k1=new K();
System.out.println("----------");
L l1=new L();
System.out.println("----------");
}
}
OP:/*
K-IIB
K()
----------
K-IIB
K()
L-IIB
L()
----------
*/
---------------------------------------------------------------------------
13)
class M
{
M()
{
System.out.println("M()");
}
{
System.out.println("M-IIB-1");
}
{
System.out.println("M-IIB-2");
}
}
class N extends M
{
N()
{
System.out.println("N()");
}
{
System.out.println("N-IIB-1");
}
{
System.out.println("N-IIB-2");
}
}
class Manager6
{
public static void main(String[] args)
{
M k1=new M();
System.out.println("----------");
N l1=new N();
System.out.println("----------");
}
}
OP:/*
M-IIB-1
M-IIB-2
M()
----------
M-IIB-1
M-IIB-2
M()
N-IIB-1
N-IIB-2
N()
----------
*/
---------------------------------------------------------------------------
14)
class O
{
{
System.out.println("O-IIB");
}
O()
{
System.out.println("O()");
}
}
class P extends O
{
P()
{
System.out.println("P()");
}
{
System.out.println("P-IIB");
}
P(int i)
{
this();//IT will call constructor of same class
System.out.println("P(int)");
}
}
class Manager7
{
public static void main(String[] args)
{
O m1=new O();
System.out.println("------");
P o1=new P();
System.out.println("------");
P s1=new P(30);
System.out.println("------");
}
}
OP:/*
O-IIB
O()
------
O-IIB
O()
P-IIB
P()
------
O-IIB
O()
P-IIB
P()
P(int)
------
*/
No comments:
Post a Comment