Skip to main content

Java Inheritance II HackerRank solution with explanation

Java Inheritance 2 HackerRank

Problem Description :

Write the following code in your editor below:

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

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 2 integer parameters.

Output Format :

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

See full Problem Description :

Solution Explanation :

In this problem, simply you have to create two new class : Arithmetic and Adder.

Arithmetic class contains only one method : add. add method have two integer parameters and return addition of two parameters with int value.

Adder class extends Arithmetic class and does not contains own method.

Lets see solution in code.

Solution : 

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

 

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

class Adder extends Arithmetic {
    
}


public class Solution{
    public static void main(String []args){
        // Create a new Adder object
        Adder a = new Adder();
        
        // Print the name of the superclass on a new line
        System.out.println("My superclass is: " + a.getClass().getSuperclass().getName());   
        
        // Print the result of 3 calls to Adder's `add(int,int)` method as 3 space-separated integers:
        System.out.print(a.add(10,32) + " " + a.add(10,3) + " " + a.add(10,10) + "\n");
     }
}

Output :

My superclass is: Arithmetic

42 13 20

Explanation :

  • In main method, there is Adder class object (Adder a = new Adder()). 
  • In first print statement, it is printing our super class name using Adder class object "a".
  • In second print statement, it is calling our add method using Adder class object. Since we are extending Arithmetic class so we can get all properties of Arithmetic class in Adder class and use all properties of Arithmetic class. 
  • So when code calls a.add() method, Adder class does not have any own add() method so it is calling Arithmetic class add() method and return addition of two parameter.


Happy Coding.

See other HackerRank and LeetCode probelm and Solution with explanation :

Comments

Popular posts from this blog

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.st

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And last