Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Java interface how to  (Read 1054 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: February 01, 2009, 10:21:56 AM »

Java interfaces are abstract classes that can be used in other classes. Classes that implements an interfaces must implement all the methods that are defined in the interface class.

Example
I wrote an interface named: ComputerInterface , this class is public and has 2 empty methods called "startUp" and "shutDown"

Code
GeSHi (java):
  1. public interface ComputerInterface {
  2. void startUp();
  3. void shutDown();
  4. }
  5.  
Created by GeSHI 1.0.7.20

If we want to implement the interface in a class we need to write another class that does that, so here we go. As you can see below this is a public class that implements the ComputerInterface. This means that the ComputerInterfaceDemo class must have atleast the methods that are defined in the ComputerInterface class as you can see below or the class will complain that you  haven't implemented the methods.

Code
GeSHi (java):
  1. public class ComputerInterfaceDemo implements ComputerInterface {
  2. public void startUp() {
  3. System.out.println("Start up");
  4. }
  5.  
  6. public void shutDown() {
  7. System.out.println("Shut down");
  8. }
  9. }
  10.  
Created by GeSHI 1.0.7.20


« Last Edit: February 08, 2009, 11:04:09 AM by HappyFace » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here