Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

home / homework help / questions and answers / engineering / computer science /

ID: 645915 • Letter: H

Question

home / homework help / questions and answers / engineering / computer science / 1)which statement from s1 to s6 (see below) cause ...

Question

1)Which statement from s1 to s6 (see below) cause

compilation error? Assume the statement that causes

compilation is deleted, what is the output when the program is

executed?

class X{

void f1() { System.out.println("XXX"); }

void f2() { System.out.println("AAA"); }

}

class Y extends X{

void f1() { System.out.println("YYY"); }

void f1() { System.out.println("BBB"); }

}

class Z extends X{

void f1() { System.out.println("ZZZ"); }

}

class Test{

static void g(X a)

{ a.f1(); }

public static void main(String args[]){

Y y = new Y(); // s1

g(y); // s2

Object obj = new Y(); // s3

obj.f1(); // s4

X x = new Z(); // s5

x.f1(); // s6

2)

Explanation / Answer

class X
{
void f1()
{
System.out.println("XXX");
}

void f2()
{
System.out.println("AAA");
}

}


class Y extends X
{
void f1()
{
System.out.println("YYY");
}

void f2()
{
System.out.println("BBB");
}

}


class Z extends X
{

void f1()
{
System.out.println("ZZZ");
}

}


class Test
{

static void g(X a)

{
a.f1();
}

public static void main(String args[])
{

Y y = new Y(); // s1

g(y); // s2

Y obj = new Y(); // s3

obj.f1(); // s4

X x = new Z(); // s5

x.f1(); // s6
}
}