Q:

Java find output programs (Interface) | set 1

belongs to collection: Java find output programs

0

Find the output of Java programs | Interface | Set 1: Enhance the knowledge of Java Interface concepts by solving and finding the output of some Java programs.

Question 1:

interface ICalc {
  public void Addition(int num1, int num2);
  public void Subtraction(int num1, int num2);
}

class Calc extends ICalc {
  public void Addition(int num1, int num2) {
    int sum = 0;

    sum = num1 + num2;
    System.out.println("Addition: " + sum);
  }

  public void Subtraction(int num1, int num2) {
    int sub = 0;

    sub = num1 - num2;
    System.out.println("Subtraction: " + sub);
  }
}

public class InfEx {
  public static void main(String[] args) {
    Calc C = new Calc();
    C.Addition(40, 60);
    C.Subtraction(60, 40);
  }
}

Question 2:

interface ICalc {
  public void Addition(int num1, int num2);
  public void Subtraction(int num1, int num2);
}

class Calc implements ICalc {
  public void Addition(int num1, int num2) {
    int sum = 0;

    sum = num1 + num2;
    System.out.println("Addition: " + sum);
  }

  public void Subtraction(int num1, int num2) {
    int sub = 0;

    sub = num1 - num2;
    System.out.println("Subtraction: " + sub);
  }
}

public class InfEx {
  public static void main(String[] args) {
    Calc C = new Calc();
    C.Addition(40, 60);
    C.Subtraction(60, 40);
  }
}

Question 3:

interface ICalc {
  public void Addition(int num1, int num2);
  public void Subtraction(int num1, int num2);
}

class Calc implements ICalc {
    @Override
  public void Addition(int num1, int num2) {
    int sum = 0;

    sum = num1 + num2;
    System.out.println("Addition: " + sum);
  }

  @Override
  public void Subtraction(int num1, int num2) {
    int sub = 0;

    sub = num1 - num2;
    System.out.println("Subtraction: " + sub);
  }
}

public class InfEx {
  public static void main(String[] args) {
    Calc C = new Calc();
    C.Addition(40, 60);
    C.Subtraction(60, 40);
  }
}

Question 4:

interface ICalc {
  public void Addition(int num1, int num2) {
    int sum = 0;

    sum = num1 + num2;
    System.out.println("Addition: " + sum);
  }
}

public class InfEx implements ICalc {
  public static void main(String[] args) {
    InfEx C = new InfEx();
    C.Addition(40, 60);
  }
}

Question 5:

interface ICalc {
default void Addition(int num1, int num2) {
    int sum = 0;

    sum = num1 + num2;
    System.out.println("Addition: " + sum);
  }
}

public class InfEx implements ICalc {
  public static void main(String[] args) {
    InfEx C = new InfEx();
    C.Addition(40, 60);
  }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question 1:

Output:

/InfEx.java:6: error: no interface expected here
class Calc extends ICalc {
                   ^
1 error

Explanation:

The above program will generate syntax error because here we need to use implements keyword to implement methods in the class. Here, we want to implement Addition() and Subtraction() method in Calc class.

Answer Question 2:

Output:

Addition: 100
Subtraction: 20

Explanation:

In the above program, we created an interface ICalc that contains a declaration of Addition() and Subtraction() method. Then we implemented methods Addition() and Subtraction() of ICalc interface into Calc class.

Now look to the main() method of the InfEx class. Here, we created object C of Calc class. Then we called Addition() and Subtraction() methods using object C.

Answer Question 3:

Output:

Addition: 100
Subtraction: 20

Explanation:

In the above program, we created an interface ICalc that contains a declaration of Addition() and Subtraction() method. Then we implemented methods Addition() and Subtraction() of ICalc interface into Calc class.

Here, we used the "@Override" modifier in the Calc class in the method definition but it is not required to implement interface methods in java.

Now look to the main() method of InfEx class - Here we created object C of Calc class. Then we called Addition() and Subtraction() methods using object C.

Answer Question 4:

Output:

/InfEx.java:2: error: interface abstract methods cannot have body
  public void Addition(int num1, int num2) {
                                           ^
/InfEx.java:10: error: InfEx is not abstract and does not 
override abstract method Addition(int,int) in ICalc
public class InfEx implements ICalc {
       ^
/InfEx.java:13: error: cannot find symbol
    C.Addition(40, 60);
     ^
  symbol:   method Addition(int,int)
  location: variable C of type InfEx
3 errors

Explanation:

The above program will generate syntax errors because here we defined the Addition() method within the interface ICalc, we cannot define methods within the interface directly, we need to use the default keyword to define methods within the interface in Java.

Answer Question 5:

Output:

Addition: 100

Explanation:

In the above program, we created an interface ICalc, here we defined Addition() method within the interface using the default keyword. Then we implemented the ICalc interface in the InfEx class.

Now look to the main() method, here we created the object of InfEx class and called Addition() method that will print "Addition: 100" on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java find output programs (Interface) | set 2... >>
<< Java find output programs (Inheritance) | set 3...