#4.5 Java Tutorial | Method and Constructor Overloading

Telusko
Telusko
151.8 هزار بار بازدید - 7 سال پیش - In this lecture we are
In this lecture we are discussing:
1) What is method overloading
 a) what is necessary condition for method overloading?
 b) example
2)what is constructor overloading ?
 a)what is necessary condition?
 b) example

#1
Method overloading:
:- In a same class we have more than or equal to two methods with same name
but with different parameters.
  a)Necessary condition
    i)at least two methods with same name in class.
    ii)different parameters in methods might be of different data tyoe or nuumber of parameters are different
    e.g add(int num1, int num2) , add(int num1, intnum2, int num3), add(float num1, float num2), add(int num1, float num2)
    iii)return type might same or different does not matter
    e.g public int add(int num1, int num2) public void add(int num1, int num2) is not overloaded because return type does not effect any change.
    you got error when you want to do like this //Duplicate method add(int, int) in type
   
   b)example
   class Calc{
       public void add(int num1,int num2){

       }
       public void add(float num1,float num2){

       }
       public void add(int num1, int num2 ,int num3){

       }
       public void add(int num1, float num2){

       }

   }

#2
Constructor overloading
:- I understand everyone understand constructor is a member method of a class

a)Necessary condition for constructor overloading
 :- it just same as method overloading name must be same and parameters must be different .
 e.g Calc(), Calc(int num) ,Calc(int num1, int num2)

b)example
class Calc{
   int num1;
   int num2;
   Calc(){
       //non -parametrized constructor
   }
   Calc(int num1){
       this.num1=num1; //parametrized constructor with single parameters

   }
   Calc(int num1, int num2){
       this.num1=num1;
       this.num2=num2;
   }
}

In this video we will see :
- Passing multiple parameter in method
- Creating multiple methods having same name
- Why we need method overloading
- Constructor overloading
- How overloading works

Github :- https://github.com/navinreddy20/Java-...


Instagram : Instagram: navinreddyofficial
Linkedin : LinkedIn: navinreddy20
Discord : Discord: discord

More Learning :

Java - https://bit.ly/3xleOA2
Python   :- https://bit.ly/3H0DYHx
Django  :- https://bit.ly/3awMaD8
Spring Boot  :- https://bit.ly/3aucCgB
Spring Framework :- https://bit.ly/3GRfxwe

Servlet & JSP  :- https://bit.ly/3mh5CGz
Hibernate Tutorial :- https://bit.ly/3NWAKah
Rest API | Web Service Tutorial :- https://bit.ly/38RJCiy

Git :- https://bit.ly/3NUHB3V
JavaScript  :- https://bit.ly/3mkcFys
Kotlin  :- https://bit.ly/3GR2DOG

Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
http://www.telusko.com/contactus
7 سال پیش در تاریخ 1396/11/03 منتشر شده است.
151,837 بـار بازدید شده
... بیشتر