Home > Default > Why use interfaces?

Why use interfaces?

November 30Hits:0
Advertisement
What is the point in interfaces?
I understand that you can use them to ensure a class implements certain methods. Why does this need to be done?
Do they have any other use?
thank you
mike

Answers

Interfaces are used for a number of reasons. Consider:
1. Java is a strongly-typed language which means that any when a method is called on an Object (non-reflectively), at compile-time that method must be known to exist on the Object.
2. Java does not support multiple-inheritance.
Now, suppose you decide to implement the Event model. Interfaces are a very powerful way of allowing you to do this. Providing an abstract superclass that has an abstract actionPerformed() method would suffice, but be unnecessarily restrictive (lack of multiple inheritance). So why not define an interface as a "type"? The Object which is being listened to can inform its listeners via this method. It is excellent for encapsulation as the listened-to object is unable to invoke anything else on the listener other than actionPerformed() (or whatever) and cannot do anything naughty that you don't want it to.
A fundamental principal of OO is to separate interface and implementation. What better way that to make interface and implementation a fundamental part of the language?
Also consider:
You have a massive application that uses Set-like objects all over the place. In your code, when creating new ones and passing around references you do so by the class HashSet (and explicitly constructing them). Supose on switching-on your App you find that it performs like a dog and identify that it is poor HashSet performance at fault. You decide to buy-in a superfast Set-like object from a vendor. Oh dear, you have to do a global search-and-replace on all of your code. Had you used Factories and interfaces, though you would have...
public class CollectionFactory {
  public static java.util.Set newSet() { return new HashSet(); }
  //etc
//and the implementation...
public void myMethod() {
  Set s = CollectionFactory.newSet();
  //pass the INTERFACE Set around
}You now only have to change ONE line to switch implementations. Super! Note this is the entire point of the J2EE specification. Sun define APIs that handle database-connectivity, server-side processing (Servlets) etc etc. and you and I are free to buy the implementation from whichever vendor we prefer.

Read other 4 answers

Tags:

Related Articles

  • Open Interface Rejects - SQL ReportOctober 11

    Hi guys, I'm trying to help out some folks in AP with an easier access to view Open Interface rejects. I already knew the relation between AP_INVOICES_INTERFACE and AP_INTERFACE_REJECTIONS was INVOICE_ID AND PARENT_ID (respectively). Now I've seen th

  • Logical interface in solaris 10October 11

    Hi there, I need to configure logical interface in a solaris 10 3/05 server. After reading the Solaris 10 IP services manual, I am not quite sure what to do. All the examples and explanation are about using the new subcommand addif of ifconfig. It wa

  • Unable to see interface on ASA 5510 FirewallOctober 11

    Hi All, I am unable to see 4th interface on my firewall i.e fastether0/3 on my firewall ASA 5510. Below is the output. ciscoasa# sh int ip br Interface                  IP-Address      OK? Method Status                Protocol Ethernet0/0            

  • ASA 5505 backup interfaceOctober 11

    Hello, I have setup ASA 5505 with 2 ISP, named outside (primary)  and backup, the scenario is if outside down, then backup will take over, it works now. But it is not working when the primary connection cannot reach the gateway with the interface sti

  • Run SAP PI Interfaces 24X7. HowOctober 11

    How I can run PI Interfaces 24X7 to achieve Business Continuity. The solution provided as u2013      Install Separate additional PI System u2013 called as Federation PI. Is this solution really applicable? Details: 1.     Our Current Production Lands

  • I am trying to transfer sound created on a Fantom X7 to my mac and garage band and have gotten so many different directions from others regarding the mode of connection - I am trying to use an interface and usb connection - anybody have advice?October 11

    Good morning! I am trying to transfer sound from a FantomX7 keyboard synthesizer to garageband  through an interface (MAudio) and with a usb cable.  I see the image of the sound being recorded but I cannot hear anything - I know that there is a speak

  • Problems access to a web application (Web Interface or Web report)October 11

    Hi, We found problems with the access to web application. Some users have problems with direct links to the web applications(Web Interface or Web reporting), when they click on the link an error message appears, the message displays the following tex

  • AP error while submitting "Payable Open interface"October 11

    Hello, I am trying to run the request 'Payables open interface'. I got the output once and the error was because the supplier site was invalid. I created a supplier site saved it and ran the request again. Now, I am getting the following error. Payab

  • Facing issues in Payables Open Interface Import ProgramOctober 11

    Hi, We have a requirement of calling Payables Open Interface Import Program from Pl/sql package. Invoices are getting created. But the issue is when we query our invoice from invoice workbench and we try to view  the record history (help->record hist

  • Error when running Payables Open Interface ImportOctober 11

    Hi Experts, I need help. I'm facing a problem when run Payables open interface import. Before that, I was running same concurrent many times and the concurrent program is completed normal. But, when I try again I'm getting error. what exacly problem

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 0.617 s.