lecture 01 oop concepts

Upload: vijaya-lakshmi

Post on 10-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Lecture 01 OOP Concepts

    1/28

    The Robert Gordon University

    Object Oriented Programming

    (CMM504)

    Lecture#1

    Object-Oriented ProgrammingConcepts

  • 8/8/2019 Lecture 01 OOP Concepts

    2/28

    The Robert Gordon University K. Hui 2008-2009 2

    Content Object-Oriented Programming

    Concepts components of an object

    class-level vs object-level

    Main features of Object-OrientedProgramming

  • 8/8/2019 Lecture 01 OOP Concepts

    3/28

    The Robert Gordon University K. Hui 2008-2009 3

    Object-Oriented Programming

    Concepts OOP models the world as a collection of

    objects

    e.g.you, your classmates, your dog, your mobilephone

    objects may relate to/interact with eachothers e.g. you have a dog, you make a call with your

    mobile, you talk to your classmates interaction does something

    change of state, side-effect of interaction (e.g.display on screen, file operation), etc.

  • 8/8/2019 Lecture 01 OOP Concepts

    4/28

    The Robert Gordon University K. Hui 2008-2009 4

    How to Program in the OO

    Way? Model your world (i.e. problem domain)

    as objects

    What objects do you needed?

    How are these objects related to eachother? (i.e. relationships among them)

    Do these relationships make sense? How do these objects interact to achieve

    what you want?

  • 8/8/2019 Lecture 01 OOP Concepts

    5/28

    The Robert Gordon University K. Hui 2008-2009 5

    The Concept ofObject a self-contained entity

    has 2 components:attributes &

    methods attributes are also called instance variables

    may or may not be visible to the outside

    world (i.e.other objects)

    i.e. can be hidden

    interaction with other objects iscontrolled/ confined by its interface

  • 8/8/2019 Lecture 01 OOP Concepts

    6/28

    The Robert Gordon University K. Hui 2008-2009 6

    Attribute & Method attributes

    the datacomponentofan object

    define the descriptive characteristicsofan object

    e.g.your dog is brown in colour, your mobile is branded Nokia

    methods the procedural componentof an object

    define the behaviour of an object

    e.g.a dog walks, a mobile sends SMS

    an attribute/method must belong tosomething (setting its context) you will not find an attribute/method that belongs to nobody

  • 8/8/2019 Lecture 01 OOP Concepts

    7/28

    The Robert Gordon University K. Hui 2008-2009 7

    Interface of an Object an object can hide its internal components

    from the outside world

    why hiding? the set of visible components defines the

    object's interface to the outside world an object's interface defines how it can

    interact with others NOTE: some time later, you will see the term

    "interface" in Java, which is NOT the samething

  • 8/8/2019 Lecture 01 OOP Concepts

    8/28

    The Robert Gordon University K. Hui 2008-2009 8

    Calling vs Invoking a Method in OOP, you dont really call a method

    for A to invoke a method in B:

    A sends a message to B, asking B to invoke amethod

    e.g. you ask your dog to give you its paw

    B receives the message and decides how to

    respond invoking the appropriate method

    e.g. your dog and cat may give different responses tothe same request

  • 8/8/2019 Lecture 01 OOP Concepts

    9/28

  • 8/8/2019 Lecture 01 OOP Concepts

    10/28

    The Robert Gordon University K. Hui 2008-2009 10

    The Concept ofClass a template for creating objects

    e.g. the dog class defines the attributes

    & methods of a dog, but the dog class isNOT an object.

    an object is created from a class an instance of a class

    e.g. you are an instance ofthe studentclass, your dog isan instance ofthe dogclass

  • 8/8/2019 Lecture 01 OOP Concepts

    11/28

    The Robert Gordon University K. Hui 2008-2009 11

    Objects & Classes objects of the same class

    have the same set of attributes & methods

    e.g. each student has a student no. attribute,each dog has a colour attribute

    may have different attribute values

    each object has

    its

    own copy of the attribute e.g. differentstudentsmay have different

    "student no." values, different dogs may havedifferent "colour" values

  • 8/8/2019 Lecture 01 OOP Concepts

    12/28

  • 8/8/2019 Lecture 01 OOP Concepts

    13/28

  • 8/8/2019 Lecture 01 OOP Concepts

    14/28

  • 8/8/2019 Lecture 01 OOP Concepts

    15/28

    The Robert Gordon University K. Hui 2008-2009 15

    The Context of Attributes &

    Methods an attribute/ method must be attached/

    belong to something

    its context

    when you refer to anattribute/method, you must

    specify its context whose attribute/method is it?

    e.g. Lassies colour, tell Peter to write an email

  • 8/8/2019 Lecture 01 OOP Concepts

    16/28

    The Robert Gordon University K. Hui 2008-2009 16

    Class-level vs Object-level

    Context attribute/ method can be defined at the class-

    levelor object-level (i.e.instance-level)

    e.g. the bank account classmay have a class-levelattribute interest rate which isshared by allbank account objects

    each object has itsown local copy of an

    object-levelattribute a class-level attribute is shared by all

    objects of that class, only 1 single copy

  • 8/8/2019 Lecture 01 OOP Concepts

    17/28

    The Robert Gordon University K. Hui 2008-2009 17

    When to Make it Object/Class

    Level? object/instance-level attribute:

    if you want each object to have its own copy ofthe attribute (i.e. not shared)

    e.g. each dog object has its own copy of thecolour attribute (probably of different value)

    class-level attribute if you want all objects of the same class to share

    the same copy of the attribute e.g. The Dog class has a class-level attributedefault_num_of_legs which is shared by alldog objects.

  • 8/8/2019 Lecture 01 OOP Concepts

    18/28

    The Robert Gordon University K. Hui 2008-2009 18

    Object-level vs Class-level

    Method object-level method:

    when it makes sense that the behaviour should

    occur in individual objects e.g. deposit of money into a bank account

    you must specify which bank account to deposit, not toall objects in the same class!

    class-level method: usually as utility methods e.g. the String class has a method parseInt()

    that converts a string into an int value

  • 8/8/2019 Lecture 01 OOP Concepts

    19/28

    The Robert Gordon University K. Hui 2008-2009 19

    How to Program in OOP? (Re-

    Revisited) design classes

    design their internals & interfaces

    take other peoples classes know their interfaces

    i.e. what are visible to the outside world

    create objects

    let objects interact via their interfaces i.e. visible attributes/methods

    achieve some tasks

  • 8/8/2019 Lecture 01 OOP Concepts

    20/28

    The Robert Gordon University K. Hui 2008-2009 20

    OOP Features 4 major features in OOP

    encapsulation information hiding

    inheritance

    overloading

  • 8/8/2019 Lecture 01 OOP Concepts

    21/28

    The Robert Gordon University K. Hui 2008-2009 21

    Encapsulation an object encapsulates both its

    attributes & methods

    implications:

    an attribute/ method is attached to an object/class

    when you mention an attribute/ methods, youhave to specify which object/ class it comes from

    why encapsulation? when you get hold of an object, you also get hold

    of its data & behaviour components good for reuse

  • 8/8/2019 Lecture 01 OOP Concepts

    22/28

    The Robert Gordon University K. Hui 2008-2009 22

    Information Hiding an object can hide its internal details

    e.g. you dont know how your mobiles electronics

    works except punching the buttons can selectively show some details to the

    outside world e.g. your mobile only shows the number it dials

    defines an interface to interact with theoutside world e.g. your mobile interacts with your through the

    buttons & screen

  • 8/8/2019 Lecture 01 OOP Concepts

    23/28

    The Robert Gordon University K. Hui 2008-2009 23

    Why Information Hiding? the object can have a complex internal but

    simple interface making interaction with the outside world easier

    you dont need to know about the internal ofan object only the interface is important i.e. how to interact with it

    facilitate code reuse hiding any internal change from the outside world

    by keeping the interface unchanged

  • 8/8/2019 Lecture 01 OOP Concepts

    24/28

    The Robert Gordon University K. Hui 2008-2009 24

    Inheritance a class may be similar to another

    class but being more specialised

    e.g. the class student is similar to theclass person but student is morespecialised

    a person has attributes like:

    sex, age, name a student has all these + a student no.

  • 8/8/2019 Lecture 01 OOP Concepts

    25/28

    The Robert Gordon University K. Hui 2008-2009 25

    Inheritance (contd) a subclass

    extends/ specialises a superclass inheritsattributes& methods from itssuperclass may have more attributes/ methods than its

    superclass may change the content/ procedure of an

    inherited method i.e.same method name/ signature but different

    behaviour

    why inheritance? reuse existing class definition customise/ specialise if needed

  • 8/8/2019 Lecture 01 OOP Concepts

    26/28

    The Robert Gordon University K. Hui 2008-2009 26

    Overloading different functions/ procedures/ methods can

    have the same name

    provided that the parameters are of differenttypes giving a unique signature

    the system will figure out which one to invoke

    e.g. you can have 2 procedures, both named

    call, taking a dog or person object asparameter respectively. Depending on you give ita dog or person object as the parameter, Javawill know which one to use.

  • 8/8/2019 Lecture 01 OOP Concepts

    27/28

    The Robert Gordon University K. Hui 2008-2009 27

    Why Overloading? you can call the same method (name)

    but invoke different behaviour

    dynamic binding of method

    which method to invoke is determined atruntime

    code reuse in term of the calling code

  • 8/8/2019 Lecture 01 OOP Concepts

    28/28

    The Robert Gordon University K. Hui 2008-2009 28

    Summary OOP models the world as objects

    classes are templates

    objects are instances of classes contain attributes (data) + methods

    (procedures)

    attributes & methods are attached to aclass or object

    OOP focuses on code reuse