Lesson: ObjectOriented Programming Concepts - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Lesson: ObjectOriented Programming Concepts

Description:

... tandem bikes, for example, all share the characteristics of ... tandem bicycles have two seats and two sets of handlebars. road bikes have drop handlebars ... – PowerPoint PPT presentation

Number of Views:220
Avg rating:3.0/5.0
Slides: 37
Provided by: TimothyCL4
Category:

less

Transcript and Presenter's Notes

Title: Lesson: ObjectOriented Programming Concepts


1
Lesson Object-Oriented Programming Concepts
  • What is an Object?
  • An object has state and behavior.
  • What is a Class
  • A class is a blueprint from which objects are
    created.
  • What is Inheritance?
  • Inheritance provides a powerful and natural
    mechanism for organizing and structuring your
    software.
  • What is an Interface?
  • An interface is a contract between a class and
    the outside world.
  • What is a Package?
  • A package is a namespace for organizing classes
    and interfaces in a logical manner.

2
What Is an Object? Real-World Objects
  • Object Oriented Technology views software in the
    same way we view real-world objects.
  • Real-world objects share two characteristics
  • They all have state and behavior.
  • Examples of real-world objects
  • Dogs have state (name, color, breed, hungry) and
    behavior (barking, fetching, wagging tail).
  • Bicycles have state (current gear, current pedal
    cadence, current speed) and behavior (changing
    gear, changing pedal cadence, applying brakes).
  • Desktop radio has states (on, off, current
    volume, current station) and behavior (turn on,
    turn off, increase volume, decrease volume, seek,
    scan, and tune).
  • You may also notice that some objects, in turn,
    will also contain other objects

3
What Is an Object? Software Objects
  • Software objects consist of state and related
    behavior.
  • An object stores its state in fields (variables).
  • exposes its behavior through methods (functions).
  • Methods
  • operate on an objects internal state.
  • serve as the primary mechanism for
    object-to-object communication.
  • Data Encapsulation
  • Hiding internal state and requiring all
    interaction to be performed through an objects
    methods is known as data encapsulation a
    fundamental principle of object-oriented
    programming.

4
What Is an Object? A Software Object
Representation of a Bicycle
  • Software objects consist of state and related
    behavior.
  • An object stores its state in fields (variables).
  • Shows its behavior through methods (functions).
  • Methods
  • Operate on an objects internal state.
  • Serve as the primary mechanism for
    object-to-object communication.

By attributing state (current speed, current
pedal cadence, and current gear) and providing
methods for changing that state, the object
remains in control of how the outside world is
allowed to use it. For example, if the bicycle
only has 6 gears, a method to change gears could
reject any value that is less than 1 or greater
than 6.
5
Benefits of Object Orientation
  • Modularity
  • The source code for an object can be written and
    maintained independently of the source code for
    other objects.
  • Once written, the source code for an object can
    be easily passed around inside the system.
  • Information-hiding
  • By interacting only with an objects methods, the
    details of its internal implementation remain
    hidden from the outside world.
  • Reduces complexity.
  • The details of its internal implementation can be
    changed without affecting other objects that use
    it.

6
Benefits of Object Orientation
  • Code re-use
  • A fundamental principle of software engineering
    is basing software development of re-usable
    technology.
  • Reusing already tested/debugged objects leads to
    higher quality software.
  • Pluggability and debugging ease
  • If a particular object turns out to be
    problematic, you can simply remove it from your
    application and plug in a different object as its
    replacement.
  • EXIf a bolt breaks, you replace it, not the
    entire machine.

7
What is a Class?
  • In the real world, youll often find many
    individual objects all of the same kind.
  • There may be thousands of other bicycles in
    existence, all of the same make and model.
  • Each bicycle was built from the same set of
    blueprints and therefore contains the same
    components.
  • In object-oriented terms, we say that your
    bicycle is an instance of the class of objects
    known as bicycles.
  • A class is the blueprint from which individual
    objects are created.

8
What is a Class? E.g. Bicycle Class
  • class Bicycle
  • int cadence 0
  • int speed 0
  • int gear 1
  • void changeCadence(int newValue)
  • cadence newValue
  • void changeGear(int newValue)
  • gear newValue
  • void speedUp(int increment)
  • speed speed increment

9
Bicycle Class Declaration
  • class Bicycle
  • int cadence 0
  • int speed 0
  • int gear 1
  • void changeCadence(int newValue)
  • cadence newValue
  • void changeGear(int newValue)
  • gear newValue
  • void speedUp(int increment)
  • speed speed increment

10
Bicycle Class Fields State
  • class Bicycle
  • int cadence 0
  • int speed 0
  • int gear 1
  • void changeCadence(int newValue)
  • cadence newValue
  • void changeGear(int newValue)
  • gear newValue
  • void speedUp(int increment)
  • speed speed increment

11
Bicycle Class Methods Behavior
  • class Bicycle
  • int cadence 0
  • int speed 0
  • int gear 1
  • void changeCadence(int newValue)
  • cadence newValue
  • void changeGear(int newValue)
  • gear newValue
  • void speedUp(int increment)
  • speed speed increment

12
Bicycle Demo Class
  • class BicycleDemo
  • public static void main(String args)
  • // Create two different Bicycle objects
  • Bicycle bike1 new Bicycle()
  • Bicycle bike2 new Bicycle()
  • // Invoke methods on those objects
  • bike1.changeCadence(50)
  • bike1.speedUp(10)
  • bike2.changeCadence(50)

13
UML Representation of Class
Name of class
Bicycle
List of Fields
gear speed
changeGear speedUp
List of Methods
14
UML Classes and Association
BicycleDemo has a reference to Bicycle (indicated
by the arrow).
Bicycle
BicycleDemo
gear speed
main()
changeGear speedUp
Bicycle does NOT have a reference to BicycleDemo
(indicated by the lack of an arrow)
15
UML Sequence Diagram Example Objective to show
how BicycleDemo creates and sends messages to
Bicycle objects.
16
What Is Inheritance?
  • Different kinds of objects often have a certain
    amount in common with each other.
  • Mountain bikes, road bikes, and tandem bikes, for
    example, all share the characteristics of
    bicycles (current speed, current pedal cadence,
    current gear).
  • Yet each also defines additional features that
    make them different
  • tandem bicycles have two seats and two sets of
    handlebars
  • road bikes have drop handlebars
  • some mountain bikes have an additional chain
    ring, giving them a lower gear ratio.

17
What Is Inheritance?
  • Object-oriented programming allows classes to
    inherit commonly used state and behavior from
    other classes.
  • For example, Bicycle now becomes the superclass
    of MountainBike, RoadBike, and TandemBike.

18
Inheritance Syntax
  • At the beginning of your class declaration, use
    the extends keyword, followed by the name of the
    class to inherit from
  • class MountainBike extends Bicycle
  • // new fields and methods defining a
  • // mountain bike would go here, e.g.
  • This gives MountainBike all the same fields and
    methods as Bicycle, yet allows its code to focus
    exclusively on the features that make
    MountainBike unique.

19
Inheritance Example
  • class MountainBike extends Bicycle
  • int seatHeight
  • public void setHeight(int newValue)
  • seatHeight newValue
  • MountainBike inherits all the fields and methods
    of Bicycle and adds the field seatHeight and a
    method to set it (mountain bikes have seats that
    can be moved up and down as the terrain demands).

20
UML Representation of Inheritance
Superclass
Closed, non-filled, solid line triangle points to
the superclass
21
Inheritance Rules
  • Java programming language specific rules
  • each class is allowed to have a maximum of one
    direct superclass.
  • each superclass has the potential for an
    unlimited number of subclasses.
  • Good Object Oriented practise rules
  • Ensure each subclass obeys the ISA rule
  • Distinctiveness rule
  • Make sure all inherited features make sense in
    each sublcass

22
Inheritance Rules ISA Rule
  • Ensure each subclass obeys the ISA rule
  • A chequing account is an account.
  • A village is a municipality.
  • A mountain bike is a bike.
  • Should Province be a subclass of Country?
  • No, a Province is not a Country.
  • Modeling a Province as a Country violates the ISA
    rule.
  • Modeling a Province as Country would artificially
    introduce complexity into the source code, making
    the source code more difficult to understand,
    maintain, and use.

23
Inheritance Rules Distinctiveness Rule
  • A subclass must retain its distinctiveness
    throughout its life.
  • Consider a bike with training wheels, for
    teaching kids to ride.
  • You might consider creating a class called
    TrainingBicycle, and making this a subclass of
    Bicycle.
  • However, a training wheel bicycle will not be a
    training bike once the training wheels are
    removed.
  • Therefore, TrainingBicycle is not a subclass of
    Bicycle.
  • Modeling a TrainingBicycle as a Bicycle would
    artificially introduce complexity into the source
    code, making the source code more difficult to
    understand, maintain, and use.
  • Actually, TrainingBicycle should not even be a
    class.
  • Its better to have a field that indicates if a
    Bicycle has training wheels.

24
Inheritance Rules MSFMS
  • Make sure that each feature of a superclass makes
    sense in each subclass.
  • Each subclass inherits the features of a
    superclass.
  • Modeling a class as a subclass of some superclass
    with one or more features that do not make sense
    in the subclass would artificially introduce
    complexity into the source code, making the
    source code more difficult to understand,
    maintain, and use.

25
What Is an Interface?
  • Objects define their interaction with the outside
    world through the methods that they expose.
  • Methods form the objects interface with the
    outside world.
  • For example,
  • the buttons on the front of your television set
    are the interface between you and the electrical
    wiring on the other side of its plastic casing.
  • You press the power button to turn the
    television on and off.

26
Motivation for Having an Interface
  • One of the main reasons for having an interface
    is
  • CONVENIENCE
  • Suppose you have written a group of re-usable
    classes and you wanted to give other Software
    Engineers easier access to the methods your group
    of classes provide.
  • If there existed a mechanism through which a
    class (or a group of classes) could somehow
    advertise or show what methods it provides, then
    this would make using the class (or group of
    classes) easier, since the users would not be
    exposed to, and, therefore, not have to be
    concerned about the internal or un-required
    features of the class.

27
What Is an Interface? Motivation
  • Java provides such a mechanism, called an
    interface.
  • An interface is a formal specification of the
    methods that a class (or a group of classes)
    implements.
  • In Java, an interface is a group of related
    methods with empty bodies.
  • A class (or group of classes) provides the
    implementation of the methods specified in the
    interface.

28
Interface Bicycle Example
  • To specify (or advertise) the methods that the
    Bicycle hierarchy provides, consider creating an
    interface called Bicycle.
  • interface Bicycle
  • void changeCadence(int newValue)
  • void changeGear(int newValue)
  • void speedUp(int increment)
  • void applyBrakes(int decrement)
  • Note the name of your Bicycle class would have
    to change. Using the name Bicycle for the
    interface is more appropriate than using it for
    the class. You might change the name to
    ACMEBicycle, and have the subclasses called
    ACMEMoutainBike, etc.)

29
Interface Bicycle Example
  • To implement this interface, use the implements
    keyword in the class declaration.
  • class ACMEBicycle implements Bicycle
  • void changeCadence(int newValue)
  • //implementation of changing the cadence.
  • void changeGear(int newValue)
  • //implementation of changing the gear.
  • interface Bicycle
  • void changeCadence(int newValue)
  • void changeGear(int newValue)

Note the Empty body for each method in the
interface.
30
UML Representation of Interface
Dashed line connects the class with the interface.
Interface
This class hierarchy implements the interface
Bicycle.
ACMEBicycle
speedUp changeGear
Closed, non-filled, dashed line triangle points
to the interface
31
Interface Rules (Java Specific)
  • Each class is allowed to implement any number of
    interfaces.
  • Each method specified in an interface cannot have
    any implementation.
  • Each method in an interface must have an empty
    body.
  • Each method specified in an interface must be
    implemented by some class in the source code.
  • If a class claims to implement an interface, all
    methods defined by that interface must appear in
    the classs source code.
  • Otherwise the class will not successfully compile.

32
What Is a Package?
  • A package is a container in which related classes
    and interfaces are grouped together.
  • Conceptually you can think of packages as being
    similar to different folders on your computer.
  • You might keep HTML pages in one folder, images
    in another, and scripts or applications in yet
    another.
  • For large programs which are composed of hundreds
    or thousands of individual classes, it makes
    sense to keep things organized by placing related
    classes and interfaces into packages.

33
What Is a Package?
  • The Java platform provides an enormous class
    library (a set of packages) suitable for use in
    your own applications.
  • This library is known as the Application
    Programming Interface (API).
  • The packages within the API represent the tasks
    most commonly associated with general-purpose
    programming.

34
Examples of Java Packages in its API
  • java.lang
  • The String, Integer, Double and other related
    classes are contained within the java.lang
    package.
  • For example, the Integer class contains state and
    behavior for common integer tasks, such as
  • Converting a string to an Integer object.
  • java.io
  • a File object allows a programmer to easily
    create, delete, inspect, or modify a file on the
    file system. The File and other related classes
    are contained within the java.io package.

35
What Is a Package? Java Packages
  • java.net
  • a Socket object allows for the creation and use
    of network sockets. The Socket and other related
    classes are contained within the jave.net
    package.
  • java.awt
  • various GUI objects control buttons and
    checkboxes and anything else related to graphical
    user interfaces. The GUI related classes are
    contained within the java.awt package.
  • There are many other packages and literally
    thousands of classes to choose from. This allows
    you, the programmer, to focus on the design of
    your particular application, rather than the
    infrastructure required to make it work.

36
Java API Documentation
  • The Java Platform API Specification contains
    the complete listing for all packages,
    interfaces, classes, fields, and methods supplied
    by the Java Platform 6, Standard Edition.
  • To view the API documentation online
  • Refer to the What Can Java Technology Do slide
    of these notes.
  • To download the API documentation and view
    offline
  • Refer to the What Can Java Technology Do slide
    of these notes
Write a Comment
User Comments (0)
About PowerShow.com