Consider the following codeclasses public class A protected

Consider the following code/classes.

public class A {

protected int x, y;

public A( ) { x = 1; y = 1;}

public int M( ) { return x * y;}

public double M( ) { return (double) x + y;}

} // end class A

public class B extends A {

protected int z;

public B( ) { z = 2; }

public int M( ) { return x – y;}

public String M( int a ) { z = a; return “GOODBYE”;}

public double M( ) { return 0.0; }

} // end class B

Precisely where does overloading occur in the code? Be sure to list all occurrences of overloading.

Where does overriding occur in the code? Be sure to list all occurrences of overriding.

Solution

Hi, Please find my explanation.

Please let me know in case of any issue.


public class A {
   protected int x, y;
   public A( ) { x = 1; y = 1;}
   public int M( ) { return x * y;}
   public double M( ) { return (double) x + y;}
} // end class A

public class B extends A {
   protected int z;
   public B( ) { z = 2; }
   public int M( ) { return x – y;}
   public String M( int a ) { z = a; return “GOODBYE”;}
   public double M( ) { return 0.0; }
} // end class B


Hi friend there are error is both classes:
   One class can not contains same method(Duplicate):

   Calss A has :
       public int M( ) { return x * y;}
       public double M( ) { return (double) x + y;}

       these two methods are duplicate methods, they are not iverloaded. Just changing the return type, it does not means they are
       overloaded. To overload you need to change parameter list or order of parameters.

   CLass B has :
       public int M( ) { return x – y;}
       public double M( ) { return 0.0; } : duplicate methods same as A

   Any combination:

       public String M( int a ) { z = a; return “GOODBYE”;} FROM class B
       public int M( ) { return x – y;} FROM class B

       OR
       public String M( int a ) { z = a; return “GOODBYE”;} FROM class B
       public double M( ) { return 0.0; } FROM class B

       OR
       public String M( int a ) { z = a; return “GOODBYE”;} FROM class B
       public int M( ) { return x * y;} FROM class A

       OR
       public String M( int a ) { z = a; return “GOODBYE”;} FROM class B
       public double M( ) { return (double) x + y;} FROM class A


       They are overloaded

   Any combination:
       public int M( ) { return x * y;} FROM class A
       public int M( ) { return x – y;} FROM class B

       OR
       public double M( ) { return (double) x + y;} FROM class A
       public double M( ) { return 0.0; } FROM class B

       These combination are oveloaded

Consider the following code/classes. public class A { protected int x, y; public A( ) { x = 1; y = 1;} public int M( ) { return x * y;} public double M( ) { ret
Consider the following code/classes. public class A { protected int x, y; public A( ) { x = 1; y = 1;} public int M( ) { return x * y;} public double M( ) { ret

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site