ADVERTISEMENT
ADVERTISEMENT

Java find output programs (Interface) | set 1

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.
Submitted by Nidhi, on February 03, 2021

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);
  }
}

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.

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);
  }
}

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.

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);
  }
}

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.

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);
  }
}

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.

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);
  }
}

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.



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.