Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Worksheet –

2.1

Name :BHUMIKA KHANNA Branch : CSE

UID :20BCS1271 Subject : Java lab

Aim of the practical:

Write a program to differentiate between method overloading and


method overriding.

CODE FOR Method Overloading:


class BOX {
void Add() {
int a = 4;
int b = 8;
int sum;
sum = a +
b;
System.out.println("Sum=" + sum);
}

void Add(int m, int n)


{ int sum;
sum = m + n;
System.out.println("Sum of " + m + " and " + n + "=" + sum);

void Add(int p, int q, int r) {


int sum;
System.out.println("sum of " + p + "," + q + " and " + r + "=" +
sum);

public class Method_Overloading {

public static void main(String[] args) {


System.out.println("-----> Method Overloading<
------------------------------------------------------------------------------------")
;

BOX b = new BOX();


b.Add();
b.Add(4, 5);
b.Add(1, 2, 3);

Output:
CODE FOR PARAMETERIZED CONSTRUCTOR:
class hello {
void text() {
System.out.println("Hello");
}
}

class how extends hello {


void text() {
System.out.println("Ram");
}
}

public class Method_Overriding {


public static void main(String[] args) {
System.out.println("|----------->Method Overriding< ------------|");
hello h1 = new hello();
h1.text();
hello h2 = new
how(); h2.text();
}
}

Output:

You might also like