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.
- Interface methods are implicitly public and abstract. If you don’t type any modifiers, by default it will be public and abstract.
- Interface declares only constant variables; all variables are implicitly public, static and final. Interface cannot have instance variables.
- Interface methods cannot have final, static or strictfp modifiers as its methods are abstract implicitly.
- An interface can extend one or more interfaces. And a class can implement one or more interfaces.
- 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.
- 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(); |
Leave a Reply