Lesson 20 of 34

Method OverloadingMethod Overloading

7 min 30 XP

ஒரே பெயரில் பல methods எழுதலாம், parameters வேறுபட்டால் போதும் (எண்ணிக்கை அல்லது type). Java அழைப்பைப் பார்த்து சரியானதைத் தேர்ந்தெடுக்கும். இது compile-time polymorphism.

Several methods can share a name as long as their parameters differ in number or type. Java picks the right one from the call. This is compile-time polymorphism, a classic interview term.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Return type மட்டும் வேறுபட்டால் overloading ஆகாது, compile error வரும்.

Changing only the return type is not overloading; it's a compile error.

Challenge

+30 XP

area என்ற பெயரில் இரண்டு methods எழுதுங்கள்: area(int side) சதுரத்தின் பரப்பு, area(int l, int w) செவ்வகத்தின் பரப்பு. main()-ல் s, l, w-ஐப் படித்து இரண்டையும் print செய்யுங்கள். Input: 4 3 5 Output: 16 15

Write two methods named area: area(int side) for a square and area(int l, int w) for a rectangle. In main(), read s, l and w and print both areas. Input: 4 3 5 Output: 16 15

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.