Classes and Objects - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Classes and Objects

Description:

Bicycle object - current gear, current pedal cadence, current speed. Lamp object on/off ... First, we have to have a class. Then we instantiate an object of ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 30
Provided by: csU62
Category:
Tags: classes | objects

less

Transcript and Presenter's Notes

Title: Classes and Objects


1
Classes and Objects
  • CSCI-1302

2
Remember
  • We covered reference types in the last few
    lectures.
  • Objects, Strings, and Arrays are reference types.
  • Now, we will be narrowing our focus on Objects,
    Classes, and Object-oriented programming.

3
Object Oriented Principles
  • Should know the following terms and definitions
    when working with Java
  • Information hiding
  • Encapsulation
  • Generic Code
  • Inheritance (Chapter 4)
  • Polymorphism (Chapter 4)

4
Objects
  • Objects are reference types!
  • A constructor must be called to create a new
    object. i.e. Allocate space in memory and
    assign an address to the variable.
  • An object is an Instance of a given class.
  • Objects have operations that may access or
    manipulate its state.

5
Objects
  • Objects have state and behavior.
  • State - the variables that describe the object
  • The label in a button object describes the state
  • The x y coordinates describe state for Points.
  • The behavior is the methods that allow actions to
    be performed on the object.
  • This can be to get/set the state or any other
    action on the object. (setLabel for a button)

6
Objects
  • Methods operate on an object's internal state
  • An object is an atomic unit Its parts cannot be
    dissected by general users.
  • The user should not know what is inside of an
    object!

7
Objects
8
Encapsulation / Information Hiding
  • Encapsulation and information hiding are two
    separate, orthogonal concepts
  • Information hiding conceals how an object
    implements its functionality.
  • Think of private data members (more on this
    later)
  • Accessed indirectly by supplied methods
  • How is a StringTokenizer object implemented?
    What data does it contain? Do we know?

9
Encapsulation / Information Hiding
  • Encapsulation ensures that the behavior of an
    object can only be affected through the object's
    methods.
  • How is this accomplished?
  • Think about this.
  • More on this later in the class!

10
Encapsulation / Information Hiding
  • Information hiding lets one build higher level
    abstractions on top of lower level details.
  • Ignore details of the system that are unrelated
    to the task at hand.
  • Ex. - Use a Button object without worrying how it
    is created!

11
Objects
  • Try to compare objects to real-world objects.
    What is their state?
  • Objects vary in the difficulty of their state.
  • Dog object - name, color, breed, hungry
  • Bicycle object - current gear, current pedal
    cadence, current speed
  • Lamp object on/off

12
Objects
  • How do we create a new type of object, like a
    bike?
  • First, we have to have a class.
  • Then we instantiate an object of that class.
  • By calling the constructor (Using new).

13
Classes
  • In object-oriented terms, we say that your
    bicycle is an instance of the class of objects
    known as bicycles.
  • Since an object is one instance of a class, it
    has specific values for its members!
  • A class is the blueprint from which individual
    objects are created.
  • Bike object w/ specific values for its variables ?

14
Bicycle Object
15
(No Transcript)
16
Bicycle Class
  • The Bicycle class had no constructors
  • What is the default?
  • Bad practice to set values for data members
    outside of the constructor
  • The Bicycle class had no access modifiers.
  • What is the default?

17
Bicycle Class
  • The Bicycle class does not contain a main method.
  • It's not a complete application
  • it's just the blueprint for bicycles that might
    be used in an application.
  • Created and used by some other class.

18
Common Methods for Classes (not in bike class)
  • Constructors
  • Why can we have multiple versions?
  • Mutators modify the data members
  • Accessors return the data members
  • Must be careful with these! Remember
    Encapsulation!

19
Common Methods for Classes (not in bike class)
  • equals
  • Should be included to test 2 objects of the same
    type for equality!
  • Equality objects contain same information
  • How would we write this for strings??
  • toString turns the object into a string and
    returns this string
  • Automatically called with println(objectname).

20
Class Variables
  • A class contains different Variable types
  • Data Members (fields)
  • Local Variables (within a method)
  • Parameters

21
Access Modifiers
  • Data Members (fields) and Methods can be either
    Public, Private or Protected.
  • public modifier-accessible from all classes.
  • private modifier-accessible only within its own
    class.
  • protected modifier-discussed later

22
Access Modifiers
  • In the spirit of encapsulation, it is common to
    make fields private. This means that they can
    only be directly accessed from the Bicycle class.
    We still need access to these values, however.
    This can be done indirectly by adding public
    methods that obtain the field values for us

23
Access Modifiers
  • Can we obtain access to a private field if we are
    not careful coding?
  • Think about this reference types.
  • What would a get method do with a reference type?
  • How should we avoid the problem?

24
Examples from Book
  • IntCell class
  • Date Class
  • Ticket Class

25
(No Transcript)
26
(No Transcript)
27
(No Transcript)
28
(No Transcript)
29
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com