0% found this document useful (0 votes)
26 views6 pages

Lab 5 - Ans

The document discusses syntax errors in code examples and output of code examples. It also differentiates between method overriding and overloading in code examples. The document contains code snippets and explanations for finding errors, expected output, and identifying overriding vs overloading.

Uploaded by

abdoahmed85577
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

Lab 5 - Ans

The document discusses syntax errors in code examples and output of code examples. It also differentiates between method overriding and overloading in code examples. The document contains code snippets and explanations for finding errors, expected output, and identifying overriding vs overloading.

Uploaded by

abdoahmed85577
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming 2 – 2023/2024

Lab 5
Find the syntax Errors in the following programs:
1.
class E {}
class F{}
class G extends E,F{}

Ans: Class cannot extend multiple classes.


2.
class M {
private int u, v, w;
public M() {
}
public M(int x, int y, int z) {
u = x;
v = y;
w = z;
}
}
class N extends M {}
class Program {
public static void main(String[] args) {
M m1 = new M(3, 2, 7);
N n1 = new N(10, 15, 9);
}
}
Ans: Expected 0 arguments but found 3 Because Subclass doesn’t inherit Constructor of the
Super Class.
Programming 2 – 2023/2024
Lab 5
What is the output of the following programs:
class X{
private int a;
public X(){
System.out.println("This is a test");
}
public X(int m){
System.out.println("Midterm");
}
}
class Y extends X{
private int b;
public Y(){
System.out.println("Hello");
}
public Y(int a){
super(a);
System.out.println("Welcome to Java");
}
public Y(int a, int b){
System.out.println("I am
fine");
}
}
public class Program {
public static void main(String[] args) {
Y y3 = new Y(7,11);
Y y1 = new Y();
Y y2 = new Y(5);} }

Ans:
Programming 2 – 2023/2024
Lab 5
Method Override or Overload: Identify the Correct Type
1.
class A {

public void print() {

System.out.println("Hello from A");

class B extends A {

public void print() {

System.out.println("Hello from B");

public class Main {

public static void main(String[] args) {

B obj = new B();

obj.print();

Ans: Overriding
Programming 2 – 2023/2024
Lab 5
2.
class A {

public void calculate(int a, int b) {

int result = a + b;

System.out.println("Result: " + result);

class B extends A {

public void calculate(int a, int b, int c) {

int result = a + b + c;

System.out.println("Result: " + result);

public class Main {

public static void main(String[] args) {

B obj = new B();

obj.calculate(5, 10, 15);

Ans: Overloading
Programming 2 – 2023/2024
Lab 5
3.
class A {

public void display(int x) {

System.out.println("Value of x: " + x);

class B extends A {

public void display(double y) {

System.out.println("Value of y: " + y);

public class Main {

public static void main(String[] args) {

B obj = new B();

obj.display(5.5);

Ans: Overloading
Programming 2 – 2023/2024
Lab 5
4.
class A {

public void print(String message) {

System.out.println("Message: " + message);

class B extends A {

public void print(String message, int count) {

for (int i = 0; i < count; i++) {

System.out.println("Message: " + message);

public class Main {

public static void main(String[] args) {

A obj1 = new A();

B obj2 = new B();

obj2.print("Hi", 3); // Method Call 1

((A) obj2).print("Hey"); // Method Call 2

Ans:
Method Call 1: Overloading
Method Call 2: Overriding

You might also like