Credits

Monday, December 13, 2010

Interface  becomes a medium for user and software  to interact. Since multiple inheritance is a big NO - NO in Java, this is remedied through the use of interface. Multiple objects can be used that act as a single unit. To create an interface, we employ the keyword implements just as the statement below:

public class interfaceDog extends abstractDog implements interfaceWorking{

The programs below link different programs that make use of both abstract and interface classes.

[+/-] show/hide

/*sample program on abstract classes
* parent class to program files : abstractDog, abstractSnake, abstractCow
called by abstractAnimalRefence class
*/

public abstract class AbstractAnimal {
private String name;



public AbstractAnimal(String nm)
{
name = nm;
}

public abstract void speak();

public String getName()
{
return name;
}
}


/*sample program on abstract classes
called by abstractAnimalRefence class
*/

import javax.swing.*;
public class abstractDog extends AbstractAnimal{


public abstractDog(String nm)
{
super(nm);
}

public void speak(){
JOptionPane.showMessageDialog(null," WOOF! Woof! Woof!");
}

}

public class interfaceDemoWorkingDog{
public static void main(String args[]){
interfaceDog myPixel = new interfaceDog("Gia");
myPixel.work();
}
}

/* works with interface interfaceWorking class
works also with the calling program interfaceDemoWorkingDog*/
import javax.swing.*;

public class interfaceDog extends abstractDog implements interfaceWorking{
public interfaceDog(String nm){
super(nm);
}
public void work()
{
speak();
JOptionPane.showMessageDialog(null," I can herd sheeps.");
}
}

AddThis