Method Overriding in Java
Hello there, Welcome to programming scoop tutorial.Today we will learn what is method overriding in java and how it is useful and how it is done.
So let's start tutorial
What is Method Overriding?
Method Overriding is an OOP(Object Oriented Programming) feature that enables to change the behaviour of a method in subclass.
Basically method overriding is defining a method in subclass with the same signature with specific implementation in respect to subclass. So A child class can override a method that it inherits from a parent,thereby allowing the child class to add or change the behaviour of the method in the parent.
![]() |
method overriding |
Uses of Method Overriding:-
- Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
- Method overriding is used for runtime polymorphism.
Rules of Method Overriding:-
- The return type,method name and parameter list must be identical.
- The access specifier must be at least that of the parent. For example, if the parent method is public, the child must be public. If the parent method is protected, the child must be protected or public.
How to override a method?
Method overriding is done by changing by body of method of super class in sub class by keeping its signature same like return_type,parameters should be same.
Understand method overriding with example:-
It is a simple example to demonstrate how method overriding is done in this example we will override a method of super class in sub class.
Now I hope you understand the Concept of Mehtod Overloading in java. If you have any doubt or query in any program then feel to ask in comment section or any social media sites.
Method overriding is done by changing by body of method of super class in sub class by keeping its signature same like return_type,parameters should be same.
Understand method overriding with example:-
It is a simple example to demonstrate how method overriding is done in this example we will override a method of super class in sub class.
- Class A //super class
- {
- public void fun() //overriden method
- {
- System.out.println("Class A fun Method");
- }
- }
- class B extends A //sub class
- {
- public void fun() //overriding method in subclass
- {
- System.out.println("Class B fun Method");
- }
- }
- class main
- {
- public static void main(String args[])
- {
- B obj=new B();
- obj.fun();
- }
- }
![]() |
output |
Now I hope you understand the Concept of Mehtod Overloading in java. If you have any doubt or query in any program then feel to ask in comment section or any social media sites.
Visit more post on programmingscoop understand more concept of java
0 Comments