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);
}
}
Answer Question 1:
Output:
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:
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:
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:
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:
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