Java Programming - Declarations and Access Control - Discussion

Discussion Forum : Declarations and Access Control - Pointing out the correct statements (Q.No. 1)
1.
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; } }
1 only
2 only
3 and 5
1 and 4
Answer: Option
Explanation:

(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.

Discussion:
9 comments Page 1 of 1.

Riblad said:   4 years ago
Option 3 is wrong, because an interface cannot implement another interface.

Nikhil said:   4 years ago
5th option is right because class is abstract.

Kundan said:   6 years ago
How the return type will be different in option 2?

Vikram said:   7 years ago
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.

Abc said:   9 years ago
I think correct answer is 3 Only.

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

Alam said:   10 years ago
@Harshvardhan.

One interface can extend another interface.

Harshvardhan said:   1 decade ago
How can in option 3 a class can extends an interface?

Interface can not be extended but can be implemented.

Tomnguyen said:   1 decade ago
@Sushil: agree, but I think it means we can put either 3 or 5 to the end of code and it still runs.

Sushil said:   1 decade ago
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?

Post your comments here:

Your comments will be displayed after verification.