Skip to content
the poor coder

Hackerrank Java Inheritance II Solution

Poor Man
Published
Updated
Reading time
1 min read

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:

Text
My superclass is: Arithmetic
42 13 20

Solution in java8#

Approach 1.

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

class Adder extends Arithmetic {
  
}

Approach 2.

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

class Adder extends Arithmetic {
    
}

Approach 3.

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

class Adder extends Arithmetic{
    
}

Keep going

ESC