You are here: Home / Topics / Method Overloading example in Java

Method Overloading example in Java

Filed under: Java on 2023-09-17 07:00:19

// Program to explain Method Overloading.

class  A 
{
int i, j;

A(int a, int b) 
{
 i = a;
 j = b;
}

void show() 
{
 System.out.println("i and j: " + i + " " + j);
}
}

class  B extends A 
{
int k;

B(int a, int b, int c) 
{
 super(a, b);
 k = c;
}

void show(String msg) 
{
 System.out.println(msg + " " + k);
}
}

class  MethodOverriding2 
{
public static void main(String args[ ]) 
{
 B  b  = new B(1, 2, 3);

 b.show("This is k : "); // this calls show() in B
 b.show();     // this calls show() in A
}
}

 

Output:

This is k :  3
i  and  j:  1  2

About Author:
S
Shyam Dubey     View Profile
If you are good in any field. Just share your knowledge with others.