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

Java OOP - 4 - MethodOverriding PDF

Method overriding in Java occurs when a subclass provides a specific implementation of a method that is declared in the parent class. The method must have the same name, parameters, and return type as the parent method. Method overriding enables runtime polymorphism and allows subclasses to define their own behavior while retaining the functionality of the parent class.

Uploaded by

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

Java OOP - 4 - MethodOverriding PDF

Method overriding in Java occurs when a subclass provides a specific implementation of a method that is declared in the parent class. The method must have the same name, parameters, and return type as the parent method. Method overriding enables runtime polymorphism and allows subclasses to define their own behavior while retaining the functionality of the parent class.

Uploaded by

ADITYA PANSARE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Method Overriding 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 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.

Usage of Java 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 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).
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.
//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[]){ Output:


Bike2 obj = new Bike2();//creating object Bike is running safely
obj.run();//calling method
}
}
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.
class Bank{ //Creating a parent class.
int getRateOfInterest(){return 0;}
}
class SBI extends Bank{ //Creating child classes.
int getRateOfInterest(){return 8;}
}
class ICICI extends Bank{
int getRateOfInterest(){return 7;}
}
class AXIS extends Bank{
int getRateOfInterest(){return 9;}
}
class Test2{ //Test class to create objects and call the methods
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()); Output:
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest()); SBI Rate of Interest: 8
} ICICI Rate of Interest: 7
} AXIS Rate of Interest: 9
Access Specifiers in Method Overriding
The same method declared in the superclass and its subclasses can have different access specifiers. However, there is a
restriction.

We can only use those access specifiers in subclasses that provide larger access than the access specifier of the superclass.
For example,

Suppose, a method myClass() in the superclass is declared protected. Then, the same method myClass() in the subclass can
be either public or protected, but not private
class Animal {
protected void displayInfo() {
System.out.println("I am an animal.");
}
} In the above example, the subclass Dog overrides the method
displayInfo() of the superclass Animal.
class Dog extends Animal {
public void displayInfo() { Whenever we call displayInfo() using the d1 (object of the subclass),
System.out.println("I am a dog."); the method inside the subclass is called.
}
} Notice that, the displayInfo() is declared protected in the Animal
superclass. The same method has the public access specifier in the
class Main { Dog subclass. This is possible because the public provides larger
public static void main(String[] args) { access than the protected.
Dog d1 = new Dog();
d1.displayInfo();
}
}
Important Questions on Method Overriding
Can we override static method?
No, a static method cannot be overridden. It can be proved by runtime polymorphis.

Why can we not override static method?


It is because the static method is bound with class whereas instance method is bound with an object. Static
belongs to the class area, and an instance belongs to the heap area.

Can we override java main method?


No, because the main is a static method.
Difference between method overloading and method overriding in java
No. Method Overloading Method Overriding
Method overriding is used to provide the
Method overloading is used to increase the
1) specific implementation of the method that is
readability of the program.
already provided by its super class.
Method overloading is performed within Method overriding occurs in two classes that
2)
class. have IS-A (inheritance) relationship.
In case of method overloading, parameter In case of method overriding, parameter
3)
must be different. must be same.
Method overloading is the example of Method overriding is the example of run time
4)
compile time polymorphism. polymorphism.

In java, method overloading can't be


performed by changing return type of the
Return type must be same or covariant in
5) method only. Return type can be same or
method overriding.
different in method overloading. But you
must have to change the parameter.

You might also like