Q:

Java find output programs (Interface) | set 2

belongs to collection: Java find output programs

0

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

Question 1:

interface ICalc {
  int num1 = 10;
  int num2 = 30;

default void Addition() {
    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();
  }
}

Question 2:

interface ICalc {
  int num1 = 10;
}

public class InfEx implements ICalc {
  int num2 = 30;
  public static void main(String[] args) {
    int sum = 0;

    sum = num1 + num2;

    System.out.println("Sum: " + sum);
  }
}

Question 3:

interface ICalc {
  int num1 = 10;
}

public class InfEx implement ICalc {
  static int num2 = 30;
  public static void main(String[] args) {
    int sum = 0;

    sum = num1 + num2;

    System.out.println("Sum: " + sum);
  }
}

Question 4:

interface ICalc {
  abstract void Addition(int num1, int num2);
  abstract 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 5:

abstract interface ICalc {
  abstract void Addition(int num1, int num2);
default void Subtraction(int num1, int num2) {
    int sub = 0;

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

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

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

}

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

All Answers

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

Answer Question 1:

Output:

Addition: 40

Explanation:

In the above program, we created an interface ICalc that contains num1 and num2 data members initialized with 10 and 30 respectively. Here, we also defined the 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: 40" on the console screen.

Answer Question 2:

Output:

/InfEx.java:10: error: non-static variable num2 cannot be 
referenced from a static context
    sum = num1 + num2;
                 ^
1 error

Explanation:

The above program will generate syntax error because here we declared a non-static num2 data member in the InfEx class and then we accessed num2 in the static method main(), and we know that we cannot access non-static members from a static method.

Answer Question 3:

Output:

/InfEx.java:5: error: '{' expected
public class InfEx implement ICalc {
                  ^
1 error

Explanation:

The above program will generate syntax error because we used the implement keyword instead of the implements keyword in the above program.

Answer Question 4:

Output:

Addition: 100
Subtraction: 20

Explanation:

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

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 5:

Output:

Addition: 100
Subtraction: 20

Explanation:

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

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.

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 3... >>
<< Java find output programs (Interface) | set 1...