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 {
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)
Solution
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

