What kind of methods can be implemented in an interface?
Overview+How would you implement a single class that could maintain a sorted list of ints, Strings, or any other object? Show Interfaces
Essentially, the interface defines the behavior a class must support, and many classes may implement the same interface (that is, support the same function in different ways). You might consider an interface Drivable that defined an abstract method drive. A class Car might implement Drivable, as might a class Bicycle. Defining an Interface public interface Viewable { public void display(); } An interface (as shown above) looks very similar to a class.
Implementing an Interface To implement an interface, a class must specify that implements that interface and it must also provide implementations for all of the methods defined in the interface. If a class fails to implement one or more abstract methods from the interface, the compiler will complain. public class Name implements Viewable { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public void setFirst(String first) { this.first = first; } public void display() { System.out.println("First name: " + first); System.out.println("Last name: " + last); } } A class may also implement multiple interfaces. public class C implements X, Y { //implementations for all methods in X and Y } Using Classes that Implement Interfaces Because an interface does not provide implementation for its methods, it cannot be instantiated. The following piece of code would result a compiler error: Viewable v = new Viewable(); However, the following code is perfectly valid: Viewable v = new Name("Jane", "Wu"); v.display(); We describe Name as having an "is-a" relationship with Viewable, as in "a Name is a Viewable". Therefore, a variable that can refer to a Viewable can refer to a Name or an object of any other class that implements Viewable. Similarly, on a Viewable variable, you can invoke any method defined in Viewable. Casting Now, suppose you wanted to invoke the setFirst method on the variable v defined above. You might try Viewable v = new Name("Jane", "Wu"); //valid v.display(); //valid v.setFirst("Bob"); //invalid v = new Photo(); //valid v.display(); //valid v.setFirst("Julie"); //invalid -- doesn't make sense! In order to invoke the method setFirst on the variable v, we must use casting. Recall that casting tells the compiler to treat one type of variable as another type. Viewable v = new Name("Jane", "Wu"); v.display(); Name n = (Name)v; n.setFirst("Bob"); n.display(); //displays Bob Wu v.display(); //also displays Bob Wu ComparableAs a more concrete example, lets consider the Comparable interface. java.lang provides a Comparable interface with one method, compareTo, described as follows:
You'll notice that this is the method we use to compare Strings because the String class actually implements the Comparable interface. +Revisiting our sorted list example, how would we use the Comparable interface to help us? Sami Rollins Date: 2007-09-19 Can methods be implemented in an interface?Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).
Which method must be implemented when using an interface?Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class. Implement every method defined by the interface.
What methods are allowed in interface Java?From Java8 onwards interfaces allow default methods and static methods. Static methods − A static method is declared using the static keyword and it will be loaded into the memory along with the class. You can access static methods using class name without instantiation.
How many methods can an interface have?Interface Methods
A Java interface can contain one or more method declarations.
|