Java Method Overriding With Example
Java Method Overriding
It is possible to create two or more methods that have same name. But different parameter list is referred to as method overloading. When amethod is called, it matches up with the method name and then the number.
Polymorphism :
Polymorphism(from Greek meaning "many forms") allows a single name/operator to be associated with different operations depending on the type of data passed to it. In C++, it is achieved by function overloading, operator over loading and dynamic binding(Virtual functions).
There are two types of polymorphism in Java: We can perform polymorphism in java by method overloading and method overriding.
- Runtime Polymorphism (Dynamic Polymorphism)
- Compile time Polymorphism (Static Polymorphism)
- Method Overloading
- Method Overriding
1) Runtime Polymorphism (Dynamic Polymorphism):
It is also known as Dynamic Method Dispatch.
Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime, thats why it is called runtime polymorphism.
Examples 1:
Output :
Overriding Method
Method overloading is in the same class, where more than one method have the same name but different signatures.
Example 2:
Output :
Ha.ha.ha. I am Dog class
Example 3:
Output :
subclass1 subclass2
2) Compile time Polymorphism (Static Polymorphism) :
Compile time polymorphism is also known as static polymorphism.
Method overloading is one of the way java supports static polymorphism. Here we have two definitions of the same method add() which add method would be called is determined by the parameter list at the compile time. That is the reason this is also known as compile time polymorphism.
Examples 1:
Output :
30 60
Method Overloading :
Method overloading is in the same class, where more than one method have the same name but different signatures.
Method overloading is one of the way java supports static polymorphism. Here we have two definitions of the same method add() which add method would be called is determined by the parameter list at the compile time. That is the reason this is also known as compile time polymorphism.
When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments.
Examples 1:
Output :
30 60
Method Overriding :
Method overriding is when one of the methods in the super class is redefined in the sub-class. In this case, the signature of the method remains the same.
Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.
When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
Examples 1:
Output :
I am studying 60
Example 2:
Output :
subclass1 subclass2