answersLogoWhite

0

How do you use an interface?

Updated: 8/10/2023
User Avatar

Wiki User

12y ago

Best Answer

When you implement an interface, you're agreeing to adhere to the contract defined in the interface. That means you're agreeing to provide legal implementations for every method defined in the interface, and that anyone who knows what the interface methods look like can rest assured that they can invoke those methods on an instance of your implementing class. (Thy need not bother much about how you have implemented it. All they bother about is whether a method of the name mentioned in the interface is available or not)

Now, you might stop me and ask, what if I implement an interface and opt not to write code for a method that I am supposed to? The answer is simple. The compiler wouldn't let you do that. You cannot successfully implement an interface without providing method implementation for all the methods declared inside the interface. This is how the java system ensures that when someone knows a certain method name in an interface and has an instance of a class that implements it, can actually call that method without fear that the method isn't implemented inside the class.

Assuming an interface, Convertible, with two methods: openHood(), and setOpenHoodFactor(), the following class will compile:

public class Ball implements Convertible { // Keyword 'implements'

public void openHood() { }

public void setOpenHoodFactor(int bf) { }

}

Ok, I know what you are thinking now. "This has got to be the worst implementation class that you have seen". Though it compiles and runs as well, it is actually doing nothing… the interface contract guarantees that the class implementing it will have a method of a particular name but it never guaranteed a good implementation. In other words, the compiler does not bother whether you have code inside your method or not. All it cares is if you have methods of the matching names as in the interface. That's all…

Implementation classes must adhere to the same rules for method implementation as a class extending an abstract class. In order to be a legal implementation class, a nonabstract implementation class must do the following:

• Provide concrete (nonabstract) implementations for all methods from the declared interface.

• Follow all the rules for legal overrides.

• Declare no checked exceptions on implementation methods other than those declared by the interface method, or subclasses of those declared by the interface method.

• Maintain the signature of the interface method, and maintain the same return type (or a subtype).

• It does not have to declare the exceptions declared in the interface method declaration

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

Interfaces refer to programs or devices that enable a given user to communicate with the computer. It also refers to a point where two organizations and subjects interact.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you use an interface?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can you use protools without a interface?

no you cannot use protools without an interface as a matter of fact you cant use it with just any interface either it must be a protools compatible interface and the software must be compatible with the interface.


Does bluetooth use parallel interface?

No, bluetooth is a serial interface.


Most operating systems today use a text-based user interface?

No, they use picture based interface


How do you use the word interface in a sentence?

oh..god..i just came interface


To use this with an iPhone, you need to add the Tascam iXJ2 interface?

To use this with an iPhone, you need to add the Tascam iXJ2 interface


1What are the hardware requirements to use the Vista Aero user interface?

3D interface


What do PC based control systems use to interface with field devices?

An input interface card


What is it when a user interface can be easily used?

when the window is user friendly then we can easily use the user interface.


What annotation do you use to declare a business interface as a local interface in a stateless session bean?

@Local


Microsoft and mac os use a interface?

GUI (pronounced "gooey") = Graphical User Interface


When a user interface can easily be used?

When an interface is easy to use it is normally referred to as a user friendly interface. A friendly user interface saves time and does not require any special skills to operate.


Can interface extends another interface?

yes of cause interface is use in java at the place of multiple inheritance .o as like multiple inheritance u can extends interface class to other. and another think is interface contain abstract method i.e method with no defination so u can use it without any error.