GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • Java Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — Interfaces in Java - SyntaxDB - Java Syntax Reference

    Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Print
      • User Input
      • Commenting
      • Read from Text File
      • Write to File
      • Import Package
    • Variables
      • Primitive Data Types
      • Type Casting
      • Variable Declaration
      • Constants
      • Arrays
      • HashMaps
      • HashSets
    • Control Flow
      • If Statement
      • Switch Case
      • Ternary Operator
      • For Loop
      • For Each
      • While Loop
      • Do-While Loop
      • Try Catch Block
    • Object Oriented Programming
      • Class Declaration
      • Access Modifiers
      • Constructor
      • Instantiation
      • Class Variables
      • Standard Methods
      • Class Methods
      • Inheritance
      • Interfaces
      • Abstract Methods and Classes
      • Anonymous Class
      • Declaring an Exception
      • Throwing an Exception
    • String Class
      • String Variables
      • String Length
      • String Compare
      • String Trim
      • Split String
      • Substring
      • String Representation
Categories
Language
Java
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: SE 8

Categories

  • User Program Communication
    • Print
    • User Input
    • Commenting
    • Read from Text File
    • Write to File
    • Import Package
  • Variables
    • Primitive Data Types
    • Type Casting
    • Variable Declaration
    • Constants
    • Arrays
    • HashMaps
    • HashSets
  • Control Flow
    • If Statement
    • Switch Case
    • Ternary Operator
    • For Loop
    • For Each
    • While Loop
    • Do-While Loop
    • Try Catch Block
  • Object Oriented Programming
    • Class Declaration
    • Access Modifiers
    • Constructor
    • Instantiation
    • Class Variables
    • Standard Methods
    • Class Methods
    • Inheritance
    • Interfaces
    • Abstract Methods and Classes
    • Anonymous Class
    • Declaring an Exception
    • Throwing an Exception
  • String Class
    • String Variables
    • String Length
    • String Compare
    • String Trim
    • Split String
    • Substring
    • String Representation

Object Oriented Programming

Interfaces in Java

An interface provides a framework for a class. It gives method signatures and constants to the class to be implemented. A class that uses an interface implements the interface.


Syntax
modifier interface interfaceName {
    //constant declarations    
    //method signatures
}

modifer class className implements interfaceName {
   //methods from the interface
}; 

Notes

An interface only contains method signatures and constants, which means it contains no implementation code. All method signatures must be given implementation in any class that implements the interface,


Example
public interface Country {
    //all classes that implement Country must have the method getCapital()
    public String getCapital(); 
} 
 
public class NACountry implements Country {
    String capitalCity;    
    public NACountry(String capitalCity) {
        this.capitalCity = capitalCity;        
    }    

    public String getCapital(){ //implemented method signature from the interface
        return capitalCity;
    }
} 

See Also
Related

Abstract Methods and Classes

Documentation

What is an Interface? - Java Tutorial

Add to Slack

< Inheritance   |   Abstract Methods and Classes >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.