C
C
Core Java
Search…
27) Casting

Instance Of :

Example 1:

1
package com.gs.interfacePrac;
2
3
class Par1 {
4
5
}
6
7
class Child1 extends Par1 {
8
9
}
10
11
public class TestInstanceOf {
12
13
public static void main(String[] args) {
14
15
Par1 par1 = new Child1();
16
Child1 child1 = new Child1();
17
18
if (par1 instanceof Child1) {
19
System.out.println("par1 instance of Child");
20
}
21
22
if (child1 instanceof Par1) {
23
System.out.println("child1 instance of Parent");
24
}
25
26
}
27
}
Copied!
1
par1 instance of Child
2
child1 instance of Parent
Copied!

Example 2:

1
package com.gs.interfacePrac;
2
3
class GrandPar {
4
5
}
6
7
class Par1 extends GrandPar {
8
9
}
10
11
class Child1 extends Par1 {
12
13
}
14
15
public class TestInstanceOf {
16
17
public static void main(String[] args) {
18
19
Par1 par1 = new Child1();
20
Child1 child1 = new Child1();
21
22
if (par1 instanceof Child1) {
23
System.out.println("par1 instance of Child");
24
}
25
26
if (child1 instanceof Par1) {
27
System.out.println("child1 instance of Parent");
28
}
29
30
if (child1 instanceof GrandPar) {
31
System.out.println("child1 instance of Grand Parent");
32
}
33
34
}
35
}
36
Copied!
1
par1 instance of Child
2
child1 instance of Parent
3
child1 instance of Grand Parent
Copied!

Casting :

1
package com.gs.interfacePrac;
2
3
class Interviewer {
4
5
void conductInterview() {
6
System.out.println("Default");
7
}
8
9
}
10
11
class Manager extends Interviewer {
12
13
void conductInterview() {
14
System.out.println("Manager is conducting");
15
}
16
17
void m1() {
18
System.out.println("m1 man");
19
}
20
}
21
22
class HRExecutive extends Interviewer {
23
24
void conductInterview() {
25
System.out.println("HR is conducting");
26
}
27
}
28
29
public class TestCasting {
30
public static void main(String[] args) {
31
Interviewer interviewer = new Manager();
32
// Manager manager = interviewer; ?? can we write this ?
33
Manager manager = (Manager) interviewer;
34
manager.m1();
35
manager.conductInterview();
36
37
Interviewer interviewer1 = new HRExecutive();
38
Manager manager1 = (Manager) interviewer1;
39
manager1.m1();
40
manager1.conductInterview();
41
}
42
}
Copied!

Solution :

1
public class TestCasting {
2
public static void main(String[] args) {
3
Interviewer interviewer = new Manager();
4
// Manager manager = interviewer; ?? can we write this ?
5
Manager manager = (Manager) interviewer;
6
manager.m1();
7
manager.conductInterview();
8
9
Interviewer interviewer1 = new HRExecutive();
10
if(interviewer1 instanceof Manager) {
11
Manager manager1 = (Manager) interviewer1;
12
manager1.m1();
13
manager1.conductInterview();
14
}
15
}
16
}
Copied!

What is casting ?

Why casting is needed ?

Problems :

1
package com.gs.interfacePrac;
2
3
class Interviewer {
4
5
void conductInterview() {
6
System.out.println("Default");
7
}
8
9
}
10
11
class Manager extends Interviewer {
12
13
void conductInterview() {
14
System.out.println("Manager is conducting");
15
}
16
17
void m1() {
18
System.out.println("m1 man");
19
}
20
}
21
22
class HRExecutive extends Interviewer {
23
24
void conductInterview() {
25
System.out.println("HR is conducting");
26
}
27
}
28
29
public class TestCasting {
30
public static void main(String[] args) {
31
32
Interviewer[] interviewers = new Interviewer[2];
33
34
interviewers[0] = new Manager();
35
interviewers[1] = new HRExecutive();
36
37
for (Interviewer interviewer : interviewers) {
38
interviewer.conductInterview();
39
((Manager)interviewer).m1();
40
}
41
}
42
}
Copied!
Last modified 2yr ago