About overloading and overriding

I hava a Java code like this:
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);
An error was ocurred:reference to doh is ambigous
Who can told me why?

andy.g
you are wrong!
you cannot undertand what is the different between
overlaoding and overiding!sorry for the wrong answer.
the point is the super-class's method with most specific signiture has the same priority as the sub-class's method that could match(not exactly the same, but can be promoted, like char to float) the signiture during method binding/finding. java always put the current class's method at the first place, but when a method in super-class is more specicif in signiture, the compiler can't determin which one to use.
actually, there is nothing related to overriding in this case, e.g.:
class A{
  char method(char c){.....}
class B extends A{
  //nothing's been overrided
  float method(float f){......}
  public static void main(String[] args){
     B b = new B();
     b.method('c');  //compiler error.
}so it is definetely a priority's problem, the super class's methods do not have the same prority as the sub-class's.

Similar Messages

  • What's the difference between "overloading" and "overriding" in Java

    What's the difference between "overloading" and "overriding" in Java

    hashdata wrote:
    What is the real-time usage of these concepts...?Overriding is used when two classes react differently to the same method call. A good example is toString(). For Object it just returns the class name and the identityHashCode, for String it returns the String itself and for a List it (usually) returns a String representation of the content of the list.
    Overloading is used when similar functionality is provided for different arguments. A good example is [Arrays.sort()|http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#sort(byte%5B%5D)]: all the sort() methods do the same thing (they sort arrays), but one sorts byte-arrays, another one sorts int-arrays, yet another one sorts Object-arrays.
    By the way, you almost certainly mean "real-world" usage. "real-time" (and thus "real-time usage) means something entirely unrelated to your question.

  • Overloading and Overriding

    Why do we need Overloading and Overriding?

    EJP, I'm just curious to know why you mention "*Overloading gives us the power to call a single method and pass it different parameters and the compiler at runtime decided which suitable method to call*" as a wrong answer.Because it is a wrong answer. The action occurs at compile time, not at runtime. The compiler isn't even present at runtime, by definition, so the statement is not just wrong but a contradiction in terms.
    Is there any other reason for having overloading in java?The statement isn't a reason at all. It's just a definition, and an incorrect one, of what method overloading is. A definition is not a reason.
    So why you're asking me for another reason when there hasn't been one provided at all in the entire thread, other than by me, is a complete, utter, and baffling mystery.
    You seem to have the same problem ram has, of confusing 'what' with 'why', and of confusing definitions with reasons. One is bad enough, please let's not have two of you.

  • OO:overloading and overriding in tandem..Interesting one

    Assume Dog IS-A Animal
    class test {
    public static void main(String[] args){
    Animal a = new Dog();
    foo(a);
    void foo(Dog d){
    d.printme();
    void foo(Animal a){
    a.printme();
    }//end of class test
    class Animal{
    void printme(){
    System.out.println("i am in animal");
    }//end of class animal
    class Dog{
    void printme(){
    System.out.println("i am in Dog");
    }//end of class dog
    Question:what will it print and explain how briefly u arrived at that result? (I assume u fix some obvious compile time errors(missing ; etc) as this is just a rough snippet)
    NOTES-----------
    1.Overloading has to do with reference type
    2.Overriding has to do with the object to which it actually points

    ok mlk..really sorry for being so naive.I sometimes don't clearly express my doubt. I jus got a doubt in my mind regarding overriding + overloading and i constructed a sample example as a concept(not actual code).
    Here's the full fledged program..it compiles fine.And IS-A is perfectly satisfied in this case.
    class Animal{
    void printme(){
    System.out.println("In Animal");
    class Dog extends Animal{
    void printme(){//overriding the Animal printme() method
    System.out.println("In Dog");
    public class TestDog {
    void foo(Animal a){
    a.printme();
    void foo(Dog d){
    d.printme();
    public static void main(String[] args) {
    Animal a = new Dog();
    TestDog t = new TestDog();
    t.foo(a); //method overloading
    And it prints In Dog..as expected.Got it.Thanks.

  • About hiding and overriding

    Hi,can u tell what is the difference between hiding and overriding.
    Is hiding only applied to class method(static) and overriding only applied to instance method?
    It is a must to use "super" keyword to use a overridden method.Is that right?
    And Look at the code below:
    Can u tell why s.greeting is refer to static method of super while s.name() refer to method of sub?
    What is type and what is class then? Any difference?
    I have read the doc of Java Spec and still do not feel clear about it.Thank you for attention.
    class Super {
    static String greeting() { return "Goodnight"; }
    String name() { return "Richard"; }
    class Sub extends Super {
    static String greeting() { return "Hello"; }
    String name() { return "Dick"; }
    class Test {
    public static void main(String[] args) {
    Super s = new Sub();
    System.out.println(s.greeting() + ", " + s.name());

    It's not necessarily a must to use the "super" keyword in an overridden method. If your class extends some class called "TopClass", and "TopClass" has a constructor, then you must use the super keyword as the first statement in the constructor of your class.
    When you extend a class, your class becomes a type of that class. If you want to override methods, the only requirement (that I know of) is that only the method's body can differ from that of it's super class.
    i.e.
    Here's a method from some super class:
    public boolean mySuperMethod(String s) {
    System.out.println("Hi");
    return true;
    Here's the overridden method from an extended class:
    public boolean mySuperMethod(String s) {
    boolean b = false;
    return b;
    As you can see, you can do whatever you want inside the method, but everything else has to stay the same (as far as I know).
    Here's another example that I did a while back. It's a checkbox that can't be tabbed to and can't accept user actions. I used it simply as an indicator for something.
    import java.awt.*;
    public class NonTraversableCB extends Checkbox
         public NonTraversableCB()
              super();
         public NonTraversableCB(String label)
              super(label);
         public boolean isFocusTraversable() {
              return false;
         public void processEvent() {
              return;

  • BAPI Overloading and overriding

    Hi Friends,
    I just want to know Whether we can use BAPI Overloading and overiding concept is possible in BAPI?
    Regards
    Dinesh

    Hello,
    As far as I understand It is not possible but can you please explain little bit more about the requirement so we may think of some alternative.
    Enjoy SAP!
    Augustin.

  • Difrence bitween overloading and overriding

    Plese give the definition & example for polymorphism

    Polymorphism is of two types
    1) static or design time polymorphism
    2) dynamic or run time polymorphism
    example for design time polymorphism is overloading (i.e) a method with same name shares diff signatures
    For eg: arithmetic(int,int)
    arithmetic(int,int,int)
    example for run time polymorphism is overriding (i.e) a method with same name exists in both parent class and child class.
    At run time it will be decided which method to call based on the object which calls it
    Hope this clarifies yr query
    Regards
    Jansi

  • Difference between OVERLOADING and OVERRIDING??

    Hi guys,
    can you please exaplain me difference between Overloading & Overriding with example?
    Thanks,
    JaxNa

    Here is the example of overriding :
    /* PARENT CLASS */
    package com.example.supreclass {
         public class parentClass {
              public function parentClass() {
              public function fromParent(myString:String):void {
                   trace("Output :", myString);
    /* DERIVED CLASS */
    package com.example.supreclass {
         public class myExample extends parentClass {
              public function myExample() {
              /* Override the method */
              override public function fromParent(outputStr:String):void {
                   super.fromParent(outputString);
                   trace("Override example");
    Overriding means redefining the inherited method (the parent method still can be invoked using 'super', as shown in example )
    These links should give you some idea on these :
    Overriding : http://www.kirupa.com/forum/showthread.php?p=1896981
    Overloading means having many functions with same name but each differs by parameters it takes (either by data type or number of parameters passed) and return value.
    Overloading : http://www.techper.net/2008/07/08/missing-method-overloading-in-as3/

  • Overloading and bla extends bla

    if class A extends class B and they have both got the same name on a method but different arguments does his make it overloading still or is it overriding?thanks in advance.

    is it only overloading because they extend each other
    or would it be anyway?It's just overloading because you have a class that has two methods with the same name.
    and if the arguments where the
    same would it still be overloading?No, that would be overriding, and only if that method is inherited.
    sorry im asking so
    much but i'm having trouble understanding the whole
    overloading and overriding situations when its
    extending a class is involved,does it make a
    differance?thanksYes, it does, to overriding. X defines an implementation that you want to change. If Y doesn't inherit it, there is nochting to change. You'd just be implementing yet another method.

  • Can we use overload and overwrite concept in OO-abap

    hi
    can we use overload and overwrite concept in OO-abap

    Hi
    CLASS zl_lcl_vehicle DEFINITION.
    PUBLIC SECTION.
    Signature of method
    METHODS: set_make
    IMPORTING value(im_make) TYPE string " Pass by value
    im_model TYPE string," Pass by reference
    ENDCLASS. "zl_lcl_vehicle DEFINITION
    CLASS zl_lcl_vehicle IMPLEMENTATION.
    Implementation of method.
    METHOD set_make.
    IF im_make IS NOT INITIAL
    AND im_model IS NOT INITIAL.
    gv_make = im_make.
    gv_model = im_model.
    ENDIF.
    ENDMETHOD. "set_make
    ENDCLASS. "zl_lcl_vehicle
    Overloading means changing signature as well as implementation of a method.
    Overriding is changing only implementation of method with signature unchanged.
    From ABAP perspective, only the CONSTRUCTOR method can be overloaded in a subclass i.e both the signature and implementation can be adapted in subclass.
    Any other method can't be overloaded. It can only be redefined/overridden i.e implementation changed with signature unchanged.
    In ABAP  there is something called a redefinition.
    When you inherit a class from a super class, you can redifne a method. You cannot chnage the signature( Interface) of the method. It will remain the same as that of the super class.You must redefine a method in the same visibility section in which it appears in the superclass.
    Eg.
    CLASS C_SUPER_CLASS DEFINITION .
    PUBLIC SECTION.
    METHODS: DRIVE ,
    STOP.
    PROTECTED SECTION.
    DATA SPEED TYPE I.
    ENDCLASS.
    CLASS C_SUPER_CLASS IMPLEMENTATION.
    METHOD DRIVE.
    SPEED = 0.
    WRITE: / 'Bike speed =', SPEED.
    ENDMETHOD.
    ENDCLASS.
    CLASS C_SUB_CLASS DEFINITION INHERITING FROM C_SUPER_CLASS.
    PUBLIC SECTION.
    METHODS DRIVE REDEFINITION.
    ENDCLASS
    CLASS C_SUB_CLASS IMPLEMENTATION.
    METHOD DRIVE.
    SPEED = SPEED + 10.
    WRITE: / 'Bicycle speed =', SPEED.
    ENDMETHOD.
    ENDCLASS.
    Regards
    Vasu

  • Hiding and overriding clarifications

    I am trying to understand the Java language specification document,
    one of the worst technical docs I've seen in 35+ years in the business.
    Right now I'm trying to clarify the implications and meanings of these
    two terms (hiding and overriding) in a very simple environment. Maybe
    someone here can make it clear.
    Context is just basic classes with fields, initializers, constructors, and
    methods; no interfaces, no nested classes. Keep it clear and simple.
    When I create a subclass, I understand that fields and methods are
    inherited. I also understand how hiding of fields works, and how to
    access a hidden field from an external class.
    But methods. Sheesh! At first I thought maybe fields are hidden and
    methods are overridden. But, no, because there's 8.4.10.5 titled
    "Example: Invocation of Hidden Class Methods". So a method can
    be hidden, and a hidden method can be accessed.
    So, here are my questions:
    * What is the difference between overriding a method and hiding a method?
    * How can you tell / demonstrate a method is hidden or overridden?
    * If a method is overridden, is it always inaccessible from an external class?
    * Are there pitfalls to avoid with hiding / overriding methods?
    * Are there features to exploit with hiding / overriding methods?
    * Do instance methods have different behaviors / facilities than
    class methods, as far as hiding and overriding?
    Thanks.

    * What is the difference between overriding a
    method and hiding a method?Overriding a method means the subclass chooses not to use the parent class's implementation. Hiding a method I assume means you declare a method private so that a subclass cannot use it.
    * How can you tell / demonstrate a method is hidden
    or overridden?The modifier private will be applied to methods that are hidden. Overridden methods will have the same signature as the parent and are thus easy to spot as well.
    * If a method is overridden, is it always
    inaccessible from an external class?You're confused. Hidden/private methods affect visibility. Overriding a method doesn't change it's visibility so external classes may or may not be able to access it. It all depends on the access modifiers you've applied
    * Are there pitfalls to avoid with hiding /
    overriding methods?You'd have to provide a specific scenario for this question. This is too open-ended.
    * Are there features to exploit with hiding /
    overriding methods????
    * Do instance methods have different behaviors /
    facilities than
    class methods, as far as hiding and overriding?What you're talking about is static vs. instance methods. Google it, I'm done for now.

  • Is there another way of getting apps from the appstore without putting your credit card number in, ive heard about the itunes gift card thing can anybody just give me more info about that and how i can buy free things free things from the appstorepls help

    Is there another way of getting apps from the appstore without putting your credit card number in, ive heard about the itunes gift card thing can anybody just give me more info about that and how i can buy free things free things from the appstore...pls help as im only a teenager and have no credit credit and my parents dont trust me with theres and they dont care about the fact that you can set up a password/.... PLEASE SOMEONE HELP I WILL BE SO GRATEFUL... And i would really like to get the iphone 4 but if there is no way of etting apps without your credit number then i would have to get a samsung galaxy s3 maybe ...

    You can set up an Apple ID without a credit card.
    Create iTunes Store account without credit card - Support - Apple - http://support.apple.com/kb/ht2534

  • My homepage will load about 30% and then i'm dead in the water. when i close firefox it appears to close but when i try to reopen it tells me that it's already running. started with beta 6 i deleated and loaded ver 5 no change

    when i start firefox, everything starts normal then my homepage (igoogle) starts to load, gets to about 30% and stops. from there i can click on links or bookmark and a new tab will open but no page will load. i can't get anywhere. then when i close firefox it appears to close normally but when i try to reopen firefox i get a message that firefox is still running but not responding and the only way to shut it down is to reboot. when it first occured i was running beta 6 (since it became available, i also used beta 4 & beta 5) i then uninstalled beta 6 and downloaded and installed ver 5 same problem. i then did a system restore to a time before the problem.. no luck. i'm not sure what to try next. IE & chrome both work fine

    I'm in exactly the same boat - dead at blue screen, only option is to salvage using target mode.
    I don't currently have access to another Mac to do target mode. I was planning to buy a new Mac this fall anyway, though. 2 questions in preparation for that purchase:
    1) Is there anything I can do before buying a new Mac to make the salvage process more successful? ie, should I spend the time and money going to the genius bar to have them help me get from totally crippled to partially crippled? (As for expertise, I'm a proficient consumer-grade user, but Apple Support walked me through the safe-boot process, etc.)
    2) Is Apple going to provide 10.5 once it is released to people who buy a new Mac in these 6 weeks pre-release?
    Thanks for your help,
    Bailey

  • Firefox will open 4 or 5 tabs fine, but then will not load any further websites after those first 4 or 5, or allow you to refresh one of those first tabs -- including about:config and the addon page

    Firefox 5 worked fine. I installed Firefox 7, and when I ran it, tabs would just say "connecting to..." and hang. Restarting did not help. Websites open fine in IE and Chrome. Disabled all firewalls and antivirus, did not help. Uninstalled and reinstalled Firefox 5, everything worked fine again. This was using Vista 64-bit.
    Upgraded to Windows 7, uninstalled Firefox 5, installed Firefox 7, had same problem. Uninstalled Firefox 7 completely (including the profile information, I saved that information in another folder), restarted computer, and installed Firefox 7 using a completely clean profile. Did not install any add-ons, checked to make sure all plug-ins were up-to-date, and updated plugins.
    Now when I start Firefox, I can load 3 or 4 or 5 tabs fine -- after those first few tabs are open, I cannot open or refresh any other tabs -- including about:config and the add-on manager -- I have to restart Firefox. The hangup doesn't appear to be related to what websites I am attempting to open, but it looks like the number is the problem. I have run through all of the FAQ procedures, including changing the max number of network connections to 48, and the problem does not seem to go away.
    As a side note, I had this same problem when I tried to go from version 5 to version 6 as well. Version 5 is the most recent version that worked on my system.

    Can you try Aurora - download it from http://www.mozilla.org/en-US/firefox/channel/
    And let us know how it works.

  • Each time I open my computer, I get a full screen about Firefox and a toolbar that needs a yes or no answer. How do I get rid of this?

    I used to be able to press 1 button to get to my Google Home page, but now that I have downloaded the latest version of Mozilla Firefox, the "home page" is now information about Firefox and at the top of my computer, there is a question about security that needs me to say 'no' each time. I do not want to go through these steps every time I start my system.

    See these articles for some suggestions:
    *https://support.mozilla.org/kb/Firefox+has+just+updated+tab+shows+each+time+you+start+Firefox
    *https://support.mozilla.org/kb/How+to+set+the+home+page - Firefox supports multiple home pages separated by '|' symbols
    *http://kb.mozillazine.org/Preferences_not_saved

Maybe you are looking for