Java overloaded methods ☎️

Java overloaded methods ☎️


Java overloaded methods ☎️

Java overloaded methods tutorial explained

#java #overloaded #methods


Content

0.16 -> hey what's going on everybody it's your
1.76 -> bro hope you're doing well and in this
3.36 -> video i'm going to teach you guys all
4.64 -> about overloaded methods in java so
6.879 -> sit back relax and enjoy the show
12 -> if you find this video helpful please
13.599 -> remember to like
15.12 -> comment and subscribe your support will
17.44 -> help keep this channel running
19.52 -> alright everybody let's discuss
21.279 -> overloaded methods overloaded methods
23.6 -> are methods that share the same name but
26.96 -> they have different parameters this is
29.199 -> allowed because
30.16 -> each method needs its own unique method
32.88 -> signature
33.76 -> that is the method's name plus its
36.079 -> parameters
36.88 -> they can share the same name but they
38.559 -> would need different parameters to give
40.64 -> them each a unique
41.84 -> method signature let's create a few
44.239 -> overloaded methods
45.36 -> perhaps just a few add methods that will
47.92 -> add some numbers together
49.36 -> outside of our main method let's declare
51.36 -> a few overloaded methods
53.36 -> so for this example we'll need these to
55.199 -> be static let's return an integer so the
57.76 -> return type is going to be
59.039 -> int and this will be called add
62.559 -> and we will accept a few arguments so we
64.96 -> need to set up some corresponding
66.96 -> parameters let's say int a and
70.24 -> int b and we will return
73.52 -> the sum return a plus b
76.88 -> so let's create another method an
78.88 -> overloaded method
79.92 -> also called add wait a minute we have a
82.96 -> problem
83.52 -> so we have two methods that are
86.32 -> duplicates
87.28 -> duplicate method add and type main they
89.92 -> both have
90.64 -> the same method signature they both
92.88 -> share the same
94.159 -> method name as well as the same
96.159 -> parameters where they accept
97.92 -> two integer values there are two ways in
100.72 -> which we can resolve this
102.32 -> the first is that we can change one of
104.159 -> the method names and this error would go
106.399 -> away
106.96 -> but that defeats the purpose of
108.56 -> overloaded methods the other way
110.72 -> is that we can change the parameters
112.96 -> that we have for each method
114.799 -> let's say we would like two versions
116.799 -> maybe even three versions
118.479 -> of our add method one that accepts two
121.119 -> values
121.759 -> another that accepts three values let's
124.24 -> say into c
125.6 -> return a plus b plus c and another that
128.959 -> will accept
130 -> four values so we'll have int
133.92 -> d return a plus b plus c plus d
138.64 -> and then our problem went away so let's
141.2 -> test these i'm just going to add a print
143.04 -> line
143.36 -> that states which overloaded method
145.2 -> we're using this
146.72 -> is overloaded method number
150.16 -> one and i'm going to copy this and paste
153.2 -> it
153.92 -> and we'll change the second instance of
156.319 -> this print line to
157.599 -> this is overloaded method number two and
160.8 -> lastly this is overloaded method
162.879 -> number three so let's try this let's say
165.76 -> we have an integer
166.879 -> int x equals add and we can pass in
170.56 -> between
171.2 -> two and four integers now i'm going to
174.319 -> attempt to pass in
175.44 -> a single integer value but we cannot run
178 -> and compile this because we do not have
180 -> a matching
180.879 -> method signature we can pass in between
183.68 -> two
184.08 -> and four integers so let's try this
187.12 -> by passing in two arguments this time
189.76 -> two integer values
191.2 -> and then let's display the sum the
193.2 -> result
194.4 -> system that out that print line our
196.48 -> variable of x
198.4 -> so we end up using our first overloaded
201.04 -> method
201.599 -> this is overloaded method number one and
203.519 -> this will display
204.799 -> the sum a plus b which is three
208.08 -> and let's try and use our second
210.159 -> overloaded method
211.44 -> we need to pass in three integer values
214.319 -> as arguments
215.44 -> and we are now using our second
217.68 -> overloaded method
219.12 -> and lastly we can pass in four
222.56 -> integer values and we end up using our
224.959 -> third overloaded method and we get a sum
227.44 -> of 10.
228.08 -> and with overloaded methods not only are
230.64 -> the numbers of parameters taken into
232.879 -> account but the data type
234.56 -> for each specific parameter so this time
237.599 -> let's create
238.48 -> three more add methods but these ones
241.68 -> will accept double values instead of
244.48 -> integers
245.519 -> so let's change any instance of int
248.72 -> to double including the return type
255.04 -> all right and then i'm going to change
257.04 -> the number of the overloaded method that
259.44 -> we're using so
260.32 -> six five four three two one
263.68 -> our first three methods only accept
265.6 -> integers and they will accept between
268.24 -> two and four integers our next three add
271.36 -> methods only accept
272.72 -> double values and they accept between
275.04 -> two and
275.759 -> four double values so we could also pass
278.96 -> in some double values to our overloaded
281.84 -> methods
282.639 -> let's change these to 1.0 2.0
286.88 -> 3.0 and 4.0 and we will need to change
290.88 -> the data type of our variable x
292.8 -> as well so double x add 1.0 through
297.12 -> 4.0 and we are now using
300.16 -> overloaded method number six which
302.32 -> accepts four
303.52 -> double arguments in summary overloaded
306.56 -> methods are methods that share
308.32 -> the same name but have different
310.24 -> parameters they can share the same name
312.479 -> but they would need different parameters
314.32 -> to give each method a unique method
316.639 -> signature
317.6 -> some factors that are taken into account
319.6 -> with parameters
320.8 -> are the number of parameters the data
323.44 -> type
324.08 -> and the order of the values so if you
326.8 -> would like a copy of all this code
328.32 -> i will post all of this in the comments
330.16 -> down below please don't forget to help
332 -> me out and smash that like button
333.84 -> drop a comment down below and subscribe
336 -> if you'd like to become a fellow bro

Source: https://www.youtube.com/watch?v=kArGE1-vRrw