• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Interface

October 13, 2011 By j2eereference Leave a Comment

What is an Interface?

An Interface is like a Contract. It defines what a class can do when it implements the interface but without knowing how to do it. Interface is of type abstract, 100% pure abstract class. Any class can implement interface, from any inheritance tree.  A class must provide implementation for all the methods from the declared interface. An Interface declared using keyword interface.

Declaring an Interface

All interfaces are implicitly abstract, while declaring interface need not required to declare abstract. The public modifier is required if you want interface to have public access rather than default access.

public abstract interface Pet {
//interface methods and constants
}

Declaring Constants in an Interface

Any class implementing the interface will have access to the same constant defined in the intefrace ;  just like inheriting from interface. Implicitly constants declared in interface are must be public, static and final.

Let’s see with an example, below code will give compiler error, you cannot change a value of constant.

1
2
3
4
5
6
7
8
9
10
public interface Pet {
int x=8;
void doSome();
}
 
public class Cat implements Pet {
public void doSome() {
   int x=4;//Compiler error.
   }
}

Below are the some of the points needs to remember about Interface.

  1. Interface methods are implicitly public and abstract. If you don’t type any modifiers, by default it will be public and abstract.
  2. Interface declares only constant variables; all variables are implicitly public, static and final. Interface cannot have instance variables.
  3. Interface methods cannot have final, static or strictfp modifiers as its methods are abstract implicitly.
  4. An interface can extend one or more interfaces. And a class can implement one or more interfaces.
  5. If class implements an interface and in turn that interface extends another interface, then class must provide implementation for all the methods of both the interface.
  6.  Class must follow method override rules when it provides implementation for interface method.

Let see an example, where an abstract class Animal has two subclass Cat and Dog, both subclass implements Pet interface

//Pet Interface

1
2
3
4
5
6
7
8
9
<strong>public</strong> <strong>interface</strong> Pet
 
{ 
 
<strong>int</strong> <em>i</em>=10;
 
<strong>public</strong> <strong>abstract</strong> <strong>void</strong> makeNoise();
 
}

//Animal class

1
2
3
4
5
6
7
8
9
10
<strong>public</strong> <strong>abstract</strong> <strong>class</strong> Animal {
<strong>public</strong> Animal (){
System.<em>out</em>.println("In Animal constructor");
}
<strong>private</strong> <strong>int</strong> age;
<strong>public</strong> <strong>abstract</strong> <strong>void</strong> eat();
<strong>public</strong> <strong>int</strong> getAge() {
<strong>return</strong> age;
}
}

//Dog class

1
2
3
4
5
6
7
8
9
10
11
12
 
<strong>public class</strong> Dog <strong>extends</strong> Animal <strong>implements </strong>Pet{
   <strong>public</strong> Dog() {
   System.<em>out</em>.println("In Dogs Constructor");
   }
   <strong>public</strong> <strong>void</strong> eat(){
   System.<em>out</em>.println("Dog is eating Bread");
   }
   <strong>public</strong> <strong>void</strong> makeNoise(){
   System.<em>out</em>.println("bow bow");
   }
}

//Cat Class

1
2
3
4
5
6
7
8
9
10
11
12
13
 
 
<strong>public class</strong> Cat <strong>extends</strong> Animal <strong>implements</strong>Pet {
   <strong>public</strong> Cat(){
    System.<em>out</em>.println("In cats Constructor");
    }
    <strong>public</strong> <strong>void</strong> eat(){
    System.<em>out</em>.println("Cat is eating fish");
    }
    <strong>public</strong> <strong>void</strong> makeNoise(){
    System.<em>out</em>.println("Meow meow");
   }
}

//Test Class

1
2
3
4
<strong>public</strong> <strong>class</strong> TestAnimal {
<strong>public</strong> <strong>static</strong> <strong>void</strong> main(String [] args){
Pet cat = <strong>new</strong> Cat();
cat.makeNoise();

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.