Java Programming: Interface

How can we program behavior in Java? Classes have attributes (e.g. color, number of doors) and exhibit behavior (e.g. eat, sleep, or dive). There is a special group of programming languages that are called functional (e.g. JavaScript, Scala et al). In these languages you can simply write functions like eat, sleep, or dive, and these functions can live their independent lives and don’t have to be declared in classes. In object-oriented languages behavior is implemented in methods defined in classes.

We have a way to implement behaviors in  Java, this way is called interface. We declare the method in an interface, but not realize the method. Another class which is implements the interface will realized the method.  Interface is to separate the method declaration from its implementation.

We sill use Pet as our example. We wanna have the method for Pets such as talk, swim and dive. We use interface to declare the methods, then based on different Pets (Dog/Fish/Cat/Parrot), different classes will implement different method content for the method declared in the interface.

First of all, we create the interface talkative and swimmable to declare methods:

/**
*
*/
package com.java4kids.pets;

/**
* @author yzhu
*
*/
interface Talkative {

public void talk();

}

/**
*
*/
package com.java4kids.pets;

/**
* @author yzhu
*
*/
interface Swimmable {

public void swim(int howFar);
public void dive(int howDeep);

}

Then, we are going to create a Pet, let’s say Fish who has the behaviors as talk/swim and dive. Since we have the interface Talkative and Swimmable, class Fish will implement Talkative and Swimmable. In Fish, we must have to realize all methods declared in Talkative and Swimmable, just because Fish implements Talkative and Swimmable.

/**
*
*/
package com.java4kids.pets;

/**
* @author yzhu
*
*/
public class Fish implements Talkative, Swimmable {

/* (non-Javadoc)
* @see com.java4kids.pets.Swimmable#swim(int)
*/
public void swim(int howFar) {
// TODO Auto-generated method stub
System.out.println(“I swam ” + howFar/2 +”feet”);
}

/* (non-Javadoc)
* @see com.java4kids.pets.Swimmable#dive(int)
*/
public void dive(int howDeep) {
// TODO Auto-generated method stub

System.out.println(“I dive ” + howDeep + “feet”);
}

/* (non-Javadoc)
* @see com.java4kids.pets.Talkative#talk()
*/
public void talk() {
// TODO Auto-generated method stub
System.out.println(“Gurgle! Gurgle! I am a fish!”);

}

}

Just pay attention to how I put math calculation in the program.

The third Step is: create a PetMaster program to master Fish to talk/swim/dive:

/**
*
*/
package com.java4kids.pets;

/**
* @author yzhu
*
*/
public class PetMaster {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Fish myFish = new Fish();
myFish.talk();
myFish.swim(50);
myFish.dive(50);

System.out.println(“————–“);

}

}

Run PetMaster, what can you see from console display?

Gurgle! Gurgle! I am a fish!
I swam 25feet
I dive 50feet
————–

Okay! Here is your turn:

  1. Please create Class Dog, Cat, Parrot, who must implements Talkative and Swimmable
  2. in PetMaster, please insert the codes that can make Dog, Cat, Parrot to talk/swim/dive
  3. check the console

All what you have to do is follow the same manner as Fish!

Good Luck!

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s