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

Method overriding example in Java

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

// Program to explain Method Overriding.

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() 
{
 System.out.println("k: " + k);
}
}

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

 subOb.show();   // this calls show() in B
}
}

 

Output:

k: 3

About Author:
M
Mr. Dubey     View Profile
Founder and CEO of MCQ Buddy. I just like to help others. This portal helps students in getting study material free.