MELJUN CORTES JAVA_OOP

Embed Size (px)

Citation preview

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    1/50

    Object-Oriented

    Programming

    Java Fundamentals and Object

    Oriented Programming

    MELJUN CORTES MBA MPA BSCS

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    2/50

    What You Should Learn

    I. What is OOP?

    A. History

    B. Definition

    C.

    GoalsII. What is an Object?

    A. Definition

    B. Interface

    C. Class vs.Instance

    III. Three Core

    Principles of OOP

    A. Encapsulation

    B. Inheritance

    C. Polymorphism

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    3/50

    Why OOP? A little

    history The Software Crisis (1960s 1980s)

    Computers became more powerful, and so programs

    became larger and more complex.

    Software quality became terrible!

    Too many bugs.

    Schedules were not being met.

    Difficult to add features or make changes to software.

    Existing code could not be made the building blocks

    for new codeeasier to write from scratch!

    The field of software engineering was born!

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    4/50

    Software Engineering

    creating high-quality software systems inan efficient and predictable manner

    Abstractionwas one of the prime concepts

    used to simplify programming problems

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    5/50

    Abstraction

    Wikipedia: abstraction is a mechanismand practice to reduce and factor out detailsso that one can focus on a few concepts at a

    time

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    6/50

    Abstractionevolutions

    Procedural Programming

    routines were grouped into functions

    one function can call another function

    you didn't have to understand each line, justwhat each function did

    you could hide data to be accessible to onlywithin a function (encapsulation)

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    7/50

    Abstractionevolutions

    Structured Programming

    further refinement of proceduralprogramming

    formal methods of planning data-flow andfunctional decomposition

    goto banned

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    8/50

    Abstractionevolutions

    Object-Oriented Programming

    Takes encapsulat ioneven further bylocalizing data and associated operations

    into a mini-program called an object.

    An OO program is an ecosystem of objectsthat interact with each other.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    9/50

    What is Object Oriented

    Programming?

    Think of an OO system as a bunch of

    intelligent animals (the objects) inside

    your machine, talking to each other by

    sending messages to one another. Allan Holub

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    10/50

    What is Object Oriented

    Programming?

    OOP takes abstraction furthest by

    allowing you to group related data and

    operations into different types of objects.

    You no longer have to keep track of eachvariable or each function, just the different

    types of objects.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    11/50

    What is Object Oriented

    Programming?

    You create your own data types, which are

    types of objects. Each of these data types

    are called classes.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    12/50

    What is Object Oriented

    Programming?

    Creating your own classes allows you todesign a program so that it is intuitive toremember how it is organized.

    For example, you can create classes thatrepresent real-world business entities.

    You can create classes to have specificresponsibilities, so that when you need toupdate a piece of code, you know exactly

    where to look for it.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    13/50

    Goals of OOP

    Comprehensibility - make it easier for humansto understand the code

    Maintainability - make code easy to modify

    Reusability - old code should be building blocksfor new code

    Pluggability - you can create interchangeable

    components that can substitute for one another,just like machine parts

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    14/50

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    15/50

    What is an Object?

    Ex: Car

    Attributes:

    steering wheel

    engine color

    radio

    airconditioner

    Methods:

    go forward

    go backward cool the interior

    play music

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    16/50

    What is an Object?

    Ex: Purchase Order

    Attributes:

    PO Number

    Buyer Seller

    List of items being

    purchased

    Methods:

    get PO number

    get buyer get seller

    get number of items

    get item number __

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    17/50

    What is an Object?

    Ex: DB Connection

    Attributes:

    URL of DB

    user

    password

    transaction isolationlevel

    is read-only? (boolean)

    is auto-commit?(boolean)

    Methods:

    create SQL statement

    return whether read-

    only set transaction

    isolation level

    close connection

    set save point

    rollback

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    18/50

    What is an Interface?

    An object has an interface.

    The outward appearance of an object. How

    other objects see the object.

    The attributes and methods that the objectexposes.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    19/50

    What is an Interface?

    Normally, an object will only expose some of itsattributes and methods.

    Some attributes and methods are used only bythe object itself. Therefore, no other object

    should have access to those. Some attributes and methods may be made

    accessible only to certain other objects.

    Some attributes and methods may be accessibleby any other object.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    20/50

    What is an Interface?

    Normally, only methods are exposed.

    Objects usually hide their data to protect

    them from being changed by other objects

    without their control. Constants are an exception. These dont

    change anyway.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    21/50

    What is a Class and an

    Instance?

    My car is a 92 Nissan Sentra. That is its

    class. There are many other 92 Nissan

    Sentras out there.

    But there is only one instance of my car.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    22/50

    What is a Class and an

    Instance?

    Classthe definition of an object

    Instancethe created object of a class

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    23/50

    Three Core Principles of

    OOP

    Encapsulation

    Inheritance

    Polymorphism

    (note: different texts will have differing sets of core principles)

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    24/50

    What is Encapsulation?

    Encapsulation has two definitions:

    The grouping of data and operations into

    objects.

    Hiding of data and operations from otherobjects.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    25/50

    What is Encapsulation?

    The first definition can be described as

    Cohesion.

    Related data and operations should not be

    separated. They should be found in asingle object.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    26/50

    What is Encapsulation?

    Ex: Car

    You shouldnt have to ask someone with a

    radar gun to measure the speed of your

    car. Your car should have its ownspeedometer to tell you that.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    27/50

    What is Encapsulation?

    Ex: Purchase Order object

    Data for purchase orders should not be

    lumped in the same objects as data for

    invoices and receipts. The methods for retrieving data from a

    purchase order should not be found in a

    separate class from the data.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    28/50

    What is Encapsulation?

    Ex: DB Connection object

    The DB Connection object should not

    need to lookup the URL to the database

    from another object every time it does anoperation.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    29/50

    What is Encapsulation?

    The other definition can be defined as

    information hiding.

    An object should expose only what is

    necessary, and only at the appropriatelevel.

    Think CIA... need-to-know.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    30/50

    What is Encapsulation?

    Ex: Car

    To driver: only steering wheel, pedals, and stick

    shift exposed. Driver should not access engine

    or gears or axle to drive the car.

    Mechanic: access to engine, gears, etc., but not

    internals of each part.

    Manufacturer: access to internals of each part.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    31/50

    What is Encapsulation?

    Ex: Purchase Order object

    Any object can get info from the object,

    but only certain objects should have

    authority to set info. Only certain objects should be allowed to

    create the PO object.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    32/50

    What is Encapsulation?

    Ex: DB Connection object

    Only the Driver Manager object can create

    a connection

    Users cannot set whether a connection isread-only or not.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    33/50

    What is Encapsulation?

    Benefits:

    Simpler interfaces

    Only a few methods are exposed to other

    objects. Since interactions between objectsare simple, system is easier for theprogrammer to comprehend.

    Data protected from corruption

    Easier to modify code or find bugs Because of simpler interfaces.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    34/50

    What is Encapsulation?

    Objects should only expose members to each

    other through well-defined and simple interfaces.

    Example: A driver drives a car with only steering

    wheel, pedals, gear-shift and dashboard meters

    and gauges.

    Changes in one component will not affect the

    others since the interfaces remain the same.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    35/50

    Inheritance

    A way to create a new class by derivingfrom another class.

    The new class acquires the interface of the oldclass. - Interface Inheritance

    The new class often also acquires theimplementations of the old class. -Implementation Inheritance

    The new class can change theimplementations of the older class or add itsown methods and attributes.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    36/50

    Inheritance

    Subclasses become sub-types of the super

    classes.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    37/50

    Inheritance

    You can choose to refer to a class by one of its

    super types if you only need the generic

    interface.

    You can choose to refer to a class by its specific

    type if you only need the specialized interface.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    38/50

    Inheritance

    Component

    Button Checkbox Container TextComponent

    TextArea TextFieldWindow

    Dialog Frame

    Example of an actual class heirarchy, part

    of the Java GUI library:

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    39/50

    Inheritance

    Inheritance is a way to allow objects to

    share code, preventing code-duplication.

    Code-duplication is the #1 sin in OOP.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    40/50

    Inheritance

    Implementation inheritance is dangerous!

    The Fragile Base Class Problem

    A subclass mustinherit all inheritable members ofthe superclass.

    No option to disinherit a member.

    If the subclass inherits members that it doesntneed, those members might be called by otherobjects in a way that the creator of the subclassdid not intend (remember, youre working in ateam) causing undesirable behavior.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    41/50

    Inheritance

    Implementation inheritance is dangerous!

    The Fragile Base Class Problem

    If someone modifies the superclass, the subclass

    will inherit the change! Again, this might cause undesirable behavior!

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    42/50

    Inheritance

    Implementation inheritance is dangerous!

    Other issues:

    Long hierarchies become complex and difficult to

    manage. Long hierarchies lead to classes with complex

    interfaces.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    43/50

    Inheritance

    Implementation inheritance is dangerous!

    Prefer interface inheritance to implementation

    inheritance. At least you dont inherit

    behavior, just the interface. Never extend a class simply to save yourself

    some typing! There has to be a strong logical

    is-a relationship between superclass and

    subclass.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    44/50

    Polymorphism

    When a single datatype exhibits different

    behaviors during execution.

    Greek: Poly means many, morph means

    form. Polymorphism means existing inmany forms.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    45/50

    Polymorphism

    It is the other side of the same coin as

    Inheritance.

    Polymorphism is the reasonwhy we want

    to have inheritance.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    46/50

    Polymorphism

    Polymorphism allows for pluggability or

    substitutability.

    Types that implement the same interface can

    substitute for one another. Client code just sees one class, but the actual

    concrete type can be different in different

    cases.

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    47/50

    Polymorphism

    Method Overriding

    when a subclass re-implements one or more

    methods from the superclass

    changes the behavior of the method

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    48/50

    Polymorphism

    You can pass a subclass to a method

    wherever a particular class is required.

    void add(Component c);you can pass instance of Button, TextArea,

    Dialog, etc, since they all inherit the interface of

    Component

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    49/50

    Polymorphism

    Since each subclass implements the methods of

    Component differently, each subtype gets drawn

    differently, receives input differently, listens for

    different events, etc.

    void add(Component c);you can pass instance of Button, TextArea,

    Dialog, etc, since they all inherit the interface of

    Component

  • 8/10/2019 MELJUN CORTES JAVA_OOP

    50/50

    The End

    Java Fundamentals and Object-OrientedProgramming