HackerRank Java- Inheritance II




Write the following code in your editor below:

A class named Arithmetic with a method named add that takes 2 integers as parameters and returns an integer denoting their sum.
A class named Adder that inherits from a superclass named Arithmetic.
Your classes should not be be public.



  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6. class Arithmetic {
  7. int add(int x, int y) {
  8. return x + y;
  9. }
  10. }
  11. class Adder extends Arithmetic {
  12. Adder() {}
  13. }
  14. public class Solution{
  15. public static void main(String []args){
  16. // Create a new Adder object
  17. Adder a = new Adder();
  18. // Print the name of the superclass on a new line
  19. System.out.println("My superclass is: " + a.getClass().getSuperclass().getName());
  20. // Print the result of 3 calls to Adder's `add(int,int)` method as 3 space-separated integers:
  21. System.out.print(a.add(10,32) + " " + a.add(10,3) + " " + a.add(10,10) + "\n");
  22. }
  23. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube