Inheritance I I HackerRank Problem

In Object-Oriented Programming (OOP), inheritance is a key concept where one class acquires the properties (fields) and methods of another. With the use of inheritance, information is made manageable in a hierarchical order. The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Inheritance I I HackerRank Problem HackerRank solution using CodeRankGPT:

In this code, there are two classes: 'Arithmetic' and 'Adder'. The 'Arithmetic' class has a method 'add' that takes two integers as parameters and returns their sum. The 'Adder' class extends the 'Arithmetic' class, inheriting its 'add' method. In the 'main' method, an instance of the 'Adder' class is created and used to call the 'add' method. The superclass of the 'Adder' class is also printed to the console, demonstrating the inheritance relationship.


/**
 * 
 */
package com.javaaid.hackerrank.solutions.languages.java.oop;

/**
 *
 */

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

class Adder extends Arithmetic {

}

public class JavaInheritanceII {
	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");
	}
}

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform