Note that there are some explanatory texts on larger screens.

plurals
  1. POAn abstract class in Java need not implement any methods from its implementing interface. Why?
    primarykey
    data
    text
    <p>Let's look at the following simple code snippet in Java.</p> <pre><code>interface Sum { abstract public void showSum(); } interface Mul { abstract public void showMul(); } abstract class Super implements Sum { protected int x; protected int y; public Super(int x, int y) { this.x=x; this.y=y; } //No error, though the method showSum() of the implementing iterface Sum is commented. Why? /*public void showSum() { System.out.println("Sum = "+(x+y)); }*/ } final class Calculation extends Super implements Mul { public Calculation(int x, int y) { super(x,y); } public void showSum() { System.out.println("Summation = "+(x+y)); } //If showMul() is commented , it would issue a compile-time error. Why? public void showMul() { System.out.println("Multiplication = "+(x*y)); } } final public class Main { public static void main(String[] args) throws IOException { Scanner s=new Scanner(System.in); System.out.print("\nEnter a number : "); int x=s.nextInt(); System.out.print("\nEnter another number : "); int y=s.nextInt(); Calculation c=new Calculation(x,y); c.showSum(); c.showMul(); } } </code></pre> <hr> <p>Since the interface <strong>Sum</strong> containing only one method <code>showSum();</code> is being implemented by the abstract class <strong>Super</strong> , there should be necessary for the abstract class <strong>Super</strong> to implement that method <code>showSum();</code>. The compiler however doesn't complain at all and the program is working well with no problem at all. Why?</p> <hr> <p>Similarly, the non-abstract final class <strong>Calculation</strong> is implementing the <strong>Mul</strong> interface and contains the actual implementation of the method <code>showMul();</code> presented in it's implementing interface. In case, if this method <code>showMul()</code> in the class <strong>Calculation</strong> is commented, it issues a compile-time error. Why is the same thing not applicable to that abstract class <strong>Super</strong>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload