You are on page 1of 33

m  

 
  
    
m


 


V O       

     


 
 
 

 


!
j
V 
 

   
V

 
 

 
   
V
   
!
 

  


V
 
 
 

 
 


V " !
   


# 


  
V
  

$%


# 
 
V
 


 

&
V °#

   

' (
' 

) 
 

 



 

°



#
  * 

+! 
  

 
 ,

°


 
 
°
 


* 
 


 




 -
!(

' 
 
! 

 
(

' 
 
$




) 

 




.
V m 
 
m  @ 
 

 





 
 (
 
$

 
 

 



! 
 
V / 

 



   
$ $ 








0
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();
}


V / 


#
 


1    (
 !$ 

 
 
 
!+,(
$ 
 (


 
$

$

 
(

 
 
   

public class Human extends LivingThing {


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

-
V public abstract class Shape
{
protected int posX;
protected int posY;

public Shape (int x, int y)


{
posX = x;
posY = y;
}

public abstract double getArea();


}


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 );
}
}


V 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);
}
}


V 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());
}
}

!
V Interfaces  
  

 




 
  abstract 

   

  static final
V *
2
 
 



 







$(




$

 
 


 
 

 

  

j
V " %

 



 

public interface InterfaceName


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

&
V " %
 !






 


public class ClassName implements


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

.
V 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;
}
}

0
V //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 );
}
}


V //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);
}
}

-
V //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 );
}
}


V 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());

}
}



!
j
&
V 
  
 
 


 


 
3

V 1 

(

 

  

#
4$

V 2
 
 
 !


 


 


 (


 
 !




 


  


 


 

V *
$





 

 



  
! 



 


.
0

-
!
!
V )



  

 
 
* 
 
V / (
 * 

V 5 
!6
V   


 
  (


  (
 

   (


!
V  


 


2
 

V 5 (
&
* 
7
/ (

* 
V 5 
06

!!