Lec 23
Lec 23
Lec 23
I Semester 2008-09
Lecture 22
1
A common confusion : what is x in distance from origin ?
public class Point
{ double x;
double y;
2
Resolving the confusion using this reference
public class Point
{ double x;
double y;
3
this refers to the current object being accessed
4
Advantages of taking OOP
Point
5
Organizing a collection of related classes
• this library should be imported to any program which requires any class from
that library
In JAVA, it is achieved using Package
6
Package : library of related classes
Example :
7
Home work :
8
What will be the output of the following program ?
class test1
{
public static void Increment(int i)
{
i = i+1;
}
9
What will be the output of the following program ?
class test1
{
public static void Increment(int i)
{
i = i+1;
}
10
Consider the folowing class
11
What will be the output of the following program ?
class test2
{ public static void Increment(myInt i)
{
i.value = i.value+1;
}
12
What will be the output of the following program ?
class test2
{ public static void Increment(myInt i)
{
i.value = i.value+1;
}
13
An important rule in JAVA you should never miss
14
Members of a class
1. Attributes or Fields :
The data variables associated with a class and its object
2. Methods
15
Access Control of members of a class
• private :
members declared private are accessible only in the class itself.
• public :
members declared public are accessible anywhere the class is accessible.
• package :
members declared with no access modifier are accessible in the classes in
the same package.
16
General rules (but you may break them sometimes)
17
How to read attributes which are declared private ?
18
Example :
public class Point
{ private double x; private double y;
19
Method overloading in JAVA
Question : In a class, can there be two methods with the same name?
20
Method overloading in JAVA
Question : In a class, can there be two methods with the same name?
21
Signature of method defined in lecture 22
For example,
22
Signature of method correctly defined is
For example,
23
which method will be executed if the method is overloaded
24
which method will be executed if the method is overloaded
25
which method will be executed if the method is overloaded
26
which method will be executed if the method is overloaded
27
which method will be executed if the method is overloaded
28
which method will be executed if the method is overloaded
29
which method will be executed if the method is overloaded
30
which method will be executed if the method is overloaded
31
Useful progamming advice
Invoke methods with arguments which match exactly the signature of a method.
Do it by explicit type cast of arguments if needed.
What is expected from the students : they should be able to find out the method
which will be executed if the types of the arguments exactly match some method.
32