(OOP) Chapter 2
(OOP) Chapter 2
Class
Is a software design that describes the common attribute
and activities of objects.
-- attributes represent the properties of the object
-- activities represent the actions exhibited by the object
It is a collection of objects of similar type.
Cont..
class Box {
double width;
double height;
double depth;
}
stud1 1022
stud2 ?
stud1 1022
stud2
Here, b1 points the object pointed by b3, but b2 still points to the
previous object.
Cont..
// Assume we have student class and 1022/1023 are memory address
students stud1= new students(“Teborne”, “2233/02”);
students stud3= new students(“Sucre”, “2337/07”);
students stud2=stud1;
stud1=stud3; // points the same memory address
1022
stud1 1023
stud2
Teborne 2233/02
stud3 1022
int square()
{
return 10 * 10;
}
Cont..
While this method does, indeed, return the value of 10 squared, its
use is very limited.
However, if you modify the method so that it takes a parameter, as
shown next, then you can make square( ) much more useful.
int square(int i)
{
return i * i;
}
Now, square( ) will return the square of whatever value it is called
with.
That is, square( ) is now a general-purpose method that can
compute the square of any integer value, rather than just 10.
Cont..
Here is an example:
int x, y;
x = square(5); // x equals 25
x = square(9); // x equals 81
y = 2;
x = square(y); // x equals 4
In the first call to square( ), the value 5 will be passed into parameter i.
In the second call, i will receive the value 9.
The third invocation passes the value of y, which is 2 in this example.
As these examples show, square( ) is able to return the square of whatever
data it is passed.
Cont..
It is important to keep the two terms parameter and argument
straight.
A parameter is a variable defined by a method that receives a value
when the method is called.
For example, in square ( ), i is a parameter.
An argument is a value that is passed to a method when it is
invoked.
For example: square (100) passes 100 as an argument.
Inside square( ), the parameter i receives that value.
You can use a parameterized method to improve the Box class.
Set instance variables values by method
In the preceding examples, the dimensions of each box had to be set
separately by use of a sequence of statements, such as:
bx.width = 10;
bx.height = 20;
bx.depth = 15;
While this code works, it is troubling for two reasons.
First, it is clumsy and error prone. For example, it would be easy to
forget to set a dimension.
Second, in well-designed Java programs, instance variables should
be accessed only through methods defined by their class.
Cont..
Thus, a better approach to setting the dimensions of a box is to create a
method that takes the dimension of a box in its parameters and sets
each instance variable appropriately.
Class name and constructor name should be the same. That is, it has
the same name as the class in which it resides and is syntactically
similar to a method.
Constructors have no return type (not even void), this is because
they return the instance of the class itself.
Constructors should be public.
Constructors can not be inherited.
They can be overloaded. Many constructors with the same name can
be defined.
Constructor and normal method
Its name should the same as class name Its name can be any identifier
Constructor with the same name, but the compiler uses two mechanism to differentiate
the overloaded constructors.
number of parameter
data type of parameter
public class students{ public static void main(String[] args){
String name;
students() students stud1=new students();
{
System.out.println(“default constructor”); students stud2=new students(“Teborne”);
}
students(String name) Output
}
{ ======
}
this.name=name; default constructor
System.out.println(this.name); Teborne
}
“This” keyword
this implicit reference to current object.
used to:
-- prevent instance variables hiding by local variable.
-- improve readability of the program
-- invoke the constructor.
Passing by- value -This method copies the value of an argument into the formal parameter
of the subroutine.
Therefore, changes made to the parameter variable values have no effect on the
argument variable values.
Used to pass simple data types.(primitive data type)
Passing by reference -In this method, a reference to an argument (not the value of the
argument) is passed to the parameter.
changes made to the parameter variable values will affect the argument variable
values.
Used to pass objects.
Passing arguments: sample codes
// pass by value public class ByRference {
j/=2; 15 20 }
15 20
} void Test(ByRference ref) {
int a=15; }
} System.out.println(reff.a+" "+reff.b);
} }}