<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">An interface is a collection of methods that have no implementation &#8211; they are just created, but have no functionality. An interface is a bit like a class, except you can only declare methods and variables in the interface. You cannot actually implement the methods.</p>
<p> An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.</p>
<p> Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.</p>
<p><b>Declaring an interface:</b><br />
While Java provides interfaces for you to use, you can also create your own. <br />
An interface is declared with the <i><b>interface </b></i>keyword. </p>
<p> <b>Syntax:</b></p>
<pre class="highlight" name="code">interface nameOfInterface{
//methods for interface here;
}
</pre>
<div align='center' id="ads-id"></div>
<p>To use a interface in your class , append the keyword “implementsā after your class name followed by the interface name</p>
<pre class="highlight" name="code">public class JavaInterfaceExample implements IntExample
</pre>
<p>
<b>Java Interface Example :</b></p>
<pre class="highlight" name="code">interface IntExample{

public void sayHello();
}
}

public class JavaInterfaceExample implements IntExample{
public void sayHello(){
System.out.println("Hello Visitor !");
}
public static void main(String args[]){

JavaInterfaceExample javaInterfaceExample = new JavaInterfaceExample();
javaInterfaceExample.sayHello();
}
}
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
Hello Visitor !</div>
<p>
 <b>Points to note:</b></p>
<ul class="list-4">
<li>The class which implements the interface needs to provide functionality for the methods declared in the interface</li>
<li>All methods in an interface are implicitly public and abstract</li>
<li>An interface cannot be instantiated</li>
<li>An interface reference can point to objects of its implementing classes</li>
<li>An interface can extend from one or many interfaces. A class can extend only one class but implement any number of interfaces</li>
</ul>
</div>
<p>
<b>Example:</b></p>
<pre class="highlight" name="code">interface RidableAnimal extends Animal, Vehicle
</pre>
<p>
<b>Class vs Interface:</b><br />
<b>An interface is similar to a class in the following ways:</b></p>
<ul class="list">
<li>An interface can contain any number of methods.</li>
<li>An interface is written in a file with a <b>.java</b> extension, with the name of the interface matching the name of the file.</li>
<li>The bytecode of an interface appears in a <b>.class</b> file.</li>
<li>Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.</li>
</ul>
<p> <b>However, an interface is different from a class in several ways, including:</b></p>
<ul class="list">
<li>You cannot instantiate an interface. </li>
<li>An interface does not contain any constructors.</li>
<li>All of the methods in an interface are abstract.</li>
<li>An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.</li>
<li>An interface is not extended by a class; it is implemented by a class.</li>
<li>An interface can extend multiple interfaces.</li>
</ul>
</div>
<p>
<b>Rules for Implementing Interfaces:</b><br />
<b>When overriding methods defined in interfaces there are several rules to be followed:</b></p>
<ul class="list">
<li>Checked exceptions should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method.</li>
<li>The signature of the interface method and the same return type or subtype should be maintained when overriding the methods.</li>
<li>An implementation class itself can be abstract and if so interface methods need not be implemented.</li>
</ul>
<p> <b>When implementation interfaces there are several rules:</b></p>
<ul class="list">
<li>A class can implement more than one interface at a time.</li>
<li>A class can extend only one class, but implement many interface. </li>
<li>An interface can extend another interface, similarly to the way that a class can extend another class.</li>
</ul>
</div>
<p>
<b>Extending Interfaces:</b></p>
<p> An interface can extend another interface, similarly to the way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.</p></div>
<p></p>
<pre class="highlight" name="code">//Filename: Sports.java
public interface Sports
{
 public void setHomeTeam(String name);
 public void setVisitingTeam(String name);
}

//Filename: Football.java
public interface Football extends Sports
{
 public void homeTeamScored(int points);
 public void visitingTeamScored(int points);
 public void endOfQuarter(int quarter);
}

//Filename: Hockey.java
public interface Hockey extends Sports
{
 public void homeGoalScored();
 public void visitingGoalScored();
 public void endOfPeriod(int period);
 public void overtimePeriod(int ot);
}
</pre>
<p>
The Hockey interface has four methods, but it inherits two from Sports; thus, a class that implements Hockey needs to implement all six methods. Similarly, a class that implements Football needs to define the three methods from Football and the two methods from Sports.<br />
<b><br />
</b> <b>Extending Multiple Interfaces:</b></p>
<p> A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.</p>
<p> The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.</p>
<p> For example, if the Hockey interface extended both Sports and Event, it would be declared as:</p>
<pre class="highlight" name="code">public interface Hockey extends Sports, Event
</pre>
<p>
<b>Tagging Interfaces:</b></p>
<p> The most common use of extending interfaces occurs when the parent interface does not contain any methods. For example, the <i><b>MouseListener </b></i>interface in the <i><b>java.awt.event </b></i>package extended <i><b>java.util.EventListener</b></i>, which is defined as:</p>
<pre class="highlight" name="code">package java.util;
public interface EventListener
{}
</pre>
<p>
An interface with no methods in it is referred to as a tagging interface. There are two basic design purposes of tagging interfaces:</div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/2013/04/encapsulation-in-java.html">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html">Index </a>||  ; >;>;<a href="https://dineshonjava.com/2013/04/packages-in-java.html">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/encapsulation-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/packages-in-java/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '511'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…