0% found this document useful (0 votes)
26 views7 pages

Method Overriding

This document discusses polymorphism and method overriding in Java. Polymorphism allows performing the same action in different ways. Method overriding allows subclasses to provide specific implementations of methods declared in parent classes. The document provides examples of method overriding and discusses the rules that must be followed for it to occur.

Uploaded by

sara.gaikwad.45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

Method Overriding

This document discusses polymorphism and method overriding in Java. Polymorphism allows performing the same action in different ways. Method overriding allows subclasses to provide specific implementations of methods declared in parent classes. The document provides examples of method overriding and discusses the rules that must be followed for it to occur.

Uploaded by

sara.gaikwad.45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

• Polymorphism in Java

• Polymorphism in Java is a concept by which we can perform a single


action in different ways. Polymorphism is derived from 2 Greek
words: poly and morphs. The word "poly" means many and "morphs"
means forms. So polymorphism means many forms.
• There are two types of polymorphism in Java: compile-time
polymorphism and runtime polymorphism. We can perform
polymorphism in java by method overloading and method overriding.
Method Overriding in Java

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 a subclass provides the specific implementation of the
method that has been declared by one of its parent class, it is known as
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

1.Understanding the problem without method overriding


2.Can we override the static method
3.Method overloading vs. method overriding
Rules for Java Method Overriding
1.The method must have the same name as in the parent class
2.The method must have the same parameter as in the parent
class.
3.There must be an IS-A relationship (inheritance).
understanding the problem without method overriding
Let's understand the problem that we may face in the program if we don't use method overriding.
1.//Java Program to demonstrate why we need method overriding
2.//Here, we are calling the method of parent class with child
3.//class object.
4.//Creating a parent class
5.class Vehicle{
6. void run(){System.out.println("Vehicle is running");}
7.}
8.//Creating a child class
9.class Bike extends Vehicle{
10. public static void main(String args[]){
11. //creating an instance of child class
12. Bike obj = new Bike();
13. //calling the method with child class instance
14. obj.run();
15. }
16.} output-Vehicle is running
Example of method overriding
In this example, we have defined the run method in the subclass as defined in the parent class but it has some
specific implementation. The name and parameter of the method are the same, and there is IS-A relationship
between the classes, so there is method overriding.

1./Java Program to illustrate the use of Java Method Overriding


2.//Creating a parent class.
3.class Vehicle{
4. //defining a method
5. void run(){System.out.println("Vehicle is running");}
6.}
7.//Creating a child class
8.class Bike2 extends Vehicle{
9. //defining the same method as in the parent class
10. void run(){System.out.println("Bike is running safely");}
11.
12. public static void main(String args[]){
13. Bike2 obj = new Bike2();//creating object
14. obj.run();//calling method
15. }
16.}
Output-Bike is running safely
A real example of Java Method Overriding
Consider a scenario where Bank is a class that provides functionality to get the rate of interest. However, the rate of
interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide 8%, 7%, and 9% rate of
interest.
1.//Java Program to demonstrate the real scenario of Java Method Overriding
2.//where three classes are overriding the method of a parent class.
3.//Creating a parent class.
4.class Bank{
5.int getRateOfInterest(){return 0;} }
6.//Creating child classes.
7.class SBI extends Bank{
8.int getRateOfInterest(){return 8;} }
9.class ICICI extends Bank{
10.int getRateOfInterest(){return 7;} }
11.class AXIS extends Bank{
12.int getRateOfInterest(){return 9;} }
13.//Test class to create objects and call the methods
14.class Test2{
15.public static void main(String args[]){
16.SBI s=new SBI(); ICICI i=new ICICI(); AXIS a=new AXIS();
17.System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
18.System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
19.System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest()); }}

20.Output-SBI Rate of Interest: 8 ICICI Rate of Interest: 7 AXIS Rate of Interest: 9

You might also like