Method Overriding in java

23:52 Unknown 0 Comments

Same method (name, parameter) declare in both main class and sub class, that method is known as method overriding.

same method used in single class that method is known as method overloading, while same method used in more than one class or different class that method is known as method overriding

Syntax:
public class Fruits {
                void sweetfruits(){
                                ...
                 }
}
class Mango extends Fruits{
                void sweetFruits(){
                                 ...
                 }
}


Example:

output:



If want to print both method in single object then must be use super keyword, see below:
Example:

output:


I want change something that
Fruits f = new Mango();
       f.sweetfruits();
then what will be the output:
Example:

output:

These all about method overriding concept. As per requirements you can use method overriding concept.

0 comments:

e gift