Method overloading and constructor overloading in java
-
Upload
baabtracom-mentoring-partner-first-programming-school-in-india -
Category
Education
-
view
687 -
download
3
description
Transcript of Method overloading and constructor overloading in java
Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
Week Target Achieved
1 40 21
2 40 23
3 40 27
Typing Speed
Jobs AppliedWeek Company Designation Applied Date Current Status
1 Advent software
Sr. Application Engineer 28-8-2013 Short listed
2 Alcatel-lucent Software Engineer 30-8-2013 Waiting
3 Intel info group System Engineer 6-9-2013 Waiting
Method overloading and constructor overloading in JAVA
Muhammed Noufal V [email protected]
mwww.facebook.com/vtnoufalvttwitter.com/noufalurnappyin.linkedin.com/pub/
muhammed-noufal9744003056
What is Method• A Java method is a collection of
statements that are grouped together to perform an operation
• A class can contain any number of methods.
• Methods can be with parameter and without parameter.
• The parameter in a method are called type signature.
Method Overloading• Two or more methods within the same class that
share the same name, but with different parameter declarations (type signatures).
• The process is referred to as method overloading.
• Overloading methods demonstrate the concept of polymorphism.
• Overloaded methods may have different return types.• When java encounters a call to an overloaded method,
it simply executes the version of the method whose parameters match the arguments used in the call.
Example• public class example_1 { int n1; int n2; example_1() { n1 = 10; n2 = 20; } void square() { System.out.println("The Square is " + n1 * n2); } void square(int p1) { n1 = p1; System.out.println("The Square is " + n1 *
n2); }
void square(int p1, int p2) { n1 = p1; n2 = p2; System.out.println("The Square is " +
n1 * n2); } public static void main(String args[]) { example_1 obj1 = new example_1(); obj1.square();
obj1.square(4); obj1.square(7,8);
}}
Constructor• Special type of method • A constructor initialize an object
immediately upon creation • Name of constructor must be same
as name of Class• Once defined, the constructor is
automatically called immediately after the object is created before the new operator compleates
• They do not specify any return type not even void
Constructor Overloading
• Constructors are methods that can be overloaded, just like any other method in a class.
• Constructors having the same name with different parameter list is called constructor overloading.
Exampleclass Point{ int x; int y; Point(int a, int b) { x = a; y = b; } }class Circle{ int originX; int originY; int radius; Circle() { originX = 5; originY = 5; radius = 3; } Circle(int x1, int y1, int r) { originX = x1; originY = y1; radius = r; }
Circle(Point p, int r) { originX = p.x; originY = p.y; radius = r; } void display() { System.out.println("--Center at " + originX
+ " and " + originY); System.out.println("Radius = " + radius); } public static void main(String args[]) { Circle c1 = new Circle(); Circle c2 = new Circle(10,20,5); Circle c3 = new Circle(new Point(15,25),10); c1.display(); c2.display(); c3.display(); } }
Contact Us
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Start up VillageEranakulam,Kerala, India.
Email: [email protected]