9.method Over Loading Ridding
9.method Over Loading Ridding
occurs when two or more methods in one class have the same method name but different
parameters.
//hello
welcome Java
hello, welcome to Java
Method Overriding
occurs when two methods have the same method name and parameters. One of the methods is in the parent class, and the
other is in the child class. Overriding allows a child class to provide the specific implementation of a method that
is already present in its parent class.
Rules:
• A final method does not support method overriding.
• A static method cannot be overridden.
• Private methods cannot be overridden.
• The return type of the overriding method must be the same.
• We can call the parent class method in the overriding method using the super keyword.
• A constructor cannot be overridden because a child class and a parent class cannot have the constructor with the
same name.
class fruit{
//defining a method
void eat(){System.out.println(“Apple is eating");}
}
//Creating a child class
class meal extends fruit{
//defining the same method as in the parent class
void eat(){System.out.println(“Vegetable is eating");}
•It is performed within the same class •It involves multiple classes
•Return type can be different but you •Return type must be same in
must change the parameters as well. overriding