Q)What is method overriding?
=>Redefining the super class method in sub class is known as method overriding.
=>When method overriding is implemented, super class version of the method is known as
an overridden method.Sub class version of the method is known as an overriding method.
For eg.
class Vehicle{
void move(){
}//overridden method
}//super class
class Car extends Vehicle{
void move(){
}//overriding method
}//sub class
Note:- When method overriding is implemented, if the method is called on the sub class object, always ov
erriding method only is executed but not the overrdden method.
=>Method overriding is implemented in the following scenarios.
1.)Parent class provided functionality is not required in child class. Method name reusability is required
for the sub class object but not the code reusability.
2.)Super class provided functionality is required but insufficient for the sub class object.To add some
more functionality in the sub class under the same method name.
3.)Parent class has given only method name but no code(abstract method concept)
=>There are four rules to be followed in order to implement method overriding.
1)signature of both the methods should be the same.
2)Return type of the methods should be the same.
3)Overriding method should not have weaker access privileges(equal is ok and more also ok)
4)Overriding method should not have those checked exceptions in the throws clause that are not specifi
ed in the Overridden method’s throws clause.
Q)Example program on method overriding.
//MethodOverridingApplication.java
class Vehicle{
void move(){
System.out.println("Every vehicle should move this way");
}//overridden method
}//super class
class Car extends Vehicle{
void move() {
System.out.println("As a car I move in my own way");
}//overriding method
}//sub class
class MethodOverridingApplication{
public static void main(String[] args){
Car c=new Car();
c.move();
}
}
**************************
//MethodOverridingApplication2.java
=>Redefining the super class method in sub class is known as method overriding.
=>When method overriding is implemented, super class version of the method is known as
an overridden method.Sub class version of the method is known as an overriding method.
For eg.
class Vehicle{
void move(){
}//overridden method
}//super class
class Car extends Vehicle{
void move(){
}//overriding method
}//sub class
Note:- When method overriding is implemented, if the method is called on the sub class object, always ov
erriding method only is executed but not the overrdden method.
=>Method overriding is implemented in the following scenarios.
1.)Parent class provided functionality is not required in child class. Method name reusability is required
for the sub class object but not the code reusability.
2.)Super class provided functionality is required but insufficient for the sub class object.To add some
more functionality in the sub class under the same method name.
3.)Parent class has given only method name but no code(abstract method concept)
=>There are four rules to be followed in order to implement method overriding.
1)signature of both the methods should be the same.
2)Return type of the methods should be the same.
3)Overriding method should not have weaker access privileges(equal is ok and more also ok)
4)Overriding method should not have those checked exceptions in the throws clause that are not specifi
ed in the Overridden method’s throws clause.
Q)Example program on method overriding.
//MethodOverridingApplication.java
class Vehicle{
void move(){
System.out.println("Every vehicle should move this way");
}//overridden method
}//super class
class Car extends Vehicle{
void move() {
System.out.println("As a car I move in my own way");
}//overriding method
}//sub class
class MethodOverridingApplication{
public static void main(String[] args){
Car c=new Car();
c.move();
}
}
**************************
//MethodOverridingApplication2.java