Professional Documents
Culture Documents
Day 2 Viva
Day 2 Viva
give an example:
Ans:- In Java, instanceof operator is used to check
“is-a” relationship at runtime.
example of instanceof operator
class FourWheeler
{
}
class Car extends FourWheeler
{
}
public class MyClass
{
public static void main(String[] args)
{
FourWheeler f=new Car();
System.out.println(f instanceof Car); // true
System.out.println(f instanceof
FourWheeler); //true
System.out.println(f instanceof Object); // true
}
}
2) What is upcasting and downcasting with
example?
4) Given, in C++,
MyClass m1;
MyClass m2=m1;
And in Java,
MyClass m1=new MyClass();
MyClass m2=m1;
What is the difference?
Ans:- in C++ It means “m2” object is initialized with
“m1”. i.e. for “m2” copy constructor is called, which
is provided by compiler implicitly.
So if you say….
MyClass m1=new MyClass();
MyClass m2=new MyClass(m1);
Copy constructor would be invoked for second object.
class PrintStream
{
public void print(){}
public void println(){}
}
if(A.getClass() == B.getClass())
{
System.out.println("A and B are instances of same
class");
}
Else
{
System.out.println("A and B are instances of
different class");
}
java MyClass
or
or
emp.staticmemberfun();
Whichever is first……..
b) Explicitly
Class.forName("classname");