Sometimes, knowing the type of an object during run time is useful. For example, you might have one thread of execution that generates various types of objects, and another thread that processes these objects. In this situation, it might be useful for the processing thread to know the type of each object when it receives it. Another situation in which knowledge of an object's type at run time is important involves casting. In Java, an invalid cast causes a run-time error. Many invalid casts can be caught at compile time. However, casts involving class hierarchies can produce invalid casts that can be detected only at run time. For example, a superclass called A can produce two subclasses, called B and C. Thus, casting a B object into type A or casting a C object into type A is legal, but casting a B object into type C (or vice versa) isn't legal. Because an object of type A can refer to objects of either B or C, how can you know, at run time, what type of object is actually being referred to before attempting the cast to type C? It could be an object of type A, B, or C. If it is an object of type B, a run-time exception will be thrown. Java provides the run-time operator instanceof to answer this question.
The instanceof operator has this general form:
object instanceof type
Here, object is an instance of a class, and type is a class type. If object is of the specified type or can be cast into the specified type, then the instanceof operator evaluates to true. Otherwise, its result is false. Thus, instanceof is the means by which your program can obtain run-time type information about an object.
The following program demonstrates instanceof:
// Demonstrate instanceof operator.
class A {
int i, j;
}
class B {
int i, j;
}
class C extends A {
int k;
}
class D extends A {
int k;
}
class InstanceOf {
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
D d = new D();
if(a instanceof A)
System.out.println("a is instance of A");
if(b instanceof B)
System.out.println("b is instance of B");
if(c instanceof C)
System.out.println("c is instance of C");
if(c instanceof A)
System.out.println("c can be cast to A");
if(a instanceof C)
System.out.println("a can be cast to C");
System.out.println();
// compare types of derived types
A ob;
ob = d; // A reference to d
System.out.println("ob now refers to d");
if(ob instanceof D)
System.out.println("ob is instance of D");
System.out.println();
ob = c; // A reference to c
System.out.println("ob now refers to c");
if(ob instanceof D)
System.out.println("ob can be cast to D");
else
System.out.println("ob cannot be cast to D");
if(ob instanceof A)
System.out.println("ob can be cast to A");
- 230 -
System.out.println();
// all objects can be cast to Object
if(a instanceof Object)
System.out.println("a may be cast to Object");
if(b instanceof Object)
System.out.println("b may be cast to Object");
if(c instanceof Object)
System.out.println("c may be cast to Object");
if(d instanceof Object)
System.out.println("d may be cast to Object");
}
}
The output from this program is shown here:
a is instance of A
b is instance of B
c is instance of C
c can be cast to A
ob now refers to d
ob is instance of D
ob now refers to c
ob cannot be cast to D
ob can be cast to A
a may be cast to Object
b may be cast to Object
c may be cast to Object
d may be cast to Object
The instanceof operator isn't needed by most programs, because, generally, you know the type of object with which you are working. However, it can be very useful when you're writing generalized routines that operate on objects of a complex class hierarchy
share some interview advanced level questions related to some Open source : Linux, Apache, Mysql, Php, Java.
Monday, December 6, 2010
Private final methods
If one class have one private final method so what happen if another class is try to overridden the same method in another class:
for reference check the below example
class Raptor{
private final void test(){
System.out.println("i am final of Raptor");
}
}
class Hawk extends Raptor {
public final void test(){
System.out.println("i am the final of Hawk");
}
public static void main(String[] args) {
Hawk hawk_obj = new Hawk();
hawk_obj.test();
}
}
Result:
compile and program may run the output is "i am the final of Hawk", because the test function is private and it'll never available in another class so the final method never be override in another class rule is not applicable.
for reference check the below example
class Raptor{
private final void test(){
System.out.println("i am final of Raptor");
}
}
class Hawk extends Raptor {
public final void test(){
System.out.println("i am the final of Hawk");
}
public static void main(String[] args) {
Hawk hawk_obj = new Hawk();
hawk_obj.test();
}
}
Result:
compile and program may run the output is "i am the final of Hawk", because the test function is private and it'll never available in another class so the final method never be override in another class rule is not applicable.
Java Constructor don't have return type:
Constructor never return in Java and if you see the constructor have return type, so it's not a constructor it's a method with the same name of class...
class Bird {
public String Bird() {
return ("I am bird");
}
public static void main (String args[]){
Bird b - new Bird(); // object creation and assign the Reference to variable
b.Bird(); // now the function call
}
class Bird {
public String Bird() {
return ("I am bird");
}
public static void main (String args[]){
Bird b - new Bird(); // object creation and assign the Reference to variable
b.Bird(); // now the function call
}
Good use of static, constructor and init blocks
class Bird {
{
System.out.print("b1 ");
}
public Bird() {
System.out.print("b2 ");
}
}
class Raptor extends Bird {
static {
System.out.print("r1 ");
}
public Raptor() {
System.out.print("r2 ");
}
{
System.out.print("r3 ");
}
static {
System.out.print("r4 ");
}
}
class Hawk extends Raptor {
public static void main(String[] args) {
System.out.print("pre ");
new Hawk();
System.out.println("hawk ");
/*Raptor rap = new Raptor();
if (rap instanceof Raptor){
System.out.println("i m working");
}*/
}
}
Guess what is the answer: r1 r4 pre b1 b2 r3 r2 hawk
static always related with class not with instance so they run when the class initialized, next turn for constructor but here we have init blocks so first the init blocks have run then the constructor come in picture from top to bottom, super class init => constructor => child class init => constructor...
{
System.out.print("b1 ");
}
public Bird() {
System.out.print("b2 ");
}
}
class Raptor extends Bird {
static {
System.out.print("r1 ");
}
public Raptor() {
System.out.print("r2 ");
}
{
System.out.print("r3 ");
}
static {
System.out.print("r4 ");
}
}
class Hawk extends Raptor {
public static void main(String[] args) {
System.out.print("pre ");
new Hawk();
System.out.println("hawk ");
/*Raptor rap = new Raptor();
if (rap instanceof Raptor){
System.out.println("i m working");
}*/
}
}
Guess what is the answer: r1 r4 pre b1 b2 r3 r2 hawk
static always related with class not with instance so they run when the class initialized, next turn for constructor but here we have init blocks so first the init blocks have run then the constructor come in picture from top to bottom, super class init => constructor => child class init => constructor...
Subscribe to:
Posts (Atom)