About overriding and overloading - Advanced Topic

Please look at this thread
http://forum.java.sun.com/thread.jsp?forum=31&thread=159240
Someone posted a code snippet that I can't fully explain, perhaps some Java Guru can answer!

This is the program:
The question is why the compiler error in the indicated line but not when upcating b to Homer.
<code>
class Homer {
     float doh(float f) {
          System.out.println("doh(float)");
          return 1.0f;
     char doh(char c) {
          System.out.println("doh(string)");
          return 'd';
class Bart extends Homer {
     float doh(float f) {
          System.out.println("doh(float)");
     return 1.0f;
class Hide {
     public static void main(String[] args) {
          Bart b = new Bart();
          b.doh('x');//compiler error in this line
          b.doh(1);
          b.doh(1.0f);
</code>
I looked for an answer in the Java Language Specification and I found the section 15.12.2.2
I will try to sum up in the following:
Steps performed by the compiler to spawn the symbolic reference for a given method invocation:
1) It finds out which class or interface looked for the method declaration.
In the example b.doh('x'); is the type given of b, that is Bart.
2) Looks for the methods that are both applicable and accesible in the methods declared and inherited in that type.
A method declaration is applicable to a method invocation if they have the same number of parameters, and, the types of the arguments in the declaration are the same or can be converted via a widenning convertion to those of the declaration.
Whether a method declaration is accessible to a expression of method invocation depends on the modifiers specified in the declaration and where the invocation occurs.
If there are several methods declaration both applicable and accessible the one more specific is chosen. If no more specific method exist, the compiler generates an error.
This how to find out which method is more specific:
Let T be the class where a method m is declared, and let U be another class where a method called as well m is declared. Both have the same number of parameters because they are applicable, but they have different types in the argument list. If it happens that T is a subclass of (or the same as) U, and, all the parameters types in the m declared in T can be converted via widenning convertion to those in the m declared in U (or are the same); the m in T is more specific than the m in U.
3) The checks described in the section 15.2.3 in the JLS are performed. They do not apply in our example.
Back to the example, the compiler finds two applicable and accessible methods in Bart:
float doh(float f)      declared in Bart
char doh(char c)     declared in Homer     
The last one inhertited from Homer.
So which is the more specific? let�s see first if doh declared in Bart is more specific than doh declared in Homer: Bart is a subclass of Homer, but float is not of the same type nor widenning convertible to char . Now, if doh in Homer is more specific than doh in Bart: well, Homer is not the same nor widenning convertible to bart. So there is no a more specific method and the compiler complains.
If you apply these rules to b upcasted to Homer doh(char) is more specific. The same happens if doh(char) is added to Bart.

Similar Messages

  • Question about overriding and overload

    Below is a super class named Animal and subclass named Horse
    public class Animal {
    public void eat() {
    System.out.println("Generic Animal Eating Generically");
    public class Horse extends Animal {
    public void eat() {
    System.out.println("Horse eating hay ");
    public void eat(String s) {
    System.out.println("Horse eating " + s);
    public class Test {
    public static void main ( strin []agrs) {
    Animal ah2 = new Horse();
    ah2.eat("Carrots");
    At the bold line, why can not polymorphism work here.
    Can anyone explain it to me pls.
    Thanks

    800239 wrote:
    Below is a super class named Animal and subclass named Horse
    public class Animal {
    public void eat() {
    System.out.println("Generic Animal Eating Generically");
    public class Horse extends Animal {
    public void eat() {
    System.out.println("Horse eating hay ");
    public void eat(String s) {
    System.out.println("Horse eating " + s);
    public class Test {
    public static void main ( strin []agrs) {
    Animal ah2 = new Horse();
    *ah2.eat("Carrots");*
    }At the bold line, why can not polymorphism work here.
    Can anyone explain it to me pls.
    ThanksThe variable ah2 is declared as a reference of type Animal. That means we can only call methods that exist and are accessible in Animal. The compiler doesn't know or care that at runtime the Animal reference variable points to a Horse object. All that it sees is that ah2 is of type Animal.
    The kind of polymorphism where that does matter is when a child class overrides (not overloads) a non-static, non-final, non-private parent method with the exact same signature.

  • About method overriding and overloading

    Hi,
    What we need method overriding and overloading
    What is significance of it
    regards
    Amar

    I've read a few articles lately about people whose senses and catagories seem to "leak"
    from one to another. For example
    * Monday is red, Tuesday is yellow, etc..
    * G is red, E flat is yellow, etc...
    * Red is sour, yellow is bitter, etc...
    With that in mind...
    Overriding is spicey (garam masala), overloading is bitter (top note of asafoetida).

  • Overriding and overloading together

    Hi all,i've a qn on overloading and over-riding combined.
    Consider the following program.
    class base {
    int i;
    base(int val) { i = val; }
    void f(int i) { System.out.println("base int"); } // <1> case1
    void f(double d) { System.out.println("base double"); } //case2
    class sub extends base {
    int j;
    sub(int val1, int val2) { super(val1); j = val2; }
    void f(int i) { System.out.println("sub int"); } // <2> //case3
    void f(double d) { System.out.println("sub double"); } //case4
    class Test {
    public static void main(String argv[]) {
    base a = new base(1);
    sub s = new sub(1,2);
    base b = s;
    a.f(1);
    a.f(1.2);
    s.f(1);
    s.f(1.2);
    b.f(1);
    b.f(1.2);
    when i comment case1 follwoing is printed
    base double
    base double
    sub int
    sub double
    sub double
    sub double
    apparently the reason being, when i override the overloaded function, the over-riding will hide all the variants of the member function and not just the one which i overrode.
    Line 5 - Here the subtle issue is that we are overriding already overloaded
    function. Case2 and case3 are overloaded (in base/sub classes) and case4
    overrides case2. Due to this , the overriding hides all the overloaded
    variants of that member function and hence case2/case3 are hidden. So due
    to dynamic binding and hiding of the overloaded (case3) , sub's double is
    called. Also since sub's int is not availble, co-ercion takes place too. IS THIS REASONING CORRECT??
    Also,what happens if i uncomment case1 and comment case3??
    Where can i find more information on this??
    Any help is highly appreciated
    Regards

    Then what will happen if i uncomment case1 and comment case3?Did you try it?
    base int
    base double
    base int
    sub double
    base int
    sub doubleIt is the difference between early and late binding.
    Overloaded methods are bound early.
    Overridden methods are bound late.
    To reiterate,
    Determining which of the overloaded methods to call is done when the class is compiled[], based on the compile time type information.
    Determining which overridden version of that method to call is done at [b]runtime.
    Even if a subclass DOES have a more appropriate overloaded method to call at runtime, that method can not get used. This was demonstrated when you commented out case1. The most appropriate method it found in the base class took a float. It knew nothing (at that time) about the overloaded version in the subclass.
    So we know the signature of the method we are going to call before we run the program.
    At runtime it looks at the actual type of the object that it has, and calls the version most lately declared in the object's hierarchy - ie this resolves the overriding.
    So explaining the results above:
    It detemines at compile time which version of the overloaded methods it will call.
    Because both are available in the base class, it will call
    f(int)
    f(double)
    f(int)
    f(double)
    f(int)
    f(double)
    At RUNTIME, when you call it
    - the first object is of type base, and thus calls the int and double methods of base
    - the second object is of type sub. But it doesn't override the int version of the method, so base's implementation is used. It DOES override the float version, so you get its implementation there.
    - same result for the third object.
    Hope this clears things up,
    cheers,
    evnafets

  • Overriding and Overloading

    Hi,
    I'm new to abstract Java world.
    If I compare Overide and Overload,
    Does it just mean
    OVERIDE superclass and subclass having same method name with different functions?
    But OVERLOAD is having more than 1 method with the same name but with different functions?
    Thanks,

    Generally you're right. In specification there is "method signature" used to distingush it.
    overide: exactly same signature but different implementation in subclas
    overload: same name but different signature, means: list of arguments, thrown exception

  • How about telling us in advance what programs or plug ins will NOT work with the new Firefox. I just lost my "safe search" feature with AVG, and I think that's sort of important!

    Just upgraded to Firefox 6 (which I like, by the way), but when I upgraded to 5, I lost the gaming feature when I installed AVG, and now I've lost the AVG safe search feature, which I think is very important considering that I have grandkids that use my computer,
    How about telling us in advance (and I'm running Firefox, so a link would be easy) whether or not we're going to lose an important feature like the one I lost?
    So, how do I get back to Firefox 5 without uninstalling and reinstalling so I can have that safe search feature back?

    Attached is Dennis Linam’s Audition – “Log File” and “Log – Last File”
    Contact information Dennis [email protected]
    Previous contact information with your organization (DURIM):
    Dennis - i just finished my audition trial and bought the subscription the 2014 version.
    created by durin in Audition CS5.5, CS6 & CC - View the full discussion 
    DURIM - Okay.  I would expect the "Cache Warning" message because your default directories would not be the same as the ones in the settings file I generated.
    If you go back to the "7.0" directory and open the "Logs" folder, can you copy the "Audition Log.txt" file and send it as an attachment to [email protected]?  We'll take a look in that logfile and see if it gives us more information about why this is failing now.
    Also, do you have any other Adobe applications installed on this machine, such as Premiere Pro?  If so, do they launch as expected or fail as well?
    I do have the trial Pro version of Adobe reader, but I have not activated it, because I fear the same thing will happen did it. I cannot afford to activate the subscription for that product and take the chance of it not working either. I depend on those two programs religiously. Here is the files that you requested. I appreciate any help you can give me to get this audition program started
    Audition Log- file
    Ticks = 16       C:\Program Files (x86)\Common Files\Adobe\dynamiclink\7.0\dynamiclinkmanager.exe
    Sent from Windows Mail

  • Nation advanced search on my IMac, which I think is a spyware. When I want wo go to google chrome it redirects me to this ominous nation search engeen. Has anybody got some information about it and how I can get rid of

    I just caught a thing called nation advanced search on my iMac computer, which I think is a spyware. When I want wo go to google chrome it redirects me to this ominous nation search engeen. Has anybody got some information about it and how I can get rid of it? What does can it do to my data and is it really a spyware? I just found information in youtube, that it is a spyware, and that it has to be removed, but the information they give for the removal is just working for PCs not for Macs.
    Please help!

    Thank you all for the friendly assistance provided.
    I have found a solutiton to the problem I was having.  I hope that many more can benefit from the information I will provide.
    I started at Finder, then went to GO selected COMPUTER and then Machintosh HD, then Library, Scripting Additions folder and trashed all that was there. This is what worked for me, if you choose to not delete your content its up to you.  Happy Computing.
    God Bless!!
    This solved my question 

  • Where can I find detailed, systematic HELP for advanced topics relating to Thunderbird?

    In moving from XP to Windows 7, I opted for Thunderbird as email client in order to bypass Microsoft's hyper-intrusive Windows Live Mail (I used Outlook Express for years).
    I have a very complicated email structure, and it's taken me weeks (seriously) to learn how to replicate it in TBird. Now I'm trying to customize TBird further, but none of the TBird articles/forum Q&A's/Google searches address my questions.
    There is no live tech support for TBird, and I'm just about ready to leave it for good. However -- one last effort: where can I find detailed, systematic HELP for advanced topics relating to Thunderbird?
    Thanks.

    I am no expert, but I don't know of any authoritative reference as to what elements of HTML and CSS are supported in Thunderbird. However, as I believe you appreciate, it's more than just what Thunderbird supports, but one must also think about what is likely to work in other email clients. Keep it simple. Avoid ancient deprecated tags that other email clients may not support, and for similar reasons, avoid cutting edge technology. Remember that recipients using tablets or smartphones won't appreciate large fancy email documents.
    The only thing ''guaranteed'' to work in email is plain text. ;-)
    If you haven't already discovered it, the Stationery add-on is designed specifically to support OE stationery in Thunderbird. Your existing stationery may "just work" in this add-on. It makes switching between various stationery templates much easier, but I'm not confident that it will affect interpretation of your CSS or HTML coding.
    Your code is at least clean and minimal. Most times my involvement with troublesome templates and signatures centres on the horrible bloat and mso custom code generated by Word or Outlook.
    Having said that, you and I are mortal enemies, as I don't have much patience with what you aspire to achieve. I specifically don't like background images, nor being obliged to suffer other folks' bizarre choice of typefaces and colours (but your simple 12pt black Tahoma is quite inoffensive. ;-) ) I'm of an age where my tolerance and eyesight are easily offended.
    Nonetheless, I'm intrigued by how to parse the tag for the background image, as it doesn't look like a legitimate pathname to a graphics file. Does the background image actually appear as required?

  • I have a Macbook Pro june 2011... I have 8GB ram but I only have 256mb VRAM... I've read some other questions about this and I realized... Why do I not have 560mb of VRAM since I have 8GB of RAM? Is there any way to get more VRAM to play games on steam?

    I have a Macbook Pro june 2011... I have 8GB ram but I only have 256mb VRAM...
    I've read some other questions about this and I realized... Why do I not have 560mb of VRAM since I have 8GB of RAM?
    Is there any way to get more VRAM to play games on steam?
    I've learned  by reading other topics that I can't upgrade my graphics card on my Macbook Pro because it's soldered into the motherboard or somthing like that, but please tell me if there is any way to get more video ram by chaning some setting or upgrading something else. I'd also like to know why I only have 256MB of VRAM when I have 8GB of RAM, since I have 8GB of RAM I thought I was supposed to have 560mb of VRAM...
    So the two questions are...
    Is there any way to upgrade my VRAM, so that I can play games on steam?
    Why do I only have 256MB VRAM when I have 8GB total RAM?
    Other Info:
    I have a quad core i7 Processor.
    My graphcics card is the AMD Radeon HD 6490M.
    I am also trying to play games on my BOOTCAMPed side of my mac, or my Windows 7 Professional side.
    THANK YOU SO MUCH IF YOU CAN REPLY,
    Dylan

    The only two items that a user can change on a MBP are the RAM and HDD (Retinas not included).  You have what the unit came with and the only way you will be able to change that is to purchase a MBP with superior graphics
    If you are very much into gaming, the I suggest A PC.  They are far superior for that type of application to a MBP.
    Ciao.

  • Does anyone know anything about iPhones and/or iPads having barcode scanners for business purposes?

    Does anyone know anything about iPhones and/or iPads having barcode scanners for business purposes?  I am doing some research about the benefits of using a Q R Code type of system, but question which handheld sets would be more compatible for using that type of barcode.  Being a avid Apple supporter, I cannot seem to find very much research on this specific topic.
    Any thoughts?

    I have a QR reader on my iphone. Works just brill. I've used it to create business cards, with a boat load more information on it (Including my duty times, etc)
    Most of the QR readers in the App store are free, so give them a try. You can copy and paste the data just like normal text.
    What application for them did you have in mind?

  • From hdd to ssd: what about os and all data?

    Hello everybody.
    I know there are a lot of discussion about it and i read lots of them,
    But maybe because english is not my first language, i have some problems in understanding the right things to do.
    Also, i am pretty new with mac world as i just have my mb pro since last november.
    so i hope you can help me with easy words, thanks.
    here's the list of what i have:
    - MacBook Pro 2.5ghz, 4gb ram, 500gb hdd
    - os x mavericks (installed 2 days ago)
    - all my documents/photos/music in my MB Pro
    ... and:
    - crucial ssd 480gb
    - an external hd enclosure usb 3.0
    - the 17 in 1 tool kit
    What do i have to do first?
    And how will i transfer/install the os x from hd to ssd? or it "remains" somewhiere in my MB Pro?
    I know there is a link from crucial with the procedure for removing hd and installing ssd, is this all i have to do?
    Really hope you can help me... thanks in advance!
    F.

    Follow these steps:
    Make certain that you have your data backed up as a precaution.
    Attach the SSD to the MBP and open Disk Utility>Erase.  Format the SSD to Mac OS Extended (Journaled).
    Open Disk Utility>Restore and in the 'Source' field drag the internal HDD from the left hand column.
    In the 'Destination' field, drag the SSD from the left hand column.
    Click on the 'Restore' button.
    Depending upon the amount of data, this process may take a couple of hours.
    When finished, restart your MBP holding doen the OPTION key.  The display should show the internal HDD and the external SSD (yellow in color).  Click on the SSD and if it boots the MBP, the clone has been successful.
    Perform the physical swap.  Your MBP should be ready to go.  All of your data will be on the SSD exactly as it was on the HDD.
    Ciao.
    Message was edited by: OGELTHORPE

  • How should we be going about creating and saving and/ or exporting our ODC files using Excel 2013 for use in Project Server 2013 Online?

    Hi I need your guidance on how I should go about setting up my Excel 2013 reports so that others in our Project Online 2013 environment can access and updates these reports of mine.
    My questions are as follows:
    I presume I need to create and save my ODC files in a PWA > Data Connections folder.  I have English and French users in our environment.  Do I need save them twice?  Once in the French and again in the English Data Connections folder?
     Likewise for the Excel file? 
    How should I go about creating my ODC files within Excel?  By default, the ODC files are being created on my PC's > My Documents > My Data Sources folder.  I presume I need to get them saved or exported to the PWA > Data Connections
    folder. So, How should I be going about creating and saving and/ or exporting the ODC files???
    FYI...My oData Feeds that I wish to use and join in this particular Excel file are as follows:
    https://cascades.sharepoint.com/sites/pwa/_api/projectdata/AssignmentTimephasedData01T00:00:00'
    https://cascades.sharepoint.com/sites/pwa/_api/projectdata/Projects()?$select=ProjectId,ProjectName,CAS_Classification,CAS_PCO,CAS_IT_Department,CAS_Program,CAS_SubProgram
    https://cascades.sharepoint.com/sites/pwa/_api/projectdata/TimeSet()?select=TimeByDay,TimeDayOfTheWeek$filter=TimeByDay ge datetime'2014-10-19T00:00:00'
    https://cascades.sharepoint.com/sites/pwa/_api/projectdata/Resources()?$select=ResourceId,ResourceName,Programs,Supplier,Source,Role,CostType
    Thanks in advance,
    \Spiro Theopoulos PMP, MCITP. Montreal, QC (Canada)

    Thank you Guilaume.  May I ask you to help clarify a bit more for me?  If I have to do it for both languages (the reports and ODC files), do I simply copy the same ODC files from e.g., the English to French folder in PWA (Odc files)?  Or does
    that defeat the purpose?  Or, do I need to create a new set of oData Feed Connection files with a French version of Excel 2013 and save them to the French Data Connections folder in PWA?  Do I need to have a French version of Excel 2013 to create
    French ODC files and ultimately French based reports and/ or vice versa?
    I did notice that the following oData metadata command from within a browser produces different results (ie., English versus French metadata returned) depending on who runs it (i.e., French or English user, etc).  As you can see I am a bit confused.
     Any help you can provide would be greatly appreciated.
    https://XXXXX.sharepoint.com/sites/pwa/_api/projectdata/$metadata
    \Spiro Theopoulos PMP, MCITP. Montreal, QC (Canada)

  • About order and delivery quantities

    I have a number of questions about order and delivery quantities:
    1) How can I make a delivery even when there is no order?
    2) Where is customization of the controllig mechanism if the quantity of the items can be higher at the in the order than at the delivery or not?
    Thanks in advance...

    1) How can I make a delivery even when there is no order?
    Transaction Code: VL01NO
    Delivery Type: LO
    2) Where is customization of the controllig mechanism if the quantity of the items can be higher at the in the order than at the delivery or not?
    Ideally the Quantity of Order can be higher at Order, compared to Delivery. This is a case of partial delivery. Always ensure that Delivery Quantity = Pick Quantity i.e if Delivery Quantity is 50, but you want to deliver 30, in that case reduce the delivery quantity also to 30. If the quantity is different, PGI will not be allowed.
    Regards,
    Rajesh Banka
    Reward points if helpful.

  • VLAN Override and Web Auth: How to overcome issues?

    Hello
    I have been investigating if we can deploy vlan override and assign a user vlan via RADIUS, post authentication on a WRD SSID. Having read around the discussions, I can see that there are others who have wanted similar, but have been told that it is not possible:
    "Marucho, the particularity of how Web authentication works on the WLC  is that it is carried over HTTP between Client and WLC. So the Wireless  Client has to already have an IP address prior to starting the web  authentication. Since the Wireless Client already has an IP address then  you cannot override it anymore.
    Unlike  dot1x, which takes place over EAPOL and then when you have eap success,  client moves to get an ip address from the sent by Radius VLAN."
    However, we still have a problem that we would like to overcome and wonder if anyone has any experience or suggestions they could share?
    We are a University with a large number of devices grabbing an IP address whilst only remaining associated and not actually going on to authenticate through the WRD. This creates a situation where we have a large number of IP addresses deployed unnecessarily and we would like to tackle this.
    We are unable to use private IP for authenticated users (Policy decision) but could use them for associated users and so were hoping we might be able to deploy a private subnet on the WRD SSID prior to authentication and then use VLAN override to assign authenticated users onto the correct VLAN. In order to try and achieve this we were planning on using a very short DHCP lease on the private subnet, so that post-authentication the client device requests a public IP address almost instantly.
    Is there any way of achieving this that someone could suggest or would we be knocking our ehads against a brick wall?
    thanks
    Bryn

    Just giving 2 ideas :
    -How about using a WPA PSK on your webauth ssid ? Just give the PSK in the SSID name. This prevents non-intended connections (no automatic association because it's open ssid) and still allows anyone with an intention, to connect to it and you still have the webauth behind. This reduces number of ip addresses.
    -How about modifying the webauth successful authentication page to give the credentails to access a private network (PSK or dot1x) where credentials would regularly change ?
    Those are workarounds.
    Nicolas

  • I deleted over 200 self taken videos and over 500 pictures in the Moments window in my photo tab.  They deleted but still show in my setting in the About window and I haven't gained any additional storage capacity.  What do I do now?

    I deleted over 200 self taken videos and over 500 pictures in the Moments window in my photo tab.  They deleted but still show in my setting in the Setting/About window and I haven't gained any additional storage capacity.  What do I do now? Please help!  Thanks in advance for any assistance!!!

    Check the Recently Deleted album. Deleted photos will reside there for up to 30 days. This allows all those people who accidently delete photos to recover them, something they couldn't do before. To actually free up the space you might have to delete the photos in the Recently Deleted album.

Maybe you are looking for

  • Please reply for the query tuning

    Hi, i am a beginner in oracle dba, I have to know if i have studied little bit about query tuning in ORACLE. I wanna know if i have the following query and its plan then how it can be tuned: QUERY:           SELECT z.emplid ,h.first_name || ' ' || h.

  • ORA-17125: "Improper statement type returned by explicit cache"

    I have been using SQLJ to some extent in the current java project but just now I'm getting this error on a particular piece of code which was converted using SQLJ pre-compiler: // #sql [xtrConnectionContext] { CALL // XTR_CFNG_DBAPI.pRemoveXTRLotsLog

  • Legit Download or Scam

    Greetings, I am interested in purchasing a new version of cs6 master collection, and have found one being offered online for 800 euro, with the download being advertised as off of the adobe site. More clearly, after the download, it appears the purch

  • Aged Material Report

    Hello Experts, Is there a standard SAP report for "Aged Material"? Please let me know. Your help is greatly appreciated. Thanks.

  • JTable custom cell renderer and editor breaks row sorting

    Hello Forum, I have a JTable on which I set setAutoCreateRowSorter(true); I then hook up a custom cell editor and renderer for the Date class. this.tblLeden.setDefaultRenderer(Date.class, new DateCellEditor()); this.tblLeden.setDefaultEditor(Date.cla