Consider the following Java interface and class definitions: public interface X
ID: 3858441 • Letter: C
Question
Consider the following Java interface and class definitions: public interface X { int getX(): } public interface U { int getY(): } class C implements X { public int getX() { return 1: } } class D extends C implements Y { public int getX () { return 2: } public int getY() { return getX() + getx(): } class E extends C { public int getY() { return 3: } } For each code snippet below, what value that will be printed out D d1 = new D(): System.out.println (d1.getY()): a. 3 b. 9 c. 4 d. none of the above Answer: C c1 = new D(): System.out.printin (cl.getX()): a. 2 b. 3 c. 1 d. None of the above Answer: X x1 = new C(): System.out.printin(x1.getX()): a. 5 b. 1 c. 0 d. None of the above Answer:Explanation / Answer
The java code for the given program is
//defining interface X
interface X
{
int getX();
}
//defining interface Y
interface Y
{
int getY();
}
//class C which inherits the interface X
class C implements X
{
//defining method getX() which returns a value 1
public int getX()
{
return 1;
}
}
//class D which inherits the interface Y
class D extends C implements Y
{
//defining method getX() which returns a value 2
public int getX()
{
return 2;
}
//defining method getY() which returns a value of addition of both getX()+getY()
public int getY()
{
return getX()+getX();
}
}
//class E which inherits class C
class E extends C
{
//defining method getY() which returns value of 3.
public int getY()
{
return 3;
}
}
//creating class interfaces
public class interfaces2
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
//creating object for class D
D d1=new D();
//displaying a value by calling a method getY() which returns a value
System.out.println(d1.getY());
C c1=new D();
System.out.println(c1.getX());
X x1=new C();
System.out.println(x1.getX());
}
}
The output of the above program is:4
2
1
6.Answer:c. 4
D d1=new D();
System.out.println(d1.getY());
the output is:4
7.Answer: a. 2
C c1=new D();
System.out.println(c1.getX());
the output is 2.
8.Answer:b.1
X x1=new C();
System.out.println(x1.getX());
output is:1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.