answersLogoWhite

0


Best Answer

An interface is a collection of methods that must be implemented by the implementing class.

An interface defines a contract regarding what a class must do, without saying anything about how the class will do it.

Interface can contain declaration of methods and variables.

implementing class must define all the methods declared in the interface

If a class implements an interface and does not implement all the methods then class itself must be declared as abstract

Variables in interface automatically become static and final variableof the implementing class

Members of interface are implicitly public, so need not be declared as public.

An interface must be implemented in class.

User Avatar

Wiki User

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

Wiki User

12y ago

An Interface is nothing but a contract as to how a class should behave. It just declares the behavior as empty methods and the implementing class actually writes the code that will determine the behavior.

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.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

interfaces are the collection of method declarations in JAVA.

They provide a means to provide multiple inheritance in JAVA since it dosenot support it directly.

a class can 'implement' any number of interfaces but should privide definetions of methods.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

An interface in java is used to declare that your class will implement all the functions within the defined interface. So if I have an interface called interfaceA and a class called classB, which implements interfaceA, then you are ensuring that classB will have all the functions declared in interfaceA. You do not actually code how the functions operate in the interface declaration, instead you define the the code inside of the class that implements the interface. A quick example:

interface Noisy

{

void makeNoise();

}

class Cow implements Driveable

{

void makeNoise(){ System.out.println("Moo");}

}

class Dog implements Noisy

{

void makeNoise(){ System.out.println("Bark");}

}

As you can see, both Cow and Dog implement the interface Noisy. This means that both Dog and Cow must have the makeNoise() function included in their code. Even though the code does not operate exactly the same for each class, you have guaranteed that each class will contain that function. Thus later on if someone has an instance of a Cow or Dog object they know that they can call the makeNoise() function no matter which one it is.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Some predefined interfaces are

1. Runnable - An interface used to create Threads

2. Serializable - An interface used for Serialization

3. Cloneable - An interface used for cloning etc...

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

An Interface is nothing but a contract as to how a class should behave. It just declares the behavior as empty methods and the implementing class actually writes the code that will determine the behavior.

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.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a predefined interface in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can you create a constructor for an interface in java?

NO, we cannot create a contructor for an interface in java.


Can you define variables in interface in java?

yes we can define a variable in an interface in java.


Is main a predefined function in java?

No. It is a user defined function which the person who is creating the java class has to code by himself.


What is intrface in java?

interface is a list of methods which implements that interface


What is a tagging interface type?

A tagging interface type in Java is an interface that has not defined methods such as the java.io.Serializable interface.


What java interface must be implemented by all threads?

Runnable interface


Can you create an empty interface with no definitions?

Yes. This is a valid interface definition in Java: interface Useless {}


What do mean by interface in java?

An interface in Java is like an abstract class, but there are no method bodies allowed in it and it has to be declared with the interface keyword. It is Java's way of getting around the Deadly Diamond of Death. Only abstract methods and constants are allowed in it.


What would interface java be used for?

Interface Java can be used for a variety of tasks and commands such as .swf files, Java files, running scripts, as well as website video players for websites.


What is interface in java?

Interface is collection of abstract methods which has only declaration and no implementation


What are the different types of package in java?

1.user defined packages 2.predefined packages


Can you create an object of interface in java?

maybe