Please explain the answers. Your help is appreciated! Please explain the answers
ID: 3580197 • Letter: P
Question
Please explain the answers. Your help is appreciated!
Please explain the answers. Your help is appreciated!
This question refers to the following Java classes D2 and D3: class D2 {int x, y; public D2(int x, int y){this.x = x; this.y = y;} public boolean equals(Object o){if (!(o instanceof D2)){return false;} else {D2 d = (D2)o; return d.x == x && d.y -== y;}}} class D3 extends D2 {int z; public D3(int x, int y, int z){super(x, y); this.z = z;} public boolean equals(Object o){if (!(o instanceof D3)){return false;} else {D3 d = (D3)o; return d.z == z && super.equals(o);}}} Give the output of each of the following code snippets: 2.1 D3 v1 = new D3(0, 0, 0); D2 v2 = v1; System, out .println(v1==(D3)v2); 2.2 D2 v1 = new D3(0, 0, 0); D3 v2 = new D3(0, 0, 0); System, out .println(v1 == v2); 2.3 D2 v1 = new D2(0, 0); D3 v2 = new D3(0, 0, 0); System.out.println(v1.equals(v2)); 2.4 D2 v1 = new D2(0, 0); D3 v2 == new D3(0, 0, 0); System.out.println(v2.equals(v1));Explanation / Answer
2.1.It will print true.Because == sign will just compare type of objects not its contents.Hence v2 (which is casted to D3) will be equal to v1(which is type of D3)
2.2.It will print false.Because == sign will just compare type of reference objects not its contents.Hence v2 (which is type of reference D3) will not be equal to v1(which is type reference of D2).Hence it gives false.
2.3It will print true.Because equals method we have overided and provided own method which will compare contents of objects.Both contents are of same i.e. x=0,y=0,z=0;Hence,it is true.but keep in mind it will call equals method of D2 class.(D3 is always instance of D2 because D3 is derived class and D2 is base class)
2.4It will print False.Even both have same content but it will call equals method of D3 class.In that method,
if(!(o instanceof D3))
{
return false;
}
Above will return return false,because Parent class(D2) cannot be instance of base class(D3).Hence,it return false
=======================================================================================
package snippet;
public class D2 {
int x,y;
public D2(int x,int y)
{
this.x=x;
this.y=y;
}
public boolean equals(Object o)
{
if(!(o instanceof D2))
{
return false;
}
else
{
D2 d=(D2)o;
return d.x==x&&d.y==y;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
D3 v1=new D3(0,0,0);
D2 v2=v1;
System.out.println(v1==(D3)v2);
*/
/*D2 v1=new D3(0,0,0);
D3 v2=new D3(0,0,0);
System.out.println(v1==v2);*/
/*D2 v1=new D2(0,0);
D3 v2=new D3(0,0,0);
System.out.println(v1.equals(v2));*/
D2 v1=new D2(0,0);
D3 v2=new D3(0,0,0);
System.out.println(v2.equals(v1));
}
}
class D3 extends D2
{
int z;
public D3(int x,int y,int z)
{
super(x,y);
this.z=z;
}
public boolean equals(Object o)
{
if(!(o instanceof D3))
{
return false;
}
else
{
D3 d=(D3)o;
return d.z==z&&super.equals(o);
}
}
}
=======================================================
Output:
false
======================================================
Given code.Strip the comments and try each input