When Method Overriding occurs in Java?

Java 8Object Oriented ProgrammingProgramming

Method overriding occurs in Java if the child class has the same method as that declared in the parent class. The method in the child class has the same name and parameters as that of the method in the parent class. Method overriding is useful in runtime polymorphism.

A program that demonstrates this is given as follows:


 Live Demo

class A {
   int a;
   A(int x) {
      a = x;
   void print() {
      System.out.println("Value of a: " + a);
class B extends A {
   int b;
   B(int x, int y) {
      b = y;
   void print() {
      System.out.println("Value of b: " + b);
public class Demo {
   public static void main(String args[]) {
      B obj = new B(4, 9);


Value of b: 9
Updated on 30-Jul-2019 22:30:24