---
title: "What is method overriding:"  
description: "What is method overriding:"  
author: "Samuel Fernandes"  
published: 2015-03-30  
updated: 2020-09-17  
canonical: https://www.mindstick.com/interview/2378/what-is-method-overriding  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# What is method overriding:

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.

In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

1-Method overriding is used to provide specific implementation of a method that is already provided by its super class.

2-Method overriding is used for runtime polymorphism

1. Rules :
2. method must have same name as in the parent class
3. method must have same parameter as in the parent class.
4. must be IS-A relationship (inheritance).

\
**Code Without Method Overriding**

```
class Vehicle{    void run(){System.out.println("Vehicle is running");}  }  class Bike extends Vehicle{        public static void main(String args[]){    Bike obj = new Bike();    obj.run();    }  } 
```

**Output****:**Vehicle is running\
\
Code using Method Overriding

```
class Vehicle{  void run(){System.out.println("Vehicle is running");}  }  class Bike2 extends Vehicle{  void run(){System.out.println("Bike is running safely");}    public static void main(String args[]){  Bike2 obj = new Bike2();  obj.run();  }  
```

**Output:**Bike is running safely\
\
\
\
\
\
\

## Answers

### Answer by Anonymous User

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.

In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

1-Method overriding is used to provide specific implementation of a method that is already provided by its super class.

2-Method overriding is used for runtime polymorphism

1. Rules :
2. method must have same name as in the parent class
3. method must have same parameter as in the parent class.
4. must be IS-A relationship (inheritance).

\
**Code Without Method Overriding**

```
class Vehicle{    void run(){System.out.println("Vehicle is running");}  }  class Bike extends Vehicle{        public static void main(String args[]){    Bike obj = new Bike();    obj.run();    }  } 
```

**Output****:**Vehicle is running\
\
Code using Method Overriding

```
class Vehicle{  void run(){System.out.println("Vehicle is running");}  }  class Bike2 extends Vehicle{  void run(){System.out.println("Bike is running safely");}    public static void main(String args[]){  Bike2 obj = new Bike2();  obj.run();  }  
```

**Output:**Bike is running safely\
\
\
\
\
\
\


---

Original Source: https://www.mindstick.com/interview/2378/what-is-method-overriding

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
