Inheritance-2 - faculty.uoh.edu.sa

Report
Programming With Java
ICS201
ICS 201
Introduction to Computer Science
Inheritance
University Of Ha’il
1
Programming With Java
ICS201
Inheritance Hierarchy
Programming With Java
ICS201
Encapsulation



Data Hiding
Abstraction
Security
‫؟‬
3
Encapsulation (data hiding)
Programming With Java
ICS201


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
withDraw any value
AccountValue
Change Address
Name
Address
Programming With Java
ICS201
Encapsulation
Sell (1 SR, Pepsi)
Person
Buy Pepsi
Vending
Machine
Sell
5
Programming With Java
ICS201
- public and private Modifiers …
Illegal because we try to access a private member (age) from o
utside the class Employee
Problem ..
Programming With Java
ICS201

It is considered good programming practice to make all
instance variables private
Question: how to access and modify the instanc
e variables of Employee objects e1, e2, e3 and e
4? .. answer .. Use accessor and mutaor method
s …. next slide ..
Programming With Java
ICS201

- 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;
}
Programming With Java
ICS201
- 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
Encapsulation and Inheritance Pitfall: Use of Private
Instance Variables from the Base Class
Programming With Java
ICS201

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
Encapsulation and Inheritance Pitfall: Use of
Private Instance Variables from the Base Class
Programming With Java
ICS201

Instead, a private instance variable of the base class
can only be accessed by the public accessor and
mutator methods defined in that class
Access Modifiers
Programming With Java
ICS201



public

Can be used
restriction
in
any
Java
program
without
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 )
What is Package ?
Way to group a related class into one unit

To resolve the name conflicts between class
Programming With Java
ICS201


names
import packageName.className ;
Include all the classes in package

import packageName.*;
Programming With Java
ICS201
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
Visibility and Inheritance
Public
default
protected
private
Clients in same
package
C
C
C
None
Clients in different
packages
C
None
None
None
Subclass in same
package
C
C
C
None
Subclass in
different package
C
None
C
None
Programming With Java
ICS201
Visibility
Note: C; Can access
default: If the access modifier is omitted
Programming With Java
ICS201
Access Modifiers
© 2006 Pearson Addison-Wesley. All rights
reserved
7-16
The class Object
All classes defined without an explicit extends clause
automatically extend Object
Programming With Java
ICS201

The Object Class is the Superclass of Every Java Class
The Class Object
Programming With Java
ICS201

The class Object is in the package java.lang
which is always imported automatically
public class Circle
Equivalent
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

similar documents