September 03, 2010, 10:31:47 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Java interface how to  (Read 1135 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
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
Javaforums.net/forum :: A friendly community for software developers and students that want to learn Java!
« on: February 01, 2009, 10:21:56 AM »

 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.202 seconds with 32 queries.

Google visited last this page Today at 08:05:03 AM