Hackerrank Java Inheritance II Solution

Hackerrank Java Inheritance II Solution

.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

Write the following code in your editor below:

  1. A class named Arithmetic with a method named add that takes  integers as parameters and returns an integer denoting their sum.
  2. A class named Adder that inherits from a superclass named Arithmetic.

Your classes should not be be .

Input Format

You are not responsible for reading any input from stdin; a locked code stub will test your submission by calling the add method on an Adder object and passing it  integer parameters.

Output Format

You are not responsible for printing anything to stdout. Your add method must return the sum of its parameters.

Sample Output

The main method in the Solution class above should print the following:

My superclass is: Arithmetic
42 13 20

Solution in java8

Approach 1.

class Arithmetic {
  int add(int a, int b) {
    return a + b;
  }
}

class Adder extends Arithmetic {
  
}

Approach 2.

//Write your code here
class Arithmetic {
    public int add(int a, int b) {
        return a + b;
    }
}

class Adder extends Arithmetic {
    
}

Approach 3.

//Write your code here
class Arithmetic{
    public int add(int x,int y){
        return x+y;
        
    }
}

class Adder extends Arithmetic{
    
}

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe