Java Inheritance | Practice | GeeksforGeeks

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.
Consider the following class cls1:
class cls1
{
    void add(int p,int q){
        System.out.println(p+q);
    }
}
There is another class cls2 which extends class cls1:
class cls2 extends cls1
{
    void mul(int p,int q)
    {
        System.out.println(p*q);
    }
}
So we can make object of a class cls2, which can use both mul and add methods. Main function is already created in the editor and instance of cls2 is also made. So the task is to add a method in class cls2 naming task() which will take 2 parameters as input i.e. a and b and print the sum of their squares i.e a2+b2. Changes in the main function has been made already.
Input:
The input line contains T, denotes the number of testcase. Each testcase contains one line containing a and b separated by space.
Output:
For each testcase in new line, print the required answer.
Constraints:
1 <= T <= 100
1 <= a, b <= 103
Example:
Input:
1
2 4
Output:
6
8
20
Explanation:
Testcase 1: Output contains 3 lines:
In 1st line sum of two numbers is printed from the method add().
In 2nd line multiplication of two nos is printed from the method mul().
In 3rd line sum of the sqaures of the two nos is printed using your created method task().
0
uttkarshrai20001 month ago
class cls2 extends cls1{   void add(int p,int q){      System.out.println(p+q);    }   void mul(int p,int q)   {       System.out.println(p*q);   }   void task(int p,int q)   {       //Add your code here.       System.out.println(p*p + q*q);   }}
0
whitehatonion1 month ago
class cls2 extends cls1{   void mul(int p,int q)   {       System.out.println(p*q);   }   void task(int p,int q)   {       //Add your code here.       System.out.println((p*p)+(q*q));   }} 
0
rohitpandey484Premium2 months ago
Java Solution 
class cls2 extends cls1{   //void add(int p,int q)   //{       //System.out.println(p+q);   //}   void mul(int p,int q)   {       System.out.println(p*q);   }   void task(int p,int q)   {      // int a=Math.pow(p,2);       //int b=Math.pow(q,2);       p*=p;       q*=q;       System.out.println(p+q);     // return (p+q);   }}
0
dkumarmgs5720
This comment was deleted.
0
mdsaif04052 months ago
class cls2 extends cls1
{
    void mul(int p,int q)
    {
        System.out.println(p*q);
    }
    void task(int p,int q)
    {
        System.out.println((p*p)+(q*q));
    }
}
0
shantanupatil9583 months ago
class cls2 extends cls1{   void mul(int p,int q)   {       System.out.println(p*q);   }   void task(int p,int q)   {      System.out.println(p*p+q*q);   }}    class Main{        public static void main(String[] args){            Scanner sc = new Scanner(System.in);            int t=sc.nextInt();            cls2 obj1=new cls2();            for(int i=0;i<t;i++){                int a=sc.nextInt();                int b=sc.nextInt();                obj1.add(a,b);                obj1.mul(a,b);                obj1.task(a,b);            }        }    }
0
gauravverma9918187053 months ago
class cls1
{
void add(int p,int q)
{
System.out.println(p+q);
}
}
class cls2 extend cls1
{
void mul(int p,int q)
{
System.out.println(p*q);
}
}
void task(int p,int q)
{
System.out.println((p*p)+(q*q));
}

0
shubhamtopare17124 months ago
class cls2 extends cls1{   void mul(int p,int q)   {       System.out.println(p*q);   }   void task(int p,int q)   {       add((p*p),(q*q));//Need to use inheritance i.e. existing code   }}
-1
mdaliakbar16014 months ago
class cls2 extends cls1{   void mul(int p,int q)   {       System.out.println(p*q);   }   void task(int p,int q)   {       //Add your code here.       System.out.println((p*p)+(q*q));   }} 
0
pankajkumarravi6 months ago
**************** Java Code *******
class cls2 extends cls1{void mul(int p,int q) {       System.out.println(p*q);   }   void task(int p,int q) {               System.out.println((p*p)+(q*q));   }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.