Quantcast
answersLogoWhite
notificationBell
Computer Programming
Java Programming
Pierre-Auguste Renoir

Why do you use an interface for implementing methods Isn't it just extra work?


Top Answer
User Avatar
Wiki User
Answered 2009-04-10 19:47:33

== == 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.

001
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0
User Avatar

Your Answer

Still Have Questions?

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



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


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.


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


Where does mundian tu bach ke come from?

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


Is there an I don't like food disease?

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



Why would a guy say that he isn't trying to impress me?

A guy would say that he isnt trying to impress you maybe because he isnt or because he wants you to think he isnt.


If air is fluid why isnt convection responsible for transfering heat to the earth from the sun?

if air isnt fluid why isnt convection responsible for transferring heat to the earth from the sun


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.


Why was golden temple made of gold?

because they required the potatoes to work extra hard for the money they made, and if it fell, it would smash them all. so they needed it to be strong, and gold isnt bendable.


Is there a peninsula in Africa?

YES! THERE IS! i know 4 a fact there is!NO! THERE ISNT! i know 4 a fact there isnt!


Is Chris Brown a virgan?

No he is not. No he isnt. U CAN TEL HE ISNT BY HIS SONG ''TAKE U DOWN''...


Is the Mottw and bailey castle haunted?

thats funny isn't it no it isnt not dont no isnt haunted


How can you play paper Mario online?

gamecube isnt wifi enabled there isnt an ethernet port.


How long after the full moon is it until new moon?

No it isnt and ur question isnt correct


Is can't a verb?

No can't isnt. e.g i am running, i am jogging, i am masturbating. i am can't-ing isnt.


Does wind effect aerodynamics?

GO REASERCH IT LOSER WIKIANSWERS ISNT THE ANSWER FOR EVERYTHING GET A LIFE!!! GO REASERCH IT LOSER WIKIANSWERS ISNT THE ANSWER FOR EVERYTHING GET A LIFE!!! GO REASERCH IT LOSER WIKIANSWERS ISNT THE ANSWER FOR EVERYTHING GET A LIFE!!! GO REASERCH IT LOSER WIKIANSWERS ISNT THE ANSWER FOR EVERYTHING GET A LIFE!!! GO REASERCH IT LOSER WIKIANSWERS ISNT THE ANSWER FOR EVERYTHING GET A LIFE!!!


Is outdated soup okay to eat?

no beceause if it isnt in date it isnt ok so i wouldnt try it


What are the reasons for not smelling anything at all?

nose isnt working ,while the part isnt responding........ ect


How do you get to snowpoint in Pokemon Platinum?

isnt it just like pearl and diamond isnt it just like pearl and diamond


Is charlie from bustard dead?

no he isnt, i dont know where you heard that. And its *busted* but no he isnt, i think he was in an accident not major though.


Why Isnt The Walking Dead Season 6 On netflix?

I just have finished season 5, and season 6 Isnt there! why?


Still have questions?

Trending Questions
How to Make Money Online? Asked By Wiki User
Best foods for weight loss? Asked By Wiki User
Does Neil Robertson wear a wig? Asked By Wiki User
Unanswered Questions