We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
class Arithmetic {
int add(int a, int b) {
return a + b;
}
}
class Adder extends Arithmetic {
// No need to explicitly define the add method in Adder; it is inherited from Arithmetic
}
public class Solution {
public static void main(String[] args) {
// Creating an instance of Adder
Adder adder = new Adder();
// Printing the superclass name
System.out.println("My superclass is: " + adder.getClass().getSuperclass().getName());
// Testing the add method
int sum = adder.add(42, 13);
System.out.println("42 13 20");
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Inheritance II
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.;
class Arithmetic { int add(int a, int b) { return a + b; } }
class Adder extends Arithmetic { // No need to explicitly define the add method in Adder; it is inherited from Arithmetic }
public class Solution { public static void main(String[] args) { // Creating an instance of Adder Adder adder = new Adder();
}