Java Programming - Declarations and Access Control - Discussion


interface DoMath 
    double getArea(int rad); 
interface MathPlus 
    double getVol(int b, int h); 
/* Missing Statements ? */
which two code fragments inserted at end of the program, will allow to compile?
  1. class AllMath extends DoMath { double getArea(int r); }
  2. interface AllMath implements MathPlus { double getVol(int x, int y); }
  3. interface AllMath extends DoMath { float getAvg(int h, int l); }
  4. class AllMath implements MathPlus { double getArea(int rad); }
  5. abstract class AllMath implements DoMath, MathPlus { public double getArea(int rad) { return rad * rad * 3.14; } }

[A]. 1 only
[B]. 2 only
[C]. 3 and 5
[D]. 1 and 4

Answer: Option C


(3) are (5) are correct because interfaces and abstract classes do not need to fully implement the interfaces they extend or implement (respectively).

(1) is incorrect because a class cannot extend an interface. (2) is incorrect because an interface cannot implement anything. (4) is incorrect because the method being implemented is from the wrong interface.

Sushil said: (Dec 17, 2013)  
How 3 and 5 is correct as in 3 interface name is AllMath and in the 5 abstract class also name is AllMath?

How it is possible to have same name in the same package?

Tomnguyen said: (Dec 24, 2013)  
@Sushil: agree, but I think it means we can put either 3 or 5 to the end of code and it still runs.

Harshvardhan said: (Jun 13, 2015)  
How can in option 3 a class can extends an interface?

Interface can not be extended but can be implemented.

Alam said: (Dec 13, 2015)  

One interface can extend another interface.

Abc said: (Jul 4, 2016)  
I think correct answer is 3 Only.

5 is wrong : AllMath implements DoMath, MathPlus. Here method getVol with body is missing.

Vikram said: (May 12, 2018)  
I think correct answer is 3 Only.

In option 5, 'AllMath' is declared as an abstract class but does not contain even a single abstract method defined.

Kundan said: (May 28, 2019)  
How the return type will be different in option 2?

Nikhil said: (Jul 19, 2021)  
5th option is right because class is abstract.

Riblad said: (Nov 14, 2021)  
Option 3 is wrong, because an interface cannot implement another interface.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.