OOP with Java ... Dr. Ahmed M. Al-Salih ...

advertisement
OOP with Java
Dr. Ahmed M. Al-Salih
University of Babylon/ College of IT
class – First Semester- Department of Software
2nd
Constructor and Set and Get Methods:
Constructor Used to initialize an object of the class and any members. Called only once
for the lifetime of the object being initialized:
public class Person
{
String person_name;
public Person(String name)
{
person _name = name;
}
}
Example usage:
// Initialize object of Person class by calling constructor.
Person p = new Person("College of IT");
Get and Set Methods
Used to get or set values of object members respectively. Unlike constructors, set methods
can be used to initialize member values more than once:
public class Person
{
// Member.
String person_name;
// Constructor.
public Person(String name)
{
person_name = name;
}
// Get member value.
public String getName()
{
return person_name;
}
// Set member value to given value.
public void setName(String name)
{
person_name = name;
}
}
Example usage:
Page 44
OOP with Java
Dr. Ahmed M. Al-Salih
University of Babylon/ College of IT
class – First Semester- Department of Software
2nd
// Initialize object of Person class.
Person p = new Person("College of IT");
String name;
// Get name of this person.
name = p.getName();
System.out.println(name);
// Set new name for this person.
p.setName("University of Babylon");
name = p.getName();
System.out.println(name);
Output:
College of IT
University of Babylon
To understand the effects of public and private, consider the following program:
// Public vs private access.
class MyClass
{
private int alpha; // private access
public int beta; // public access
int gamma; // default access (essentially public)
/* Methods to access alpha. It is OK for a
member of a class to access a private member of the same
class. */
void setAlpha(int a)
{
alpha = a;
}
int getAlpha()
{
return alpha;
}
}// end class MyClass
class AccessDemo
{
public static void main(String args[])
{
MyClass ob = new MyClass();
/* Access to alpha is allowed only through
its accessor methods. */
ob.setAlpha(-99);
System.out.println("ob.alpha is " + ob.getAlpha());
// You cannot access alpha like this:
Page 45
OOP with Java
Dr. Ahmed M. Al-Salih
University of Babylon/ College of IT
class – First Semester- Department of Software
2nd
// ob.alpha = 10; // Wrong! alpha is private!
// These are OK because beta and gamma are public.
ob.beta = 88;
ob.gamma = 99;
}
}
Type Checking
Java is a strongly types language, which means that the Java compiler checks
the code to ensure that every assignment and every call is type correct. If a type
error is discovered, compilation fails with an error message.
Type checking depends on the fact that every variable declaration gives the type
checking of the variable, and the header of every method and constructor defines its
signature: the types of its arguments and results (and also the types of any
exceptions it throws).
This information allows the compiler to deduce an apparent type for any
expression and this deduction then allows it to determine the legality of an
assignment For example consider
int y= 7;
int z= 3;
int x= num.gcd (z,y); // discuss that
When the compiler processes the call to num.gcd, it knows that the num.gcd
requires two integer arguments and it also knows that expressions z and y are both
of type int. therefore; it knows the call of gcd is legal. Furthermore it knows that gcd
returns an int and therefore it knows that the assignment to x is legal.
Note Java has an important property: legal Java programs (that is, those accepted
by the compiler) are guaranteed to be type safe. This mean that there cannot
by any type errors when the program runs.
Page 46
OOP with Java
Dr. Ahmed M. Al-Salih
University of Babylon/ College of IT
class – First Semester- Department of Software
2nd
Java types are organized into a hierarchy in which a type can have a number of
supertypes , we say the type is a subtype of each its supertype . (other texts may
use the term superclass and subclass)
In addition, some texts say a type extends another type to mean it is a subtype of
the other type.
The subtype relation is transitive: if R is a subtype of S, and S is a subtype of
T, then R is a subtype of T. the relation is also reflexive:
Type S is a subtype of itself.
Since objects of a subtype behave like those of a super-type , it makes sense
to allow them to be referred to by a variable whose declared type is a supertype.
This usage is permitted by Java: an assignment v=e is legal if the type of e is a
subtype of the type of v. for example, the following is legal
Object o1= a; // a is an array defined previously
Object o2= s; // is a string defined previously
Here a is an array and s is a string
An implication of the assignment rule is that actual type of the object obtained
by the evaluating an expression is a subtype of the apparent type of the expression
deduced by the compiler using declaration. For example, the apparent type of o2 is
Object, but it actual type is String.
Type checking is always done using the apparent type. This means, for
example, that any method calls made using the object will be determined to be legal
based on the apparent type. Therefore only Object method like equal can be called
on Object O2, string method like length (which returns a count of the number of
characters in the string) cannot be called:
Example
Page 47
OOP with Java
Dr. Ahmed M. Al-Salih
University of Babylon/ College of IT
class – First Semester- Department of Software
2nd
If ( o2.equals(“abc”)// legal
If ( o2.lenght ( ) ) // illegal
Furthermore, the following is illegal:
S=o2; // illegal
Because the apparent type of o2 is not a subtype of String. Compilation will fail when
the program contains illegal code as in these examples.
Sometimes a program needs to determine the actual type of an object at runtime, for
example, so that a method not provided by the apparent type can be called. This can
be done by casting.
s= (String) o2; // legal
The use of a cast causes to occur at runtime; if the check succeeds, the indicated
computation is allowed and otherwise, a ClassCastException will be raised. In the
example, the casts check whether o2’s actual type is the same as the indicated type
string, these checks succeed, and therefore, the assignment of the statement is
allowed.
Example:
Object o=”abc”;
We have a following code, we need to indicate whether or not a compile time error
will occur, and for those statements that are legal at compile time, indicate whether
they will return normally or by throwing an exception
boolean b= o.equals("a,b,c");
char c= o.charAt(1);
Object o2=b;
String s=o;
String t=(String)o;
Page 48
OOP with Java
Dr. Ahmed M. Al-Salih
University of Babylon/ College of IT
class – First Semester- Department of Software
2nd
t.lenght ()// 3
c=t.charAt(1);
c=t.charAt(3);
Casting on a primitive types
Casting means assigning a value of one type to a variable of another type. If
two types are compatible, the java software performs the conversation automatically.
If information might be lost in an assignment, the programmer must confirm the
assignment with a cast.
• The assignment between long and int requires an explicit cast.
long bigValue = 99L;
int x = bigValue; // Wrong, needs a cast
int x = (int) bigValue; // OK
int x = 99L; // Wrong, needs a cast
int x = (int) 99L; // OK, but...
int x = 99; // default integer literal
Example
Short a,b,c;
a=1;
b=2;
c=a+b; // is this correct?
Page 49
OOP with Java
Dr. Ahmed M. Al-Salih
Page 50
University of Babylon/ College of IT
class – First Semester- Department of Software
2nd
Download