Java Inheritance II HackerRank Problem- Coder In Me

Java Inheritance II HackerRank Problem :

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.

Your classes should not be public.

Firstly know about Inheritance in JAVA

Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features(fields and methods) of another class.

Important Views On Inheritance :

  •  Super Class: The class whose features are inherited is known as super class(or a base class or a parent class).

 

  • Sub Class: The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
  • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

Input Format for Java Inheritance II

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 for Java Inheritance II

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

HINT : you have a locked code, only you define the code for inheritance property . Only you define the relationship between super class and sub class .

Code for Java Inheritance II HackerRank Problem :

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

 

 

 

Leave a reply:

Your email address will not be published.