Login
Register

Home

Trainings

Fusion Blog

EBS Blog

Authors

CONTACT US

Fusion Blog
  • Register

Oracle Gold Partners, our very popular training packages, training schedule is listed here
Designed by Five Star Rated Oracle Press Authors & Oracle ACE's.

webinar new

Search Courses

Objective:

In the previous article Set and File handling in Java, we have learned about the Set function and the ways of file handling in Java. In this article,, we are going to learn about the Overriding method in java.

 

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

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

Method overriding is used for runtime polymorphism

Rules for Method overriding:

  1. method must have same name as in the parent class

  2. method must have same parameter as in the parent class.

  3. must be IS-A relationship (inheritance).

 

Example 1:

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

 

In thi above 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 is same and there is IS-A relationship between the classes, so there is method overriding.

 

Example2:

Consider a scenario, Bank is a class that provides functionality to get rate of interest. But, rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide 8%, 7% and 9% rate of interest.

bankinheritance.png

class Bank{

int getRateOfInterest(){return 0;}

}

 

class SBI extends Bank{

int getRateOfInterest(){return 8;}

}

 

class ICICI extends Bank{

int getRateOfInterest(){return 7;}

}

class AXIS extends Bank{

int getRateOfInterest(){return 9;}

}

 

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

Difference between Method overloading and Method Overriding:

 

No.

Method Overloading

Method Overriding

1)

Method overloading is used to increase the readability of the program.

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

2)

Method overloading is performed within class.

Method overriding occurs in two classes that have IS-A (inheritance) relationship.

3)

In case of method overloading, parameter must be different.

In case of method overriding, parameter must be same.

4)

Method overloading is the example of compile time polymorphism.

Method overriding is the example of run time polymorphism.

5)

In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.

Return type must be same or covariant in method overriding.


Varun Kapila

Add comment


Security code
Refresh

About the Author

Varun Kapila

Search Trainings

Fully verifiable testimonials

Apps2Fusion - Event List

<<  Apr 2024  >>
 Mon  Tue  Wed  Thu  Fri  Sat  Sun 
  1  2  3  4  5  6  7
  8  91011121314
15161718192021
22232425262728
2930     

Enquire For Training

Fusion Training Packages

Get Email Updates


Powered by Google FeedBurner