Sunteți pe pagina 1din 33

Pemrograman Berorientasi

Objek
Minggu ke-VII
Teknik Informatika - Universitas Ma
Chung - 2010

1
Kuis Kecil

Deskripsikan perbedaan variabel yang
dideklarasikan secara:
 public
 static
 private
 protected

2
3
4
Polymorphism

The concept of polymorphism.

To use overridden methods to effect polymorphism.

To distinguish between abstract and concrete classes.

To declare abstract methods to create abstract
classes.

How polymorphism makes systems extensible and
maintainable.

To determine an object's type at execution time.

To declare and implement interfaces.

5
Introduction

Examples of polymorphism:
 Classes Fish, Frog and Bird represent the three
types of animals
 Each of these classes extends superclass Animal
(which contains a method move).
 Each subclass implements method move.
 Each specific type of Animal responds to a move
message in a unique way, a Fish might swim three
feet, a Frog might jump five feet and a Bird might
fly ten feet.

6
Polymorphism with Java Abstract

Method abstract:
 Method di dalam abstract class tidak
mempunyai implementasi
 Untuk membuat abstract method, tulis saja
deklarasi method tanpa body dan gunakan
keyword abstract 

Kita menggunakan abstract class untuk
mendefinisikan jenis-jenis yang luas dari
behavior yang ada.

7
Polymorphism with Java Abstract
public abstract class LivingThing {
      public void breath(){
            System.out.println("Living Thing breathing...");
      }  

      public void eat(){


            System.out.println("Living Thing eating...");
      }  

      /**
       * abstract method walk
       * Kita ingin method ini di-overrride oleh subclass dari
       * LivingThing
       */
      public abstract void walk();
}

8
Polymorphism with Java Abstract

Ketika sebuah class meng-extends abstract
class LivingThing, diwajibkan meng-
override abstract method walk(), jika tidak,
subclass tersebut juga akan menjadi
abstract class, dan tidak bisa di-instansiasi.

public class Human extends LivingThing { 


public void walk(){
    System.out.println("Human walks...");
   }  
}

9
Polymorphism with Java Abstract

public abstract class Shape
{
    protected int posX;
    protected int posY;
    
    public Shape (int x, int y)
    {
        posX = x;
        posY = y;
    }
    
    public abstract double getArea();
}

10
Polymorphism with Java Abstract
public class Square extends Shape
{
    private int side;
    
    public Square (int x, int y, int s)
    {
        super(x,y);
        side = s;
    }
    
    public double getArea()
    {
        return ( side * side );
    }
}

11
Polymorphism with Java Abstract

public class Circle extends Shape
{
    private int radius;
    
    public Circle (int x, int y, int r)
    {
        super(x,y);
        radius = r;
    }
    
    public double getArea()
    {
        return (Math.PI * radius * radius);
    }
}

12
Polymorphism with Java Abstract

public class DemoPolymorphism
{
    public static void main(String[] args)
    {
        Shape vars;
        vars = new Square(1,2,3);
        System.out.println("Area of square = " +vars.getArea());
        vars = new Circle(1,2,3);
        System.out.println("Area of circle = " +vars.getArea());
    }
}

13
Polymorphism with Java Interface

Interfaces are similar to abstract classes but
all methods are abstract and all properties
are static final.

A Java interface describes a set of methods
that can be called on an object, to tell the
object to perform some task or return some
piece of information.

14

Here's the general form of an interface:

public interface InterfaceName


{
// definitions of methods, e.g.
// void MethodName();
}

15

Here's how a class to implement an
interface :

public class ClassName implements


InterfaceName
{
// class code
// must contain implementation of each
method in InterfaceName
}

16
Polymorphism with Java Interface

public interface GeometricInterface
{
    public abstract double getArea();
}

//Shape.java
public class Shape
{
    protected int posX;
    protected int posY;
    
    public Shape (int x, int y)
    {
        posX = x;
        posY = y;
    }
}

17
Polymorphism with Java Interface

//Square.java
public class Square extends Shape implements GeometricInterface
{
    private int side;
    
    public Square (int x, int y, int s)
    {
        super(x,y);
        side = s;
    }
    
    public double getArea()
    {
        return ( side * side );
    }
}

18
Polymorphism with Java Interface

//Circle.java
public class Circle extends Shape implements GeometricInterface
{
    private int radius;
    
    public Circle (int x, int y, int r)
    {
        super(x,y);
        radius = r;
    }
    
    public double getArea()
    {
      return (Math.PI * radius * radius);
    }
}

19
Polymorphism with Java Interface

//CampusArea.java
public class CampusArea implements GeometricInterface
{
    private double width;
    private double length;
    
    public CampusArea(double width, double length)
    {
        this.width = width;
        this.length = length;
    }
    
    public double getArea()
    {
        return ( width * length );
    }
}

20
Polymorphism with Java Interface

public class DemoPolymorphism
{
    public static void main(String[] args)
    {
        GeometricInterface vars;
        vars = new Square(1,2,3);
        System.out.println("Area of square = " +vars.getArea());
        vars = new Circle(1,2,3);
        System.out.println("Area of circle = " +vars.getArea());
        vars = new CampusArea(3,3);
        System.out.println("Area of campus = " +vars.getArea());
        
    }
}

21
22
23
24
25

The source code indicates that class Invoice implements
interface Payable.

Like all classes, class Invoice also implicitly extends
Object.

Java does not allow subclasses to inherit from more than
one superclass, but it does allow a class to inherit from a
superclass and implement more than one interface.

All objects of a class that implement multiple interfaces
have the is-a relationship with each implemented
interface type.

26
Modifying Class Employee to
Implement Interface Payable

27
28
Method Serang Pada BebekWar V.3.0

29
30
31
Kuis Besar II

Buat case study yang mengimplementasikan
konsep:
 Inheritance
 Abstract
 Interface

Kamis, 1 April 2010.

Score: 30%

Contoh case study: perangkat elektronik, game
strategi, sistem di universitas, dsb

32
Kuis Besar II

Implementasikan case study tersebut dalam
Java secara lengkap.

Senin, 5 April & Kamis, 8 April

Score: 70%

33

S-ar putea să vă placă și