Question 6 (6 pts)
The interface SayHello is defined as followed:
public interface SayHello
{
void printGreeting( );
}
Write the statements to instantiate an object of the anonymous class that implements the above interface by using the following definition of printGreeting( ) and then call the instance method printGreeting( ).
void printGreeting( )
{
System.out.println(“Hello guys”);
}
Question 7 (8 pts)
The abstract class SayHello and the static method process are defined as followed:
class SayHello
{
private String greeting;
SayHello( )
{
greeting = “Hello guys”;
}
SayHello( String wts )
{
greeting = wts;
}
void printGreeting( );
}
static void process( SayHello obj )
{
obj.printGreeting( );
}
Write the statements to instantiate an object (with its instance variable initialized with the string “Bonjour mes amis” ) of the anonymous class that extends this abstract class by using the following definition of printGreeting( ); then write a statement to call method process by passing to it that object.
void printGreeting( )
{
System.out.println( greeting );
}
Answer 6:
SayHello s = new SayHello() {
public void
printGreeting()
{
System.out.println("Hello guys");
}
};
s.printGreeting();
Answer 7:
SayHello s = new SayHello("Bonjour mes amis") {
@Override
void
printGreeting() {
System.out.println( greeting );
}
};
s.process(s);
Get Answers For Free
Most questions answered within 1 hours.