Two methods with same name but different return type?

Can I have two methods with same name but different return type in Java? I used to do this in C++ (method overloading or function overloading)
Here is my code:
import java.io.*;
public class Test{
public static void main(String ar[]){
try{          
//I give an invalid file name to throw IO error.
File file = new File("c:/invalid file name becasue of spaces");
FileWriter writer = new FileWriter(file ,true);
writer.write("Test");
writer.close();     
} catch (IOException IOe){
     System.out.println("Failure");
//call first method - displays stack trace on screen
     showerr(NPe);
//call second method - returns stack trace as string
        String msg = showerr(NPe);
        System.out.println(msg);
} // end of main
public static void showerr(Exception e){
     StringWriter sw = new StringWriter();
     PrintWriter pw = new PrintWriter(sw);
     e.printStackTrace(pw);
     try{
     pw.close();
     sw.close();
     catch (IOException IOe){
     IOe.printStackTrace();     
     String stackTrace = sw.toString();
     System.out.println("Null Ptr\n" +  stackTrace );
}//end of first showerr
public static String showerr(Exception e){
     StringWriter sw = new StringWriter();
     PrintWriter pw = new PrintWriter(sw);
     e.printStackTrace(pw);
     try{
     pw.close();
     sw.close();
     catch (IOException IOe){
     IOe.printStackTrace();     
     return sw.toString();
}//end of second showerr
} // end of class
[\code]

Overloading is when you have multiple methods that have the same name and the same return type but take different parameters. See example
public class Overloader {
     public String buildError(Exception e){
          java.util.Date now = new java.util.Date() ;
          java.text.DateFormat format = java.text.DateFormat.getInstance() ;
          StringBuffer buffer = new StringBuffer() ;
          buffer.append(format.format(now))
               .append( " : " )
               .append( e.getClass().getName() )
               .append( " : " )
               .append( e.getMessage() ) ;
          return buffer.toString() ;
     public String buildError(String msg){
          java.util.Date now = new java.util.Date() ;
          java.text.DateFormat format = java.text.DateFormat.getInstance() ;
          StringBuffer buffer = new StringBuffer() ;
          buffer.append(format.format(now))
               .append( " : " )
               .append( msg ) ;
          return buffer.toString() ;
     public String buildErrors(int errCount){
          java.util.Date now = new java.util.Date() ;
          java.text.DateFormat format = java.text.DateFormat.getInstance() ;
          StringBuffer buffer = new StringBuffer() ;
          buffer.append(format.format(now))
               .append( " : " )
               .append( "There have been " )
               .append( errCount )
               .append( " errors encountered.")  ;
          return buffer.toString() ;
}Make sense ???
Regards,

Similar Messages

  • Interfaces having methods of same signature but different return types

    I have two interfaces in two files:
    public interface InterfaceA
    int f();
    public interface InterfaceB
    void f();
    Now I want to write a class that implements these two interfaces. Is it possible? If yes, could you please provide a code example?

    The easiest thing would be to change one of the names! Another approach is to have views of the class implement one or both interfaces.
    Both:
    class C {
        private InterfaceA a = new InterfaceA() {
            public int f() {
                return 0;
        public InterfaceA asA() {
            return a;
        private InterfaceB b = new InterfaceB() {
            public void f() {
        public InterfaceB asB() {
            return b;
    }One:
    class B implements InterfaceB {
        private InterfaceA a = new InterfaceA() {
            public int f() {
                return 0;
        public InterfaceA asA() {
            return a;
        public void f() {
    }

  • Two methods with same name?

    hello,
    When a class has two or more methods with the same name but each method
    accepts unique parameters ..... what is this called? Overloading?
    Thanks,
    jd
    public class Test{
         public Test(){
              String bubby = sayHI("Jason");
              System.out.println(bubby);
         public String sayHI(){
              return "hi";
         public String sayHI(String name){
              return "hi " + name;
         public static void main(String[] args){
              getPrefs gp = new getPrefs();

    is this also called overloading when your doing it with the constructor?Yes, unequivocally.
    8.4.7 Overloading
    If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded. This fact causes no difficulty and never of itself results in a compile-time error. There is no required relationship between the return types or between the throws clauses of two methods with the same name but different signatures.
    8.8.6 Constructor Overloading
    Overloading of constructors is identical in behavior to overloading of methods. The overloading is resolved at compile time by each class instance creation expression (�15.9).
    QED, §

  • Importing files with same name but different extensions

    Hope I will be clear enough.
    Lets say that instead of sending a file from Lightroom to an external editor, I open it directly in Photoshop.
    When I'm done, I save the edited photo using the same name but in a different file format : IMG_1333.cr2 > IMG_1333.psd or IMG_1333.tif, and selecting the same folder location.
    Then, I come back into Lightroom and synchronise the folder.
    Why is the knew version of the picture not showing up beside the original file, despite the different extension ?
    I notice that the different extensions ( .psd or .tif ) are recorded in the sidecar files field in the Metadata panel.
    Of course, everything works fine if the file name is modified ( or just by selecting the "copy" option in Photoshop's "save as" dialog box ). Lightroom synchronise dialog shows the new photo to import.
    So, why is Lightroom not making a difference between files with the same name but different format extensions ?
    Thanks for any hint :-)
    Gilles.

    Gilles-
    I think your problem is likely that you have the option to stack the copy with the original turned on. First, right-click on one of these photos and go to the stacking menu. If Unstack is not grayed out then you have a stack -- select Unstack to reveal all copies.
    To change the automatic stacking of edited copies from Photoshop, when you next choose to edit a file, in the bottom left corner of the dialog is the check box for unstacking. Clear it, and that choice should "stick" until you change it again.
    Hope this helps!
    Tony

  • Unable to protect 2 computers with same name, but different Domains

    DPM 2012 R2 and UR2
    All DC:s and servers are 2012 R2 + Latest patches.
    ad.A.com has full  2 Way trust with ad.B.com. (Different forrest)
    DPM Server is DPM.ad.A.com
    Computer EXCH.ad.A.com is protected without problem 
    Computer SQL.ad.B.com  is protected without problem
    When I try to attach agent from EXCH.ad.B.com I'll get problems.
    Error 346: DPM is unable to retrieve the configuration information from EXCH.ad.B.com
    I'll guess this has to do with same name of computer, although in different domains.  Is this as Designed, or is it a defect? 
    /Peter

    please try a different User to attach/install the DPM Agent.
    if this is not the solution, you maybe have a Problem with wmi, try and test with wbemtest
    Seidl Michael | http://www.techguy.at |
    twitter.com/techguyat | facebook.com/techguyat

  • Grouping albums with same name but different artist?

    I have two albums in iTunes 7 which will not group correctly - either they show up as 24 or so separate albums or a single compilation or so on! I've tried marking them as compilations, as not compilations, you name it.
    They share the same album name, but have different artists and genres. How can I convince iTunes that they are two distinct albums? (I would have thought this test should be part of the application's logic, to be honest!)
    Gavin

    Note: I have this album set to be a compilation, I have filled in Queen in all 17 tracks as the album artist and all 17 tracks have the same Album name, it still does not group together properly when sorting by Album Artist (only when sorting by Album Name).
    iTunes 7 adds support for the ¨Album Artist¨ tag (as used and required by Windows Media Player). Adding support for this tag was actually a request I made myself a while ago.
    Unfortunately iTunes is not using this tag in a useful way (as it was intended). The purpose of this tag (as used by Windows Media Player) is to cope with Albums that contain more than one artist, a good example album is Queen's Greatest Hits III (see http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?id=161431875&s=14344 4 ) this album contains tracks by or featuring David Bowie and George Michael (amongst others) as well as Queen themselves, despite this, everyone would consider the Album itself to be by Queen.
    The purpose of the Album Artist tag is so that you can browse by the Album Artist and see the entire album listed (and not just those tracks specifically by Queen themselves). Therefore iTunes should if in browse view mode and sorted by Album Artist show the entire album grouped together - IT DOES NOT. Instead it appears to be being sub-sorted by Artist (as well as Album Artist) and this results in it being split in to 3 or more sections. This applies to all three view modes including the Coverflow view.
    As things stand the only benefit I get from iTunes now supporting the Album Artist tag is greater compatibility with Windows Media Player, apart from that it appears completely useless in iTunes as currently implemented.

  • Using multiple SSIDs with same name but different PSKs

    I have a central WLC 2504 controller that is being used for remote site FlexConnect 1141 APs. They all advertise three different SSIDs. One SSID is a global SSID that is the same at every office. One is a hidden SSID using 802.1x machine auth.
    The one I am trying to get working is the local office guest network. These SSIDs are all the same at each office but should have different PSKs. They are local to the office, therefore would only ever be applied to a specific FlexConnect group.
    I understand why in theory this is generally not a good idea but given these are for remote sites I'd like it to be possible. I always get this message though:
    "WLAN with duplicate SSID and L2 security policy found"
    Is there a way around this? New WLC code that allows it maybe?

    I was able to configure three (more I think possible) WLANs with same SSID name and all are WPA2-AES-PSK on the same WLC and all are enabled at hte same time.
    Note that you can not have any of those broadcasting on same AP group. Each WLAN can be only broadcasted on a separate AP group. For your sites, It will probably need you to define an AP group for each site to broadcast different WLANs on different sites.
    You can do that if all your WLANs have an ID of 17 or higher. (the reason is, WLANs of 1-16 are by default broadcasted on the default AP group. and because those can not be on the same AP group - including the default one - then you can't have them with WLAN IDs 1-16. i.e on same - default - AP group)
    HTH
    Amjad
    rating useful replies is more useful than saying "Thank you"

  • Change selected file to a file with same name but different extension.

    So I'm getting better at using Automator to create useful Services but still a bit of a newbie with doing powerful things with AppleScript... and I think this task requires some applescripting. Here's what I want to do:
    1. Based on a selected text file I want to run a pre-existing script that converts this file into an HTML file. This new file has the same name as the text file but has the .html extension rather than the .txt extension. (This step is easy enough and it works just fine already.)
    2. BUT NOW, I want to take that resulting HTML file and run another pre-existing script that converts the HTML file into an RTF file. The problem is that I basically want to change the selection from selectedfile.TXT to selectedfile.HTML so that I can proceed to this next step...
    3. AND THEN, I want to launch an app with this resulting RTF file. Again, I need to change the selection to selectedfile.RTF.
    Right now this requires me to run three different services, and changing the selected file between each step... But I'm sure there's an easy applescript way (or other way) to do this using just one service -- probably involves some filename parsing and some magic to change the selection??? -- or something like that...
    Could anyone point me in the right direction???
    Thank you thank you thank you!
    ~zyyyy

    You can chain your scripts together - depending on the complexity of your pre-existing scripts, you could do this with either AppleScript or Automator. The workflow would just take the selected text file (from step 1), convert to both HTML (2) and RTF (3), then launch your application with the resulting RTF - no need to change any selection.
    By the way, this is the Tiger Automator forum - Snow Leopard's Automator is quite a bit different, so you will probably have better luck posting in the Snow Leopard forum.

  • How to mass-reindex files with same name but different file extensions

    (reposting cos I'm confused about if I posted in the proper place before. Please delete if it is a repost.)
    So I'm on a remote workflow. I pick all the raw data and convert it in two quality standards, one on 'high' with full specifications and one on 'low' so I can send 2gb instead of 80gb of files through the internet for me to work remotely on, and to speed up the overall work rhythm because files are lighter. All I need to do is to work at my home with the low quality files, send it back to the client's computer after finished, re-index all files in their high resolution twins and we are good to go to finish the product with highest quality possible. But both high and low quality files need to be the same extension, that I picked .mp4 for it is the standard for pretty much anything.
    The point is, if I want to go full mobile, I only have an old HP Pavillion notebook and I really need to work with a video codec that goes very easy on it. I can only think about some 16:9 DV format. But the point is, this format generates a different file extension than my .mp4 standard, and Premiere, to my knowledge, really can't reindex files using only the file name while ignoring the file extension. It really should. And I really need to know how.
    We also need to consider Premiere versions that allow themselves to be installed at x32 processors. My desktop is x64 and I really have no problem to work on these standards, the problem is, in my notebook I really can't.
    So, halp?

    i want to say it was added in cs7, which requires subscription, so in that case might as well be using latest version 8.  if you had to use an older version like cs5, you could still use h264 or low bitrate dnxhd/prores codecs inside a quicktime .mov file. that would allow you to have same file ext on both versions of files.

  • Module with same name but different url

    Hi
    I am loading modules in my FlexLib super tab navigator
    I have two modules
    reports/sales/test.swf
    reports/service/test.swf
    First I load reports/sales/test.swf    in one tab, when I try to load
    reports/service/test.swf in next tab, it loads the  reports/sales/test.swf  again (I guess from cache)
    It is really strange or am I doing some thing wrong.
    Any help appreciated

    Hi
    Yes both Modules name is test.swf    but there package/URL is different.
    So you mean to say even if they are from different url/path, then we cannot have them loaded side by side ?
    Thanks

  • Import Library Error two functions with same name on different namespaces

    Im trying impor a library developed by me. In this library have two functions ShowMenu. First function be inside into a class and its declared as
    void __fastcall TTrayicon:howMenu(). The second function has declared as extern "C" __declspec(dllexport) ShowMenu(int x, int y).
    When i use de assistant to import this library i select the library and its header file. But in the next step the assistant show to me the first function.
    Anyone explain what's happen?

    I'm not sure what kind of answer you're looking for. LabVIEW's Import Library wizard does not always properly handle accessing functions in standard C++ DLLs. See this for more info.

  • How to create directories with same name in different schemas

    I have two schemas on the same database.
    I am trying to create directories with same name, but different paths in both the schemas.
    When i created the directories in second schema, the directory paths of the first schema are automatically changed to the second path.
    Is there any solution to create directories with same name, but different path in two different schemas on the same database.
    Thanks,
    Sridhar.

    Hi,
    >>When i created the directories in second schema, the directory paths of the first schema are automatically changed to the second path.
    If you have removed the REPLACE clause of your create statement, then you would see that is not possible.
    SYSTEM> connect scott/tiger
    Connected.
    SCOTT> create directory mydir as '/tmp';
    Directory created.
    SCOTT> connect legatti/pwd
    Connected.
    LEGATTI> create directory mydir as '/tmp';
    create directory mydir as '/tmp'
    ERROR at line 1:
    ORA-00955: name is already used by an existing object
    LEGATTI> select owner,object_name,object_type,created
      2  from dba_objects where object_name='MYDIR';
    OWNER     OBJECT_NAME    OBJECT_TYPE         CREATED
    SYS       MYDIR          DIRECTORY           19/12/2007Cheers
    Legatti

  • How to separate two contact with same name

    Hi, anyone knows how to separate two contacts with same name in Contacts?  Actually they are two persons from two gmail contacts with same name but different contact numbers, different emails....etc, but Contacts assume they are same person and merge them as one!

    FG,
    In Lion, you have the File > Duplicate option. You will then have two copies of your document open, and the one on top will have the temporary filename "OriginalFileName copy", and your next responsibility is to File > Save (or Command-S) and rename the duplicate. At this point, you edit the copy's name, to perhaps ...copy 1, or whatever you wish. The document you had open when you initiated the File > Duplicate will still be there on your screen, waiting for you to Close it or continue editing.
    Jerry

  • How to create two employees with same name as supplier record?

    Dear all,
    We need to create a supplier record for an employee so that we can issue invoice for pretty cash payment. If there are 2 employees with same name, how can we create supplier records for these 2 employees? The system does not allow duplicate supplier name.
    Please advise.
    Regards,
    HY

    Hello,
    In R12 it is possible to create 2 employee with same name but different employee number.
    And also possible to create these 2 employees as suppliers in Payables.
    HTH,
    Vik

  • Can I run 2 different domains with same name but on 2 different machines?

    I am trying to setup 2 domains with same name (sharedcds1) on 2 different machines (Machine1 and Machine2).
              When I start the weblogic managed server 1 (sharedcds1managedserver1) on Machine2, it throws an error saying it has some conflicts with the managed server 1 running on Machine1. How did the managed server of one machine know about the other server. Can I run 2 different domains with same name but on 2 different machines?
              Here is the error in the log -
              <Jun 14, 2005 10:53:29 AM EDT> <Error> <Cluster> <BEA-000123> <Conflict start: You tried to bind an
              object under the name weblogic.transaction.coordinators.sharedcds1managedserver1 in the JNDI tree.
              The object from 4596206652609838848S:130.170.61.153:[9505,9505,-1,-1,9505,-1,-1,0,0]:sharedcds1:s
              haredcds1managedserver1 is non-clusterable, and you have tried to bind more than once from two or m
              ore servers. Such objects can only be deployed from one server.>
              <Jun 14, 2005 10:53:29 AM EDT> <Error> <Cluster> <BEA-000123> <Conflict start: You tried to bind an
              object under the name weblogic.transaction.coordinators.sharedcds1managedserver1 in the JNDI tree.
              The object from 8842351474821025197S:130.170.61.154:[9505,9505,-1,-1,9505,-1,-1,0,0]:sharedcds1:s
              haredcds1managedserver1 is non-clusterable, and you have tried to bind more than once from two or m
              ore servers. Such objects can only be deployed from one server.>
              Thanks
              Satish

    Yes you can. Make sure that domains configured to use different multicast address. WLS uses multicast for communications between nodes in domain.
              although your configuration will work, you could have troubles if you going to execute inter-domain calls between domains/servers with the same names.

Maybe you are looking for

  • Force Flash weblink to open in SAME window as movie

    I'm using Adobe Encore CS4 to create a project that I want to export to Flash. However, I want one of my menus to be replaced by a web site. The goal is to have a flow such that: Menu 1. <User Clicks Item #1> Timeline plays user is forwarded to a web

  • Fedex can only be used with dreamweaver using metric, not pounds (lbs)

    Fedex can only be used with dreamweaver using metric, not pounds (lbs), evidently? This as per their tier 2 tech suport. They say I should just convert to metric, no joke! Any ideas would be greatly appreaciated? Also a warning to anyone thinking of

  • My PlayBook not showing up on my PC?

    Hello, I got a PlayBook yesterday as a present from my dad and am lovin' it! But when I connected it to my PC today and followed the usual instructions, it still didn't work for me- the BB Desktop Software showed the details (such as name and OS) of

  • Discoverer 10g unable to export the rows more that 65000 in excel

    Hi All, Earlier discoverer 10g used to export tmore that 65000 rowd data in to excel in multiple sheets. Suddenly it stopped. Any suggestions Regards, Swapnil

  • Change user-interface priority

    Hello LabVIEWERS , Does anyone of you know how to set/adjust the user-interface priority of a LabVIEW program. In an certain application I noticed a bad respons from LabVIEW towards the user-interface (it reacts only very slowly towards pushing a but