Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following code/classes. public class A { protected int x, y; public

ID: 3924337 • Letter: C

Question

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 {

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

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

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

(using java language)

Explanation / Answer

public class A {
   protected int x, y;

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

   public int M() {
       return 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";
   }

}

Overloading :
In the class B method M() is overloaded with no parameter and single parameter
   public int M() {
       return x - y;
   }

   public String M(int a) {
       z = a;
       return "GOODBYE";
   }
Overriding :
when class B is inherits from class A, then class A method M() is overrided in class B from class A

Note: overloading is occured in same class and overriding is occured in inheritance

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote