Abstract & Polymorphism

Recap

Encapsulation is the inclusion of both data and actions into a single component. The technique is to make fields private and provide public method to access the fields. Three benefits are: maintenance, flexibility, extensibility.

Inheritance ("is a") refers to the subclass inherit all the public properties of the superclass by using the keyword "extends". Four benefits are: reusability, extensibility, information hiding, and overriding.

Polymorphism

Subclasses inherit same functionalities from superclass yet define their own unique behaviors. Two major advantages are: overriding and reusability.
Sign: Hollow Arrow

Association "has a"

A1 uses and contains one instance of class B1 but B1 does not know the existence of A1. A1 has a B1.

Dependency "uses"

A uses B's methods. A uses B.

Abstraction

An abstract class is a class that is not fully implemented. An abstract class cannot initiate objects and usually contains at least one abstract method.

Why do we use abstract class / method?

An abstract super class defines the framework for subclass but provide subclasses opportunities to create their own unique functionalities under the predetermined framework.
Superclass:

public abstract void draw (Graphics g);

Subclass

public void draw( Graphics g )
   {
     int startY = getY( );
     int startX = getX( );
     g.setColor( Color.LIGHT_GRAY );
     g.fillOval( startX - 37, startY + 8,  12, 12 ) ;
   }

Programming Activity 2

The program is about the race between hares and tortoises which are all inherited from abstract class Racer.

Code & Explantion

while ( input != 's' && input != 'S' )
     {
       Scanner in = new Scanner(System.in);
       input = in.next().charAt(0);
       switch(input){
           case 'T':
           case 't': 
                racerList.add(new Tortoise("Tortoise", START_LINE, yPos));
                yPos += RACER_SPACE;
                break;
           case 'H':
           case 'h':
                racerList.add(new Hare("Hare", START_LINE, yPos));
                yPos += RACER_SPACE;
                break;
           default: 
                JOptionPane.showMessageDialog( this, "Wrong, enter t or h please" );
        }

It is a switch method. According to the user's input, the program will create a hare or a tortoise object. The creations of objects utilizes polymorphism. Tortoise and hare are both racer so they are stored together in the racerList array.

if ( raceIsOn )
{
    for (Racer animal : racerList){
        animal.move();
        animal.draw(g);
    }
}
else{
     for (Racer animal : racerList){
          animal.draw(g);
    }
}

This section utilizes abstraction. move()and draw(g) are abstract method from abstract class Racer:

public abstract void move( );
public abstract void draw( Graphics g );

Hare and Tortoise have two different implementations of these two abstract methods but we can call these two different implementations in the same way. How amazing!

Output

Real Life Implementation

The most famous vide game among Chinese teenagers, The King Glory, employs polymorphism and abstraction. There are many different heroes with different skills, such as ZJ, YS, LB. However, ZJ, YS, LB all have the skill of beating the enemy but they beat in slightly different ways. ZJ, YS, LB are subclassed of the HERO superclass. HERO superclass is an abstract superclass which contains an abstract method called beat(). ZJ, YS, LB inherit the public methods and fields from HERO super class. They also inherit the abstract method beat() from HERO and create different implementation of beat(). Although they are different implementations, we can call all versions of beat() in the same way.

留下评论