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.
Explain the various form of implementing interface
By implementing Runnable in our class and by overriding the run() method of Runnable interface
Because an interface is like a contract/skeleton which decides what the implementing class has to do. So, if any entity in an interface is protected, they would not be available to the class that is implementing the interface. Hence, all the variables and methods declared inside an interface are public by default
Yes, by implementing SingleThreadModel interface
Yes, Interface mehtods are public by default so that they could be implemented by every class implementing the interfaces.
If a class has one abstract method ,the class has to be an abstract class.Methods can be implemented in abstract class.Whereas a interface is like a abstract class...the only difference being that the methods are never implemented in Interface.
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)
Implementation in java mean to accomplish some aim or to execute some order in java software platform. Your class can implement more than one interface, so the implements keyword is followed by a comma with separated list of the interfaces implemented by the class. By convention, the implements clause follows the extends clause, if there is one.
Interfaces contain only the signatures or blueprint of methods, events or delegates and not the implementation. They're implemented in the class implementing the interface. They're used to specify a behavior common to all classes that implement them. The intricate details of how this behavor would be achieved is upto the discretion of the classes implementing them. Any class using them is bound to implement evrything specified in the interface. Hence one can be rest assured of this. They're good for data-hiding.
No. The Servlet is not thread-safe by default. You can make it thread safe by implementing the SingleThreadedModel interface
You are talking about the implementation point view of Abstract class and the interface. Let's go. 1. Interface helps Multiple inheritance:- In java you can't have a class inherited from more than one class, i.e. the multiple inheritance. Interface helps us in implementing the multiple inheritance(virtually), because their is no such restriction in implementing more than one interface. But the Abstract class can't help you doing so. 2. Interface helps global declaration:- In C/C++, a header file holds the declaration of the functions and the symbolic constants. Similarly we can have a interface which is having only the symbolic constants which is same for all classes which implements it. And also the method have only the signature and the class which implements it is free to write the body at his own wish. Abstract class can't help in doing this. 3. Abstract class is more secure:- Because in Interface we have all the things public. But in Abstract class we can have private and also friendly members. 4. Interface is overhead:- Because in Interface we are bound to implement all the methods though some of them we actually need. But in case of the Abstract class we can override the methods which we want.
Power Naps