Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
95 views

Method Overriding in Java Notes

The document provides two examples of method overriding in Java. The first example creates a Vehicle parent class with a run method, and a Bike child class that overrides the run method to output a bike-specific message. The second example creates a Bank parent class with a getRateOfInterest method, and child classes for different banks (SBI, ICICI, AXIS) that override the method to return each bank's specific interest rate. The examples demonstrate how method overriding allows child classes to provide their own implementation of a method defined in the parent class.

Uploaded by

Rajesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Method Overriding in Java Notes

The document provides two examples of method overriding in Java. The first example creates a Vehicle parent class with a run method, and a Bike child class that overrides the run method to output a bike-specific message. The second example creates a Bank parent class with a getRateOfInterest method, and child classes for different banks (SBI, ICICI, AXIS) that override the method to return each bank's specific interest rate. The examples demonstrate how method overriding allows child classes to provide their own implementation of a method defined in the parent class.

Uploaded by

Rajesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercise: Example program to show use of method overriding.

We have defined the run method in the subclass as defined in the parent class but it
has some specific implementation.

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


//Creating a parent class.
class Vehicle{
//defining a method
void run(){System.out.println("Vehicle is running");}
}
//Creating a child class
class Bike2 extends Vehicle{
//defining the same method as in the parent class
void run(){System.out.println("Bike is running safely");}

public static void main(String args[]){


Bike2 obj = new Bike2();//creating object
obj.run();//calling method
}
}

Output:
Bike is running safely

Exercise: Another example program for 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.

//Java Program to demonstrate the real scenario of Java Method Overriding


//where three classes are overriding the method of a parent class.
//Creating a parent class.
class Bank{
int getRateOfInterest(){return 0;}
}
//Creating child classes.
class SBI extends Bank{
int getRateOfInterest(){return 8;}
}

class ICICI extends Bank{


int getRateOfInterest(){return 7;}
}
class AXIS extends Bank{
int getRateOfInterest(){return 9;}
}
//Test class to create objects and call the methods
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}
Output:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9

You might also like