Java OCA OCP Practice Question 2335

Question

Consider the following definition of class Command:

abstract class Command { 
    abstract void run();            
} 

Which of the classes correctly subclass Command? (Choose all that apply).


a  class Me extends Command { 
       void run() {/* ... */} 
   }   // ww  w .j  a v  a 2 s  . c o m

b  abstract class You extends Command { 
       void run() {/* ... */} 
   } 

c  interface Run { 
       void run(); 
   } 
   class Her extends Command implements Run { 
       void run() {/* ... */} 
   } 

d  abstract class His extends Command { 
       String run() {/* ... */} 
   } 


a, b

Note

When a class extends another class or implements an interface, the methods in the derived class must be either valid overloaded or valid overridden methods.

Option (c) is incorrect.

The concrete class Her extends Command and implements Run.

To compile Her, it must implement run() with public access so that it implements run() with default access in class Command and run() with public access in interface Run:.

class Her extends Command implements Run { 
   public void run() {} 
} 

Because class Her in option (c) defines run() with default access, it fails to implement public run() from interface Run and fails compilation.

Option (d) is incorrect.

Method run() defined in class His and method run() defined in class Command don't form either valid overloaded or overridden methods.




PreviousNext

Related