answersLogoWhite

0


Best Answer

== == By creating an interface, the programmer creates a method to handle objects that implement it in a generic way. An example of this would be the Runnable interface in Java. If the programmer is given a Runnable object, they do not need to know what that object really is to be able to call the run method. Ex.

public void startThreads(ArrayList threads){ for(Runnable r : threads) (new Thread(r)).start();

} The objects in the ArrayList threads may be anything at all, but because they all implement the Runnable interface, it is possible to execute their run method generically, in this case encapsulating it in a Thread object and starting that Thread.

Answer:

In Java, it will not support a concept like "subclass having more than one superclass" ex: class subclass extends superclass1,superclass2 so, to implement this concept , Java introduced a new methodology called "interface", interface is also a class , but it has only function declarations having no function definitions. thereby, we can inherit more than one interfaces through "implements" key word. For an interface we cont declare an object,but, we have an alternative ,through assigning an object of a class which implements that interface. ex: interfacename iobj; subclass sobj=new subclass(); iobj=sobj; iobj.funame1(); iobj.funame2(); iobj.funame3(); note: these three funames are the member functions of the above interface, and subclass has to give the definition for them, dynamic binding...right! Apart from these already three declared functions, we don't give the reference to the iobj, ok! the other explanation for implements is, giving definition for a member function, today or in future ,which is declared in past.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why do you use an interface for implementing methods Isn't it just extra work?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why use interfaces in Java?

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 isnt 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


What are the inbuilt final methods in java?

final methods and variables are that whose values couldn't change.not even run time also.


What is the extra bonus on the iTunes up all night deluxe version?

there isnt a bonus dude


Is chocolate a natural resource why?

organic chocolate is but chocolate with extra flavors isnt as it is made in a factory


Why does dash light dimmer switch get hot?

The dimmer gets hot because it takes the extra voltage that isnt being sent to the lights and converts it to heat to dissipate it.


Where can you watch shuffle mermories English dub?

Shuffle memories isnt dub, cuz its just a recap of the original anime only the last one is the extra one


How do you do glitches on wee world?

Sorry no there isnt. Sorry no there isnt.


What are the lyrics to food criminal?

no it isnt ok isnt ok isnt ok dadyyouve been stuck by a food criminal


Why do you lose only a little weight when you work out?

This happens because when you work out, not only are you burning and losing extra unneeded fat, but you are also building muscle which isnt as heavy as fat but does add weight.


Is it illegal for a teacher to tell a student not to have facebook?

no it isnt but it isnt the teachers choice


Is there an I don't like food disease?

no there isnt that mite be a fib or a legent but there isnt


Where does mundian tu bach ke come from?

Isnt it indian??? Isnt it indian???