Method Overriding
Whenever a parent(super class) and child(sub class) class both are having same method with same types of parameters is known as method overriding(function overriding).
Advantages of Java Method Overriding
- Method overriding is used to achieve run-time polymorphism.
- Method overriding is used to provide own specific implementation of a method that is already provided by its super class.
Rules of Java Method Overriding
- Method must have same name as in the parent class.
- Method must have same parameters as in the parent class.
- Must be IS-A relationship(inheritance).
- You can only override method in child class(sub class) , you cannot override method in same class.
Understanding problem without method overriding
Let's understand the problem that we may face in the program if we don't use method overriding concept.
For example:
class Human
{
void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human
{
public static void main(String args[])
{
Boy b = new Boy();
b.eat();
}
}
output: Human is eating
Here output is 'human is eating ' from eat() method of parent class but if you want to give the specific implementation of eat() method of its child class, you have to use method overriding here.
Example of Method Overriding
In this example, we have defined the eat() method in the subclass as defined in the super class. but it has some specific implementation. And the name and parameter of parent class and child class is same and there is IS-A relationship(inheritance) so there is method overriding.
class Human
{
void eat()
{
System.out.println("human is eating");
}
}
class Boy extends Human
{
void eat()
{
System.out.println("boy is eating");
}
public static void main(String args[])
{
Boy b = new Boy();//create child class object
b.eat();
}
}
output: boy is eating
👉 : If child class override parent class then object of child class give the priority to child class.
👉 : If child class override parent class then object of child class give the priority to child class.
Access Modifiers with Method Overriding
"The subclass overridden method cannot have weaker access than super class".
sequence from weaker to stronger:
- Private
- Default
- Protected
- Public
Parent Class → Child Class
(Weaker) (Stronger)
For example:
class Parent
{
void show()//default access modifier
{
System.out.println("hello java from parent");
}
}
class Child extends Parent
{
public void show()//public access modifier
{
System.out.println("hello java from child");
}
public static void main(String args[])
{
Child c = new Child();
c.show();
}
}
output: hello java from child
In the above example, there is weaker to stronger sequence i.e 'public' (child class)is stronger than 'default'(parent class).
For example2:
class Parent
For example2:
class Parent
{
public void show()//public access modifier
{
System.out.println("hello java from parent");
}
}
class Child extends Parent
{
void show()//default access modifier
{
System.out.println("hello java from child");
}
public static void main(String args[])
{
Child c = new Child();
c.show();
}
}
output: compile time error
In the above example, there is compile time error because there is stronger to weaker sequence i.e 'public' is stronger than 'default'.
Q. Can we override static method?
Ans. No, we cannot override static method in java.
Q. Can we override main method in java?
Ans. No, we cannot override main method in java but we can overload main method in java.
output: compile time error
In the above example, there is compile time error because there is stronger to weaker sequence i.e 'public' is stronger than 'default'.
Q. Can we override static method?
Ans. No, we cannot override static method in java.
Q. Can we override main method in java?
Ans. No, we cannot override main method in java but we can overload main method in java.
Difference between Method Overloading and Method Overriding
Method Overloading
- Method Overloading is used to increase the readability of the program.
- Method Overloading occurs within the same class.
- In method overloading parameters should be different.
- Method Overloading is the example of compile time polymorphism.
- Inheritance is not required.
- In method overloading, One overloaded method does not hide the other method.
Method Overriding
- Method Overriding is used to provide the own specific implementation of a method that is already provided by its super class.
- Method Overriding occurs between two classes(through inheritance).
- In method overriding parameters should be the same.
- Method Overriding is the example of run time polymorphism.
- Inheritance(IS-A relationship) is required.
- In method overriding, Child class method hide the super class method from access by child class object.
0 comments:
Post a Comment