Method overriding in Java language

Method overriding in Java language

In this tutorial, we will discuss the concept of method overriding in Java language.

We have already looked at method overloading in Java. Now, we will learn method overriding in Java. Method overriding looks like method overloading with some difference.

When a subclass has the same method as the one declared in the parent class, it is called method overriding in java.

Conditions for Java Method Overriding

  • The method must have the same signature as the base class.
  • The method must have the same parameter as the base class.
  • must be IS-A relationship with extends(inheritance)

Example

Syntax

Syntax
The diagram below explains the above two classes (School1 and School2) including their methods and their relationship.
Program 1
Example
Above is a single inheritance program.It contains two classes, one vehicle is the parent class and another vehicle is the child class. Class vehicles inherit the properties of the vehicle class.
Program 2

 

Example

Above program is a single inheritance program. It contains two classes: one vehicle is the parent class and the other vehicle is the child class. The same method is inside every class. Class vehicles inherit the properties of vehicle class.
One object is created under the main method.
When a method is called through an object, derived classes override the base class.

Program 3
Example

Program 4

Example

Explanation for the above program

Example

Related Post

Abstract class in C++                                           Abstract class in Java

Encapsulation in C++                                       Encapsulation in Java

Polymorphism in C++                                      Method overriding in Python

Method overloading in java                             Constructor overloading in Java

Exception Handling in Java                                    

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.