basic - week 02

Upload: pendekar-jelapang-padi

Post on 06-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Basic - Week 02

    1/36

    TIA1013 : Introduction ToProgramming

    Week 2Introduction to

    Object Oriented Programming

  • 8/3/2019 Basic - Week 02

    2/36

    Object, Class, Message, Method &

    Attributes

    Object-Oriented Programming

    Characteristics

    Lecture Outlines

  • 8/3/2019 Basic - Week 02

    3/36

    To describe fundamental concepts of OOP.

    To state the difference between objects

    and classes To identify characteristics of OOP.

    To draw a basic of UML class diagram.

    Learning Objectives

  • 8/3/2019 Basic - Week 02

    4/36

    Two popular programming design methods:

    Structured Programming (SP)

    Object-Oriented Programming (OOP)

    Introduction to OOP

  • 8/3/2019 Basic - Week 02

    5/36

    Other name: top-down, stepwise, modular

    Main problem will be divided into sub-problem

    Analyze each sub-problem

    All sub-problem solutions will be combinedto solves the main problem

    Structured Programming(SP)

  • 8/3/2019 Basic - Week 02

    6/36

    Designs a program as collections of interactingobjects that work together to accomplish tasks.

    Model the world as it is.

    The main concepts OO:

    Objects, Classes and Instances

    Attributes and operations (behaviour)

    Object State Message-passing and methods

    Object Oriented Programming

    (OOP)

  • 8/3/2019 Basic - Week 02

    7/36

    Identify object as foundation for problemsolving

    Identify interaction method for each object

    Identify data/attribute for each object

    Identify operation for each data

    Object will combines data and operation in

    one unit An OOP program is a collection of objects

    that interacts each other

    Object Oriented Programming

    (OOP)

  • 8/3/2019 Basic - Week 02

    8/36

    An Object is a thing, both tangible andintangible. Account, Vehicle, Employee, etc.

    Objects have state, behaviour and identity.

    State: the condition of an object at any moment,affecting how it can behave

    Behaviour: what an object can do, how it canrespond to events and stimuli

    Identity: each object is unique

    Objects are categorized into classes

    Each individual object is an instance of a class

    Objects

  • 8/3/2019 Basic - Week 02

    9/36

    To create an object inside the computerprogram, we must provide a definition forobjectshow they behave and what kindsof information they maintain called a

    Class. Class - a category of objects that share the

    same:

    attributes,

    operations/methods

    Attribute keeping objects characteristics

    Operation represents objects behavior.

    Class

  • 8/3/2019 Basic - Week 02

    10/36

    Attribute- a named propertyof a class thatdescribes a range of values that instances ofthe attribute might hold

    Attributes are the way classes encapsulatedata

    Example:

    Class : Employee

    Attributes: employee_name, employee_id,

    position, department etc..

    Attribute

  • 8/3/2019 Basic - Week 02

    11/36

    A behaviorof an object Implemented in classes are methods Methods are identified and invoked by their

    signatures, including name, parameters, andreturn type

    Example:Class : EmployeeAttributes: employee_name, employee_id,

    position, department, salary etc..

    Operations: setEmployeeDetailscalculateSalaryprintEmployeeDetails

    Operation

  • 8/3/2019 Basic - Week 02

    12/36

    ClassTemplate (blue print) to define specificinstances or objects

    ObjectInstantiation of a class

    AttributesDescribes the object

    Behaviors/Operationspecify what object can do

    Methods implement an objects behavior

    Analogous to a function or procedure Messages are sent to trigger methods

    Procedure call from one object to the next

    Summary

  • 8/3/2019 Basic - Week 02

    13/36

    Basic skeleton of Java class

    /*Sample program: Displaying Welcome*/

    public class Greeting{

    public static void main (String[] args){System.out.println(Welcome);

    }

    }

    Class Declaration

  • 8/3/2019 Basic - Week 02

    14/36

    Comments

    Reserved words Identifiers

    Statements

    Blocks

    Classes

    Methods

    The main method

    //printing welcome

    public class Greeting {

    public static void main(String[] args)

    {

    System.out.println(Welcome!");

    }

    }

    StringMain method signature

    Class title Class name

    block

    block

    Java Anatomy

  • 8/3/2019 Basic - Week 02

    15/36

    The Java class declaration takes the followingform:

    * class {

    **

    }

    Class Declaration

  • 8/3/2019 Basic - Week 02

    16/36

    public void print(){System.out.println(This CD title is

    + title + and still available

    + no_of_stock + copies);}

    }

    Method definition

    Class Declaration

    public class Cd {private String title;private String no_of_stock;

    public void setData(){title = Animal Planet;no_of_stock = 45;

    }

    class

    attributes

  • 8/3/2019 Basic - Week 02

    17/36

    The notation we used here is based on the industry standardnotation called UML, which stands for Unified Modeling Language.

    Example:

    Graphical Representation of a

    Class

  • 8/3/2019 Basic - Week 02

    18/36

    Graphical Representation of a

    ClassCd

    - title

    - no_of_stock

    + setData()

    + print()

  • 8/3/2019 Basic - Week 02

    19/36

    To instruct a class or an object to perform a task,we send a messageto it.

    You can send a message ONLY TO the classes

    and objects that understand the message yousent to them.

    A class or an object must possess a matchingmethodto be able to handle the received

    message.

    Message and Method

  • 8/3/2019 Basic - Week 02

    20/36

    print()

    Message print is sent toa Cd object myCd.

    myjCd : Cd

    Sending Message..

  • 8/3/2019 Basic - Week 02

    21/36

    In Java, instance/object is created by usingnewkeyword.

    Basic syntax: = new (parameters);

    Example:

    Cd myCd = new Cd ();

    Creating Object

  • 8/3/2019 Basic - Week 02

    22/36

    We use a rectangle torepresent an object andplace the underlinedname of the objectinside the rectangle.

    Example:

    myCd This is an object namedmyCd.

    Graphical Representation of

    an Object

  • 8/3/2019 Basic - Week 02

    23/36

    public class TestCd{

    public static void main (String args[ ])

    {Cd myCd = new Cd();

    myCd.print();

    }

    }

    Accessing print() method (sending message

    printto object myCd)

    Main method, Object,

    Accessing method

  • 8/3/2019 Basic - Week 02

    24/36

    class Cd {

    private String title;private int no_of_stock;

    public void setData(){title = Animal Planet;no_of_stock = 45;

    }void print() {System.out.println(This CD title is

    + title + and still available

    + no_of_stock + copies);

    }

    }public class TestCd {

    public static void main (String args[ ]){

    Cd myCd = new Cd();

    myCd.print();

    }}

    Sample of Complete Program

  • 8/3/2019 Basic - Week 02

    25/36

    Abstraction

    Encapsulation

    Inheritance Polymorphism

    Characteristics of OOP

  • 8/3/2019 Basic - Week 02

    26/36

    Abstraction is the process of finding theessential feature set for a class.

    Concentrating on what an object isand does

    before making any decision about how theobject will be implemented.

    Capture the attributes and operation of aobject/real-world deck.

    Object in a program is an abstractionof thereal-world object. Example: myCd.

    Abstraction..

  • 8/3/2019 Basic - Week 02

    27/36

    Encapsulation is the process of hiding object data andproviding methods for data access (data and behaviorare bundled into a class and hidden from the outsideworld)

    Process of hiding the implementation details.

    Each object of a class has its own set of instance fields.Instance fields are declared with the access modifierprivate, public or protected.

    Access to the data and behavior is provided and

    controlled through an objects interface Useful, because programmer can reuse the programcode.

    Data, element, and method is being encapsulatedin aclass, and can be use by the other class.

    Encapsulation..

  • 8/3/2019 Basic - Week 02

    28/36

    Inheritance is a fundamental object-oriented designtechnique used to create and organize reusableclasses

    Inheritanceallows a software developer to derive anew class from an existing one

    The existing class is called the parent class, orsuperclass, or base class

    The derived class is called the child classorsubclass

    As the name implies, the child inheritscharacteristics of the parent

    A subclass inherits accessible data fields andmethods from its super class, and may also addnew data fields and methods.

    Inheritance..

  • 8/3/2019 Basic - Week 02

    29/36

    Inheritance..

    Super class(more general)

    Subclass

    (morespecialized)

  • 8/3/2019 Basic - Week 02

    30/36

    Inheritance..

    Inheritance relationships are shown in a UMLclass diagram using a solid arrow with anunfilled triangular arrowhead pointing to theparent class

    Vehicle

    Car

    Proper inheritance creates an is-arelationship, meaning the child is amorespecific version of the parent

  • 8/3/2019 Basic - Week 02

    31/36

    A is a superclass to BB is a subclass to AB is a superclass to C, D and E.

    C, D and E is a subclass to B

    Class A

    Class B

    Class C Class D Class E

    Inheritance..

  • 8/3/2019 Basic - Week 02

    32/36

    Inheritance..

  • 8/3/2019 Basic - Week 02

    33/36

    The ability for different classes of objects

    to respond to identical messages in

    different ways

    Polymorphism = having many forms

    Different behaviors for the same message

    Polymorphism..

  • 8/3/2019 Basic - Week 02

    34/36

    Can be overloading oroverriding.

    Overloading methods appear in the same class have the same name but, have different parameter lists, and,

    can have different return types

    Overriding methods overriding allows a subclass to re-define a method it inherits from

    it's superclass overriding methods: appear in subclasses

    have the same name as a superclass method have the same parameter list as a superclass method have the same return type as as a superclass method

    Polymorphism..

  • 8/3/2019 Basic - Week 02

    35/36

    Here, each

    type ofvehicle has

    its own

    version of

    calcPrice()

  • 8/3/2019 Basic - Week 02

    36/36

    Conclusion

    Q & A Session