Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

download Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

of 25

Transcript of Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    1/25

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    2/25

    Introduction to OOP Concept

    Session 13

    COMP6178 Introduction to Programming

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    3/25

    Introduction to OOP

    Class and Object

    Method

    Encapsulation Inheritance

    Polmorphism

    Outline

    !ina "usantara

    #

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    4/25

    Introduction to OOP

    Object Oriented Programming $OOP% is a techni&ue tocreate object'oriented programs(

    )n object described b entities in the real *orld thatcan be clearl identi+ed( E,ample- students. cars.

    des/s and other things that can be 0ie*ed as objects( Objects in programming languages must ha0e the

    beha0ior method. propert. tpe. and identit(

    )ll data and 2unctions *rapped in classes and objects(

    OOP pro0ides the 2acilit to *rite a program to change

    the actual object in the component programming code(

    !ina "usantara

    3

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    5/25

    !ina "usantara

    )d0antage o2 OOP

    )n object'oriented languages such as 4a0acombine language s/ills in the procedure badding a dimension that pro0ides- more

    5e,ible. clear. easier to use bac/ throughabstraction. encapsulation. inheritance. andpolmorphism(

    Other ad0antage o2 OOP -

    )llo*s the use o2 real *orld modeling upport the reuse o2 e,isting code

    Pro0ide 5e,ibilit in modi2ing e,isting applications

    )ssist *ith ease o2 maintenance o2 e,isting code

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    6/25

    e+ning Classes or Objects

    )n object represent an entit in the real *orld

    )n object has the properties and beha0ior- Properties- something that characteri9e the object

    !eha0ior- something that as/ the object to per2orm anaction

    Objects o2 the same tpe are de+ne using acommon class

    )n object is an instance o2 a class

    !ina "usantara

    6

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    7/25

    Class and Object E,amples

    Class - Collection o2 data de+nitions and methodsin the unit 2or a particular purpose( ) template is ablueprint that de+nes *hat data objects andmethods(

    Object- a representation o2 Class Class E,ample-

    a man. *ho has a name. age. height. *eight. and color andhair tpe $data objects nature% and also to eat. *al/. pla.and sleep $beha0ior method%(

    Object E,ample- !ernard is 1: ears old 17;cm tall.*eight 7; /g. blac/ curl hair. an acti0it as eating.sleeping. plaing and roads(

    !ina "usantara

    7

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    8/25

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    9/25

    E,ample Class and ObjectclassPupp?

    publicPupp $Stringname%?

    ni0ersit

    :

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    10/25

    Constructor

    ) class contains constructors that are in0o/ed tocreate objects 2rom the class blueprint(

    Constructor declarations loo/ li/e method

    declarations e,cept that the use the name o2 theclass and ha0e no return tpe(

    !ina "usantara >ni0ersit

    1;

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    11/25

    E,ample Constructor or e,ample. !iccle has one constructor-

    public class !iccle?

    private int gearB

    private int cadenceB

    privateintspeedB

    public!iccle $intstartCadence. intstartpeed. intstartHear% ?gear F startHearB

    cadence F startCadenceB

    speed F startpeedB

    publicstaticvoidmain$StringD args%?

    Bicycleobj F ne* Bicycle(. 1;. 1%B calling constructor

    !ina "usantara >ni0ersit

    11

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    12/25

    estructor

    !ecause 4a0a is a garbage collected language oucannot predict *hen $or e0en i2% an object *ill bedestroed( ence there is no direct e&ui0alent o2a destructor(

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    13/25

    Methods

    )bilit o2 objects

    )ll *or/ processes and beha0ior o2 an objectrepresenteddescribed b this method(

    E,ample- People can tal/as/. *al/. eat. sleepand rest(

    !ina "usantara

    1#

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    14/25

    E,ample Method!eha0iorpublicclassMotorccle?

    voidstartEngine$% ?

    i!$enginetate FF true% stem(out(println$@

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    15/25

    Method )bstraction and Encapsulation

    Method abstraction is achie0ed b separating theuse o2 method 2rom its implementation(

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    16/25

    E,ample o2 EncapsulationpublicclassEncapsulation?

    privateStringnameB

    privateintageB

    publicintget)ge$%?

    returnageB

    publicStringget"ame$%?

    returnnameB

    publicvoidset)ge$ intne*)ge%?

    age F ne*)geB

    publicvoidset"ame$Stringne*"ame%?

    name F ne*"ameB

    !ina "usantara >ni0ersit

    16

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    17/25

    Inheritance

    Object oriented programming allo*s ou to deri0ene* classes 2rom e,isting classes inheritance(

    Can be used to a0oid redundanc. ma/e the sstemeas to comprehend. and eas to maintain(

    Can use a class to model objects o2 the same tpe(

    Inheritance enable ou to de+ne a general class andlater e,tend it to more speciali9ed classes(

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    18/25

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    19/25

    E,ample o2 Inheritance

    publicclassAnimal{

    }

    publicclassMammal extendsAnimal{

    }

    publicclassReptile extendsAnimal{

    }

    publicclassDog extendsMammal{

    }

    !ina "usantara >ni0ersit

    1:

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    20/25

    Lisibilit modi+er O2ten /no*n b 0isibilit or accessibilit modi+er

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    21/25

    Modi+ers

    !ina "usantara >ni0ersit

    J1

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    22/25

    Polmorphism One method has man implementations beha0ior

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    23/25

    !ina "usantara

    E,ample o2 Polmorphism

    public interface Vegetarian{}

    public class Animal{}

    public class Deer extends Animal implements

    Vegetarian{}

    Polmorphism in ja0a using /e*ord inter2ace toma/e beha0ior 2or implement in class

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    24/25

    !ina "usantara

    E,ercise

    1( escribe and gi0e e,ample using class andobject

    J( Nhat are the dierences bet*een constructors

    and methods#( E,plain the dierence bet*een method

    o0erloading and method o0erriding Hi0ee,ample 2or method o0erloading and methodo0erriding

    3( Nhat is /e*ord superand t#isin ja0a Canou gi0e e,ample

    K( oes ja0a can use multiple inheritance Nh

  • 8/9/2019 Z00040000120144014COMP6178 Session13_Introduction to OOP Concept

    25/25

    !ina "usantara

    e2erences

    aniel Qiang. R(. J;11. Introduction to javaprogramming. 0ol(;8. Pearson Education."e* 4erse( Chapter 7. :. 1;(

    http-ja0a(sun(comdocsboo/stutorialja0aconceptsinde,(html

    http://java.sun.com/docs/books/tutorial/java/concepts/index.htmlhttp://java.sun.com/docs/books/tutorial/java/concepts/index.htmlhttp://java.sun.com/docs/books/tutorial/java/concepts/index.htmlhttp://java.sun.com/docs/books/tutorial/java/concepts/index.html