1 pendahuluan 3-

Upload: nazilatul-mahbubah

Post on 02-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 1 Pendahuluan 3-

    1/22

    Pemrograman Berorientasi Obyek

    (OOP : Object Oriented Programming)

    JAVA

    http://java.sun.com/
  • 8/10/2019 1 Pendahuluan 3-

    2/22

    Object Orientation involving encapsulation,

    inheritance, polymorphism, and abstraction, is animportant approach in programming and program

    design. It is widely accepted and used in industry and

    is growing in popularity in the first and second

    college-level programming courses.

  • 8/10/2019 1 Pendahuluan 3-

    3/22

    ClassName

    - attribute_declaration

    + constructor_declaration+ method_declaration

    Visibility shown as :

    + public

    private

    # protected

    UML diagram

  • 8/10/2019 1 Pendahuluan 3-

    4/22

    The Solution

    1. public class MyDate {

    2. private int day;

    3. private int month;

    4. private int year;

    5. public int getDay() {

    6. return day;

    7. }

    8. public int getMonth() {

    9. return month;

    10. }11. public int getYear() {

    12. return year;

    13. }

    14. public void setDay(int d) {

    15. day = d;

    16. }17. public void setMonth(int m) {

    18. month = m;

    19. }

    20. public void setDay(int y) {

    21. year = y;

    22. }23.}

  • 8/10/2019 1 Pendahuluan 3-

    5/22

    Some other reasons to move on

    to Java:

    Platform-independent software Relatively easy graphics and GUI

    programming

    Lots of library packages

    Free compiler and IDEs

  • 8/10/2019 1 Pendahuluan 3-

    6/22

    Java Programming Styles(1)

    Packages:

    Package names are entirely in lower case.

    Package name should start with the web domainname reversed

    Examples:package com.sun.java.lang;

    package edu.nmsu.is.us.sp;

    Files:

    The file name must have the same base name as thename of the public class defined in the file.

    Example: If you have a public class named RecordList, the file

    containing this class should be named RecordList.java

  • 8/10/2019 1 Pendahuluan 3-

    7/22

    Java Programming Styles(2)

    Classes and Interfaces:

    Use meaningful identifiers for classes ,and interfaces.

    Capitalize each word contained in aclass identifier name.

    No underscores.

    Examples:public class RecordList {}

    public interface PanelFace {}

  • 8/10/2019 1 Pendahuluan 3-

    8/22

    Java Programming Styles(3)

    Variables:

    Use meaningful identifiers for variables.

    Capitalize each word contained in a name of avariable except the first word.

    Use nouns to identify variables as possible.

    For boolean variables, use identifirs that are likequestions.

    Use all-caps indentifiers for constants.

    Examples:int number;

    String myName;

    boolean isValid;

    final int CODE = 707;

  • 8/10/2019 1 Pendahuluan 3-

    9/22

    Java Programming Styles(4)

    Methods:

    Use meaningful identifiers for methods.

    Capitalize each word contained in a name of amethod except the first word.

    Use verbs to identify methods as possible.

    For the methods dealing with objects properties,start the method identifier with get or set.

    If the method returns boolean use is or are

    instead of get to name this method. Examples:

    private boolean paint()

    boolean isObjectValid()

    Font getFont()

    void setFont(Font f)

  • 8/10/2019 1 Pendahuluan 3-

    10/22

    Java Programming Styles(5)

    General Considerations: Use three-space indentation style. Example

    if(num < 10)

    {

    System.out.println(Not Enough);}

    Use comments to mark the beginning and the end of blocks

    Use three-line style to comment your code. Use either one of:

    // /*

    // This part is to or * This part is to

    // */

    Use empty lines to increase the readability of your code

    Use spaces between the operators such as +, -, =, and theoperands. Example:

    c = a + b;

  • 8/10/2019 1 Pendahuluan 3-

    11/22

    Some other reasons to move on

    to Java: Colleges are teaching it

    Companies are using it

    Students want it

    (Teachers welcome it... ;)

    (Programmers drink it... :)

  • 8/10/2019 1 Pendahuluan 3-

    12/22

    What are OOPs claims to fame?

    Better suited for team development

    Facilitates utilizing and creating reusable

    softwarecomponents

    Easier GUI programming

    Easier program maintenance

  • 8/10/2019 1 Pendahuluan 3-

    13/22

  • 8/10/2019 1 Pendahuluan 3-

    14/22

    OOP

  • 8/10/2019 1 Pendahuluan 3-

    15/22

    Abstraction

    ... relevant to the given project (with an

    eye to future reuse in similar projects).

    Abstraction means ignoring irrelevant

    features, properties, or functions and

    emphasizing the relevant ones...

    Relevant to what?

  • 8/10/2019 1 Pendahuluan 3-

    16/22

    Encapsulation

    Encapsulation means that all data members

    (fields) of a class are declared private. Some

    methods may be private, too.The class interacts with other classes (called

    the clientsof this class) only through the

    classs constructors and public methods.Constructors and public methods of a class

    serve as the interfaceto classs clients.

  • 8/10/2019 1 Pendahuluan 3-

    17/22

    public abstract class Foot{

    private static final int footWidth = 24;

    private boolean amLeft;

    private int myX, myY;

    private int myDir;

    private boolean myWeight;

    // Constructor:

    protected Foot(String side, int x, int y, int dir)

    {

    amLeft = side.equals("left");

    myX = x;

    myY = y;

    myDir = dir;

    myWeight = true;

    }

    Continued

    All fields areprivate

  • 8/10/2019 1 Pendahuluan 3-

    18/22

    Encapsulation ensures that

    structural changes remain local Changes in the code create software

    maintenance problems

    Usually, the structure of a class (as defined

    by its fields) changes more often than the

    classs constructors and methods

    Encapsulation ensures that when fields

    change, no changes are needed in other

    classes (a principle known as locality)

  • 8/10/2019 1 Pendahuluan 3-

    19/22

    Inheritance

    A class can extend another class,

    inheriting all its data members and

    methods while redefining some of themand/or adding its own.

    Inheritance represents the is a

    relationship between data types. Forexample: a FemaleDanceris a

    Dancer.

  • 8/10/2019 1 Pendahuluan 3-

    20/22

    Inheritance Terminology:

    public class FemaleDancer extendsDancer{

    ...

    }

    subclass

    or

    derived class

    superclass

    or

    base class

    extends

  • 8/10/2019 1 Pendahuluan 3-

    21/22

    Polymorphism

    Polymorphism ensures that the

    appropriate method is called for an

    object of a specific type when the objectis disguised as a more general type.

    Good news: polymorphism is already

    supported in Javaall you have to dois use it properly.

  • 8/10/2019 1 Pendahuluan 3-

    22/22

    Polymorphism (contd)

    Situation 1:

    A collection (array, list, etc.) contains

    objects of different but related types, all

    derived from the same common base

    class.