Anda di halaman 1dari 15

SKILLS FOR INDIA

JAVA

1
Document Version: 1.00

Session 5

GUI Application Development using Java7


Topics to be covered in this session:

Method Overloading and Constructor

2
Document Version: 1.00

Key Learning Objectives


Define what is method overloading
Demonstrate method overloading
Analyze that method overloading is a form of polymorphism
Explain syntax of defining a constructor
Demonstrate usage of a constructor
Explain this

3
Document Version: 1.00

Function Overloading
A class may have more than one function having same name
But there parameter list must be different
Return type does not play any role in function overloading

4
Document Version: 1.00

Example..
class Rectangle{
public void setData(int side){
length=width=side;
}
public void setData(int side1,int side2){
length=side1;
width=side2;
}
}

5
Document Version: 1.00

Constructor method
It is a method of a class but little different
Called automatically when instance is created
Name of this function is same as the class name
Does not have any return type
Constructor initializes an object when it is created by 'new
We use constructor to initialize the private data of any object
Constructor can be overloaded

6
Document Version: 1.00

Constructor example
class Rectangle{
private int length,width;
Rectangle(){
length=10;
width=5;
}
Rectangle(int side){
length=width=side;
}
Rectangle(int side1,int side2){
length=side1;
width=side2;
}

7
Document Version: 1.00

Default Constructor
A constructor which does not accept any parameter is known as default
constructor
Compiler writes a default constructor for a class , if you do not code any
constructor

8
Document Version: 1.00

Exercise
Write a java program :
Define a class Coord having two private int variable xcoord and
ycoord
Provide following :
a) a default constructor to initialize xcoord and ycoord to 10
b) two parameter constructor to initialize xcoord , ycoord
c) public void print() ( 12,13)
d) public void add(int xvalue)
e) public void add(int xvalue,int yvalue)
f) public void add(Coord coord)

9
Document Version: 1.00

Exercise
Implement an object counter in the previous problem.

10
Document Version: 1.00

this keyword
Every java instance method has access to two special variable
this
super
Instance method works on that object which has called it.
Within instance method , if reference to object on which instance
method is working is needed , we use this
this contains reference to the current object.
In order to identify the instance variable from formal parameters
this keyword is used.

11
Document Version: 1.00

Example..
class Rectangle{
private int length,width;
public void setData(int length,int width){
//in the body of the function ,length
//refers to both instance variable and parameter
//variable(local variable)
this.length=length;
this.width=width;
}
}

12
Document Version: 1.00

Another Usage Of this


Calling constructor from another constructor
Saves you from coding initialization logic many times for different
constructors
public Rectangle(){
this(10,5);
}
public Rectangle(int side){
this(side,side);
}
public Rectangle(int side1,int side2)
{ length=side1;width=side2;}

13
Document Version: 1.00

Document Amendment History

Document Amendment History


S.No

Description

Author

Version

Date

1
2
3
4
5
6
7
8

14
Document Version: 1.00

T H A N K Y O U. . .

All information, including graphical representations, etc provided in this presentation is for exclusive use of current Globsyn
Skills students and faculty. No part of the document may be reproduced in any form or by any means, electronic or otherwise,
without written permission of the owner.
15
Document Version: 1.00

Anda mungkin juga menyukai