Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science...
-
Upload
keyon-edde -
Category
Documents
-
view
220 -
download
0
Transcript of Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science...
Pro
gram
min
g W
ith J
ava
ICS
201
University Of Ha’il 1
ICS 201
Introduction to Computer Science
Inheritance
Pro
gram
min
g W
ith J
ava
ICS
201
Encapsulation allows the programmer to group data and the methods that operate on them together in one place, and to hide details from the user.
In Java, hiding details is done by marking them private
Encapsulation (data hiding)
withDraw
Deposit
Transfer
AccountNo
AccountValue
Name Address
Change Address
withDraw any value
4
Pro
gram
min
g W
ith J
ava
ICS
201
Encapsulation
Person Vending Machine
Buy Pepsi
Sell (1 SR, Pepsi)
Sell
5
Pro
gram
min
g W
ith J
ava
ICS
201
- public and private Modifiers …
Illegal because we try to access a private member (age) from outside the class Employee
Pro
gram
min
g W
ith J
ava
ICS
201
Problem ..
It is considered good programming practice to make all in-stance variables private
Question: how to access and modify the in-stance variables of Employee objects e1, e2, e3 and e4? .. answer .. Use accessor and mutaor methods …. next slide ..
Pro
gram
min
g W
ith J
ava
ICS
201
- Accessor and Mutator Methods … The methods that retrieve the data of fields are called accessors.
The data can be accessed but not changed The name of an accessor method starts with the word get Example: public String getName()
{ return name;
}
The methods that modify the data of fields are called mutators. The name of a mutator method starts with the word set Example: public void setName(String n)
{ name = n;
}
Pro
gram
min
g W
ith J
ava
ICS
201
- Accessor and Mutator Methods (Example)
Accessor method for instance variable name
Mutator method for instance variable name
Modifying the name of e1 using a mutator method
Pro
gram
min
g W
ith J
ava
ICS
201
Encapsulation and Inheritance Pitfall: Use of Private Instance Variables from the Base Class
An instance variable that is private in a base class is not accessible by name in the definition of a method in any other class, not even in a method definition of a derived class
Pro
gram
min
g W
ith J
ava
ICS
201
Encapsulation and Inheritance Pitfall: Use of Pri-vate Instance Variables from the Base Class
Instead, a private instance variable of the base class can only be accessed by the public accessor and mutator methods defined in that class
Pro
gram
min
g W
ith J
ava
ICS
201
Access Modifiers public
Can be used in any Java program without restriction
private may only be used by the instance of the class that
declares the variable or method
Protected available to all classes in the same package available to all subclasses( even those in different
packages )
Pro
gram
min
g W
ith J
ava
ICS
201
What is Package ?
Way to group a related class into one unit
To resolve the name conflicts between class
names
Include all the classes in package import packageName.*;
import packageName.className ; import packageName.className ;
Pro
gram
min
g W
ith J
ava
ICS
201
Some Predefined Java Packages
Package Name Contents
java.applet Classes for implementing applets
java.awt Classes for graphics, windows, and GUI’s
java.awt.event Classes supporting AWT event handling
java.awt.image Classes for image handling
java.io Classes for input and output
java.lang Basic language classes like Math (always available in any Java program)
java.net Classes for networking
java.util Useful auxiliary classes like Date
Pro
gram
min
g W
ith J
ava
ICS
201
Visibility and Inheritance
Visibility Public default protected private
Clients in same package
C C C None
Clients in different packages
C None None None
Subclass in samepackage
C C C None
Subclass in different package
C None C None
Note: C; Can access default: If the access modifier is omitted
Pro
gram
min
g W
ith J
ava
ICS
201
© 2006 Pearson Addison-Wesley. All rights reserved 7-16
Access Modifiers
Pro
gram
min
g W
ith J
ava
ICS
201
The class Object
All classes defined without an explicit extends clause automatically extend Object
The Object Class is the Superclass of Every Java Class
Pro
gram
min
g W
ith J
ava
ICS
201
The Class Object The class Object is in the package java.lang
which is always imported automatically
public class CircleEquivalent
public class Circle extends Object
Most useful methods: String toString() boolean equals(Object otherObject) Object clone()
Good idea to override these methods in your own classes