Information Systems Programming with Java, 2nd edition - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Information Systems Programming with Java, 2nd edition

Description:

At the abstract level, a class can be described as an interface that defines the ... The interface for a class is provided by its public methods. ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 22
Provided by: spotCo
Category:

less

Transcript and Presenter's Notes

Title: Information Systems Programming with Java, 2nd edition


1
Chapter 3DATA TYPES, CLASSES, OBJECTS and I/O
Chapter 8Object-Oriented Programming Part I
2
Object-Oriented Programming Concepts
  • Encapsulation
  • Chapter 8
  • Polymorphism
  • Chapter 8 and 9
  • Inheritance
  • Chapter 9

3
Classes The Abstract Level
At the abstract level, a class can be described
as an interface that defines the behavior of its
objects. The abstract view of a class as an
interface provides its outside view while hiding
its internal structure and behavior details. The
interface for a class is provided by its public
methods. It dictates what must be supplied to
the object, and how the object will respond.
4
Classes The Implementation Level
The class implementation provides its inside
view, showing the data and method
implementation. At the implementation level, a
class describes a set of data and related
operations that are common to its objects. The
public class methods form the interface to the
class and are accessible from outside the class.
A private class member is only accessible within
the class by methods of the same class.
5
Class Declaration Format
class class name //DATA MEMBERS private
data type variable ?
? private data type variable
//METHOD MEMBERS public return type method
name (parameter listing) //METHOD
STATEMENTS   ? ?   
public return type method name (parameter
listing) //METHOD STATEMENTS   //END
CLASS
Data members are always private
Method members are usually public, but can also
be private utility methods.
6
Encapsulation and Information Hiding
Encapsulation means to package data and/or
operations into a single well-defined programming
unit. Encapsulation with Information hiding means
that there is a binding relationship between the
information, or data, and its related operations
so that operations outside the encapsulated unit
cannot affect the information inside the unit.
7
Encapsulation and Information Hiding
In Java, the class provides for encapsulation and
information hiding. The public methods provide
the class interface that is accessible from
outside the class. The private data members
provide the information that is accessible only
from within the class, thus providing the
information hiding.
8
Encapsulation with Information Hiding
9
Encapsulation and Information Hiding
  • Encapsulation is the mechanism that implements
    information hiding in an OO language.
  • Benefits
  • Easier to test and maintain
  • Can easily be reused
  • Flexible for future changes
  • Cost
  • Must use a method call to access private data,
    slightly reducing the efficiency of the code

10
Access Methods
  • Get (or accessor) methods return the value of a
    private variable.
  • Return a value to be used in an arithmetic
    operation or in a print statement
  • Set (or mutator) methods change the value of a
    private variable.
  • User data entry

11
Constructors
A constructor is a special class method that is
used to initialize an object automatically when
the object is defined.
Method header SavingsAccount() Initializing an
object SavingsAccount yourAccount new
SavingsAccount()
Constructor call and possible arguments
Object definition
12
Creating and Using Constructors
The name of a constructor is always the same as
the name of the class. The constructor cannot
have a return type, not even void. A class cannot
have more than one constructor however, the
constructor is often overloaded. Constructors
should only be used to initialize an object for
processing.
13
Constructor with Parameters
SavingsAccount(double bal) balancebal
SavingsAccount yourAccount new
SavingsAccount(1000.00)

Constructor call and argument
Object definition
14
Overloaded Constructors
A constructor method can be overloaded just like
any other method. An overloaded constructor will
perform different tasks depending on the number
and/or type of arguments passed that it
receives. You can use overloaded constructors to
initialize a different number of the classes data
members.
15
Example
  • class Rectangle
  • //DATA MEMBERS
  • private double length 0.0
  • private double width 0.0
  • //CONSTRUCTOR NO ARGUMENTS
  • public Rectangle()
  • //END Rectangle()
  • //CONSTRUCTOR ONE ARGUMENT
  • public Rectangle(double side)
  • length width side
  • //END Rectangle(double)
  • //CONSTRUCTOR TWO ARGUMENTS
  • public Rectangle(double l, double w)
  • length l
  • width w

16
Example cont.
  • public double perimeter()
  • return 2 (length width)
  • //END perimeter()
  • public double area()
  • return length width
  • //END area()
  • public double getLength()
  • return length
  • //END getLength
  • public double getWidth()
  • return width
  • //END getWidth
  • //END Rectangle

17
Example cont.
  • public class RectangleTest
  • public static void main(String args)
  • //DEFINE OBJECTS
  • Rectangle box1 new Rectangle()
  • Rectangle box2 new Rectangle(2)
  • Rectangle box3 new Rectangle(3,4)
  • StaugIO io new StaugIO()
  • io.writeInfo(The length of box1 is
    box1.getLength()
  • \nThe width of box1 is box1.getWidth()
  • \nThe perimeter of box1 is
    box1.perimeter()
  • \nThe area of box1 is box1.area())
  • System.exit(0)
  • //END main()
  • //END RectangleTest

18
Packages
A package groups related classes together in one
directory. All files that make up a given package
must be stored in a directory (folder) whose name
is the same as the package name. Classes that
are part of packages must be declared public so
that they are visible and can be accessed outside
the package in which the class resides.
19
Packages
You can compile classes independently and import
them when needed into an application. Java
requires that you set the operating system
CLASSPATH variable so it knows where to look for
user-defined packages. Package names should begin
with a lowercase letter, subsequent words should
begin with an uppercase letter.
20
Packages
Places SavingsAccount class in banking package
package banking public class SavingsAccount
SavingsAccount must be public
21
Packages
Imports SavingsAccount class of banking package
into application
import banking.SavingsAccount public class
Action08_03 public static void
main(String args)
Write a Comment
User Comments (0)
About PowerShow.com