Archive for the ‘OO Concepts’ Category
Java Interfaces
Interfaces expose the public methods of a class to the outside world. A class that implements an interface must implement all the methods in that interface (unless it is an abstract class). Here is an example:
//Dog
public class Dog implements Speaker
{
public void speak()
{
System.out.println(“woof”);
}
public void announce (String announcement)
{
System.out.println (“woof: “+ announcement);
}
}
//*****
//Pilosopher*****
public class Philosopher implements Speaker
{
private String philosophy;
public Philosopher (String thoughts)
{
philosophy = thoughts;
}
public void speak ()
{
System.out.println (philosophy);
}
public void announce (String announcement)
{
System.out.println (announcement);
}
public void pontificate ()
{
for (int count=1; count <= 5; count++)
System.out.println (philosophy);
}
}
//*****
//Speaker
public interface Speaker
{
public void speak();
public void announce(String str);
}
//Talking


