0% found this document useful (0 votes)
4 views4 pages

9.method Over Loading Ridding

Uploaded by

pratikshagite382
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

9.method Over Loading Ridding

Uploaded by

pratikshagite382
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Method Overloading

occurs when two or more methods in one class have the same method name but different
parameters.

public class test{


public static void main(String[] args){
System.out.println("hello");
Edureka.main(“Java");
}

public static void main(String arg1){


System.out.println(" welcome" + arg1);
Edureka.main("welcome" , "to Java");
}

public static void main(String arg1 , String arg2){


System.out.println("hello, " +arg1 +””+ +arg2);
}
}

//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");}

public static void main(String args[]){


meal obj = new meal();//creating object
obj.eat();//calling method
}
}
Method Overloading Method Overriding
•Provides a specific implementation
•It is used to increase the readability of the method already in the parent
of the program class

•It is performed within the same class •It involves multiple classes

•Parameters must be different in case •Parameters must be same in case of


of overloading overriding
•Is an example of compile-time •It is an example of runtime
polymorphism polymorphism

•Return type can be different but you •Return type must be same in
must change the parameters as well. overriding

•Static methods can be overloaded •Overriding does not involve static


methods

You might also like