User-Defined Classes and ADTs - PowerPoint PPT Presentation

About This Presentation
Title:

User-Defined Classes and ADTs

Description:

Default definition creates String with name of object's ... Definition: A data type that specifies the logical properties without the implementation details ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 31
Provided by: manasi48
Category:

less

Transcript and Presenter's Notes

Title: User-Defined Classes and ADTs


1
Chapter 8
  • User-Defined Classes and ADTs

2
Chapter Objectives
  • Learn about classes
  • Learn about private, protected, public, and
    static members of a class
  • Explore how classes are implemented
  • Learn about the various operations on classes

3
Classes
  • class reserved word collection of a fixed
    number of components
  • Components members of a class, variables,
    constants and methods are all members
  • Members accessed by name
  • Class categories/modifiers for members
  • private
  • protected
  • public

4
Classes
  • private members of class are not accessible
    outside class
  • public members of class are accessible outside
    class
  • Class members can be methods or variables
  • Variable members declared like any other variables

5
Syntax
The general syntax for defining a class is
modifier(s) class ClassIdentifier modifier(s)
classMembers
The general syntax for using the operator new
is new classIdentifier() Uses default
constructor public ClassIdentifier()
OR new classIdentifier(argument1, argument2,
..., argumentN) Uses constructor public
classIdentifier(argument1, argument2, ...,
argumentN)
6
Classes
  • The syntax to access a data member of a class
    object or method is
  • referenceVariableName.memberName
  • Shallow copying two or more reference variables
    of the same type point to the same object
  • Deep copying each reference variable refers to
    its own object

7
Constructors
  • Guarantee that data members are initialized when
    object is declared
  • Automatically execute when class object created
  • Name of constructor is name of class
  • More than one constructor can be present in one
    class
  • Default constructor constructor without
    parameters

8
The Copy Constructor
  • Executes when an object is instantiated
  • Initialized using an existing object
  • Syntax
  • public ClassName(ClassName otherObject)

9
UML Diagram
10
UML Diagram
  • Top box name of class
  • Middle box data members and their data types
  • Bottom box member methods names, parameter
    list, return type of method
  • means public method
  • - means private method
  • means protected method

11
Example class Clock
myClock new Clock() yourClock new
Clock(9,35,15)
12
Example class Clock
13
Clock Making a Copy
  • public void makeCopy(Clock otherClock)
  • hr otherClock.hr
  • min otherClock.min
  • sec otherClock.sec

14
Example class Clock
15
The Modifier static
  • In the method heading, specifies that the method
    can be invoked by using the name of the class
  • If used to declare data member, data member
    invoked by using the class name
  • Static data members of class exist even when no
    object of class type instantiated
  • Static variables are initialized to their default
    values

16
static Data Members of a Class
Illustrate illusObject1 new Illustrate(3) Illus
trate illusObject2 new Illustrate(5)
17
Finalizers
  • Automatically execute when class object goes out
    of scope
  • Have no parameters
  • Only one finalizer per class
  • Name of finalizer finalize

18
The Method toString
  • public value-returning method
  • Takes no parameters
  • Returns address of String object
  • Output using print and println methods
  • Default definition creates String with name of
    objects class name followed by hash code of
    object

19
The Reference this
  • Refers to instance variables and methods of a
    class
  • Used to implement cascaded method calls
  • Used for data hiding

20
Inner Classes
  • Defined within other classes
  • Can be either a complete class definition or
    anonymous inner class definition
  • Used to handle events

21
Abstract Data Types
  • Definition
  • A data type that specifies the logical
    properties without the implementation details

22
Programming Example Candy Machine (Problem
Statement)
A new candy machine is bought for the gym, but it
is not working properly. The machine sells
candies, chips, gum, and cookies. In this
programming example, we write a program to create
a Java application program for this candy machine
so that it can be put into operation. We divide
this program in two parts. In the first part we
design a non-GUI application program. In the
second part we design an application program that
will create a GUI as described in the second
part. The non-GUI application program should do
the following 1. Show the customer the different
products sold by the candy machine. 2. Let the
customer make the selection. 3. Show the customer
the cost of the item selected. 4. Accept money
from the customer. 5. Release the item.
23
Programming Example Candy Machine (Input and
Output)
  • Input The item selection and the cost of the
    item
  • Output The selected item

24
Programming Example Candy Machine
  • Components
  • Cash Register
  • Dispenser
  • Machine

25
Programming Example Candy Machine
26
Programming Example Candy Machine
27
Programming Example Candy Machine
28
Programming Example Candy Machine
29
Programming Example Candy Machine
30
Programming Example Candy Machine
Write a Comment
User Comments (0)
About PowerShow.com