Learning Java
Custom Search

Previous Next

Interfaces

An interface in Java is a special entity that contains abstract methods, methods without actual code. When we define a class, we use the keyword implements to connect the interface to it. A class might implement an interface to give itself additional behavior. Once you implement an interface, you have to implement all of the methods in that interface.

We will not cover creating interfaces in this tutorial. We will be making use of interfaces provided by Java. However, for illustration, the following example of an interface is shown. We create an interface, Callable, which can be used by a CellPhone class or a Laptop class. Note that both of these classes that implement Callable also use all of the methods within Callable.

interface Callable {
    String transmission = "stuff";
    boolean LineEstablished = true;

    public void MakeCall(); 

    public void ReceiveCall(); 
}
 
class CellPhone implements Callable {

    public void MakeCall() {
	// code to call from CellPhone
    }

    public void ReceiveCall() {
	// code to receive call on CellPhone
    }
}

class Laptop implements Callable {

    public void MakeCall() {
	// code to call from Laptop
    }

    public void ReceiveCall() {
	// code to receive call on Laptop
    }
}

public class CallTest {
    public static void main(String args[]) {
	CellPhone MyPhone = new CellPhone();
	Laptop MyLaptop = new Laptop();
    }
}

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved