Docstoc

Classes and Object-Oriented Programming

Document Sample
Classes and Object-Oriented Programming Powered By Docstoc
					              Classes and Object-Oriented
                     Programming
• The essence of object-oriented programming is that
  you write programs in terms of objects in the domain
  of the problem you are trying to solve, so part of the
  development process is designing a set of types to
  suit the context.
• For a program to analyze bandy scores, you may
  have types such as Player and Team.
• The variables of the basic types don’t allow you to
  model real-world objects adequately.


Classes and Object-        tMyn                            1
Oriented...
• As well as the notion of user-defined types, object-
  oriented programming implicitly incorporates a
  number of additional important ideas, namely
  encapsulation, data hiding, inheritance and
  polymorphism.




Classes and Object-        tMyn                          2
Oriented...
    Encapsulation:
•   An object will contain a specific set of data values
    that describe the object in sufficient detail for your
    needs.
•   An object will also contain a set of functions that
    operate on it – functions that will use or change the
    set of data values.
•   They define the set of operations that can be applied
    to the object – what you can do with it, or to it.
•   Every object of a given type will incorporate the same
    combination of these things: the set of data values as
    data members, and the set of operations as member
    functions.
Classes and Object-         tMyn                         3
Oriented...
• In Java data members are often called instance
  variables and member functions are called methods.
• This packaging of data values and functions within an
  object is referred to as encapsulation.




Classes and Object-       tMyn                        4
Oriented...
• Wikipedia:

    The term encapsulation is often used interchangeably
    with information hiding, while some make distinctions
    between the two. It seems that people, however, fail
    to agree on the distinctions between information
    hiding and encapsulation though one can think of
    information hiding as being the principle and
    encapsulation being the technique. A software
    module hides information by encapsulating the
    information into a module or other construct which
    presents an interface.


Classes and Object-        tMyn                         5
Oriented...
                             A loanAccount object


                                                    The instance variables define
                              balance: 16000€
                                                    the properties that
An object contains            interestRate: 22%
                                                    distinguish the object.
everything necessary
to define its properties
                                                    The methods of
and define its operations.     calcInterest()       the object define what you
                                                    can do with it.




 Classes and Object-                   tMyn                                  6
 Oriented...
  Data hiding:
• Ideally, the instance variables of a loanAccount
  object would be protected from direct outside
  interference, and would only be modifiable in a
  controlled way.
• The ability to make the data values for an object
  generally inaccessible is called data hiding.




Classes and Object-       tMyn                        7
Oriented...
    Wikipedia:

    In computer science, the principle of information
    hiding is the hiding of design decisions in a computer
    program that are most likely to change, thus
    protecting other parts of the program from change if
    the design decision is changed. Protecting a design
    decision involves providing a stable interface which
    shields the remainder of the program from the
    implementation (the details that are most likely to
    change). In modern programming languages, the
    principle of information hiding manifests itself in a
    number of ways, including encapsulation (given the
    separation of concerns) and polymorphism.
Classes and Object-         tMyn                         8
Oriented...
                            A loanAccount object

Generally, the instance
                                                   The instance variables
variables should             balance: 16000€
                                                   of an object should
not be accessible            interestRate: 22%
                                                   generally be hidden.
from outside.
                                                   The methods of the
The methods can
                              calcInterest()       object provide the tools to
provide the means
                                                   alter the instance variables
to alter instance
                                                   in a controlled way.
variables when necessary.




 Classes and Object-                  tMyn                                  9
 Oriented...
• The methods of the object can provide a mechanism
  that ensures any changes to the instance variables
  follow a particular policy, and that the values set are
  appropriate.
• Hiding the data belonging to an object prevents it
  from being accessed directly, but you can provide
  access through functions that are members of the
  object, either to alter a data value in a controlled way,
  or simply to obtain its value.
• Hiding the data within an object is not mandatory, but
  it’s generally a good idea for at least a couple of
  reasons:


Classes and Object-         tMyn                         10
Oriented...
• Maintaining the integrity of an object requires that you
  control how changes are made.
• Direct access to the values that define an object
  undermines the whole idea of object-oriented
  programming.
• Object-oriented programming is supposed to be
  programming in terms of objects, not in terms of the
  bits that go to make up an object.
• You can think of the instance variables as
  representing the state of the object, and the methods
  that manipulate them as representing the object’s
  interface to the outside word.


Classes and Object-        tMyn                         11
Oriented...
• A program using the class interface is only
  dependent on the function names, parameter types
  and return types specified for the interface.
• The internal mechanics of these functions do not
  affect the program creating and using objects of the
  class.




Classes and Object-       tMyn                           12
Oriented...
  Inheritance:
• Inheritance is the ability to define one type in terms of
  another.
• So inheritance is a relationship called generalization
  which is a specialization/generalization relationship in
  which objects of the specialized element (the child)
  are substitutable for objects of the generalized
  element (the parent).
• It is the mechanism by which more-specific elements
  incorporate the structure and behavior of more-
  general elements.



Classes and Object-         tMyn                         13
Oriented...
• Graphically, generalization is rendered as a solid
  directed line with a large open arrowhead, pointing to
  the parent (base class).
• A class that has exactly one parent is said to use
  single inheritance; a class with more than one parent
  is said to use multiple inheritance.




Classes and Object-       tMyn                         14
Oriented...
                      Motor



                      move(d: direction; s: speed)
                      stop()
                      resetCounter()




      steeringMotor                                  mainMotor




Classes and Object-                    tMyn                      15
Oriented...
• Wikipedia:

    Inheritance is a way to form new classes using
    classes that have already been defined. The new
    classes, known as derived classes, take over (or
    inherit) attributes and behavior of the pre-existing
    classes, which are referred to as base classes. It is
    intended to help reuse of existing code with little or
    no modification.




Classes and Object-          tMyn                            16
Oriented...
    Inheritance is also sometimes called generalization,
    because the is-a relationships represent a hierarchy
    between classes of objects. For instance, a "fruit" is a
    generalization of "apple", "orange", "mango" and
    many others. One can consider fruit to be an
    abstraction of apple, orange, etc. Conversely, since
    apples are fruit (i.e. an apple is-a fruit), apples may
    naturally inherit all the properties common to all fruit,
    such as being a fleshy container for the seed of a
    plant.




Classes and Object-          tMyn                          17
Oriented...
    One common reason to use inheritance is to create
    specializations of existing classes. In specialization,
    the new class has data or behavior aspects that are
    not part of the inherited class. For example, a "Bank
    Account" class might have data for an "account
    number", "owner", and "balance". An "Interest
    Bearing Account" class might inherit "Bank Account"
    and then add data for "interest rate" and "interest
    accrued" along with behavior for calculating interest
    earned.




Classes and Object-          tMyn                         18
Oriented...
    Polymorphism:
•   The word polymorphism means the ability to assume
    different forms at different times.
•   It means that, in a hierarchy of classes, you can
    specify operations with the same signature at
    different points in the hierarchy.
•   Ones in the child classes override the behavior of
    ones in the parent classes.
•   When a message is dispatched at run time, the
    operation in the hierarchy that is invoked is chosen
    polymorphically – that is, a match is determined at
    run time according to the type of the object.


Classes and Object-        tMyn                        19
Oriented...
• Wikipedia:

    Polymorphism is the ability of objects belonging to
    different types (classes) to respond to method calls
    (member function calls) of methods of the same
    name, each one according to an appropriate type-
    specific behavior. The programmer (and the program)
    does not have to know the exact type of the object in
    advance, so this behavior can be implemented at run
    time (this is called late binding or dynamic binding).




Classes and Object-         tMyn                        20
Oriented...
    In practical terms, polymorphism means that if class
    B inherits from class A, it doesn’t have to inherit
    everything about class A; it can do some of the things
    that class A does differently. This means that the
    same method can result in different actions as
    appropriate for a specific class.




Classes and Object-         tMyn                        21
Oriented...
    Summary:
• A class is a user-defined data type.
• It is a description of a set of objects that share the
  same attributes, operations, relationships and
  semantics.
• The variables and functions declared within a class
  are called members of the class.
• The variables are called instance variables and the
  functions are called methods.
• More generically, a method is the implementation of
  an operation.


Classes and Object-        tMyn                            22
Oriented...
• Having defined a class, we can declare variables of
  the class type (also called instances of the class).
• Each instance will be an object of the class.
• An object is a concrete manifestation of an
  abstraction; an entity with a well-defined boundary
  and identity that encapsulates state and behavior; an
  instance of a class.
• OOP is a programming style based on the idea of
  defining your own data types as classes.
• It involves the ideas of encapsulation of data, class
  inheritance and polymorphism.



Classes and Object-       tMyn                        23
Oriented...

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1
posted:7/1/2013
language:English
pages:23