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.

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.

  • 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/

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

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

  • One of my kids changed my password, and now i can't access my mail accounts. Any ideas on how to override the password and set up a new one?

    one of my kids changed my password, and now i can't access my mail accounts. Any ideas on how to override the password and set up a new one?

    A mail account password or you login password?
    Which OS by the way?

  • I have an extensive aperture library on my computer's hard drive and I want to break it up into separate smaller libraries on external hard drives.  How do I take projects from one library and add them to another one?

    I have an extensive aperture library on my computer's hard drive and I want to break it up into separate smaller libraries on external hard drives.  How do I take projects from one library and add them to another one?

    Coastal,
    Frank gave you the exact answer to your question. 
    However, I would like to ask if you are indeed asking the right question.  Do you really want different libraries?  The implications are that you have to "switch" libraries to see what's in the others, and so that your searches don't work across all of your pictures?  If so, then you asked the right question.  If not, you may be more interested in relocating your masters to multiple hard drives so your library gets smaller, instead of breaking up the library.
    nathan

  • Insert Edit and Overwrite Edit Crashes This One Premiere Project

    Hi All,
    I'm working off of many premiere projects, and one crashes when I use the insert edit and override edit button. At first it was only if I used the keyboard command, but now it happens when using the mouse. I'm afraid the project file is corrupted.
    Has anyone come across this issue? The workarounds I have tried include copying and pasting sequences to a new project, copying contents of a sequence into a new sequence, and importing an XML to a new project (which I thought would work, but I got an error message: Premiere experienced a generic message).
    Any thoughts? I'm kind of stumped and don't want to lose a  few days rebuilding the project file.

    Thanks for the response, Mark. And giving the version makes sense...an obvious oversight on my part
    Anyway, I was running 2014 and I actually ended up uninstalling and then reinstalling. Guessing that I was at 2014.0.0, but now I have 2014.0.1.  Since doing that, I haven't experienced the crashes. And, yes, it was with merged clips.
    HOWEVER, I still can't target audio only and insert/overwrite merged clips without jumping through a couple hoops. Works fine if I'm targeting video only with merged clips, or if I'm targeting audio only with any kind of clip (merged or non-merged). But audio only with merged? No deal.
    In order to make that work, I have to go to the merged clip in the project window, select Modify, and then futz with the the audio channels. See screen shot below. Audio tracks 1 and 2 are from my DSLR. Tracks 3 and 4 are my "good" audio. Is this something that I should need to do for each merged clip to make them work?

  • 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

  • Our Band purchased Logic Pro and it was loaded to one member's Macbook Pro. Unfortunately, he passed away with cancer in May. How can we transfer the ownership and the software (it was downloaded) to a new user's Macbook?

    Our Band purchased Logic Pro and it was loaded to one member's Macbook Pro. Unfortunately, he passed away with cancer in May. How can we transfer the ownership and the software (it was downloaded) to a new user's Macbook?

    Hi Kurt,
    The Mac IIci is not even powering on at all. Tried again with wih a tested power cable and no luck. 
    I think it's best that take  this issue to the Older Hardware Community. Not only did I see a fair number of replacement parts for the IIci avaiable online, but there also vintage external floppy drives as well. I'm not giving up.
    Thank you for your time and interest in helping.

  • Extending classes and overriding methods

    If I have a class which extends another, and overrides some methods, if I from a third class casts the extending class as the super class and calls one of the methods which gets overrided, is it the overrided method or the method of the superclass which get executed?
    Stig.

    Explicit cast can't enable the object to invoke super-class's method. Because dynamic binding is decided by the instance. The cast can just prevent you from using methods only defined in sub-class, but it does not let the object to use super-class's method, if the method's been overrided. Otherwise, the polymophism, which is one of the most beautiful feature of Object-Oriented thing, can't be achieved. As far as I know, the only way to use super-class's method is the super keyword in the sub-class, and seems no way for an object to do the same thing (I may wrong, since I haven't read the language spec).
    here's a small test:
    public class Test {
    public static void main(String[] args){
    A a = new B();
    a.method();
    ((A)a).method();
    class A{
    public void method(){
    System.out.println("A's method");
    class B extends A{
    public void method(){
    System.out.println("B's method");
    The output is:
    B's method
    B's method

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

Maybe you are looking for

  • Macbook Pro can't can't connect to server

    Disclaimer: I have been trying to solve this issue for days. I've tried taking different steps to solve this issue, however I am not the most computer savvy. I'm having multiple issues that I believe are all related to the same unknown problem. Pleas

  • Mysterious formatting columns table

    Hi all, i don't post a problem, but a curiosity. I'm managing a Oracle BI environment developed by others, so many aspects are unknown for me. I've observed that if i make an Answers request dragging and dropping any columns from my subject area, any

  • Ideapad U330 - How to install graphic driver XP?

    Hello, I installed Windows XP on the ideapad U330 (20001) and I updated to ServicePack3; afterwards I tried to install the graphic driver, but there is an error message, that it is not insalled; also the bluetooth driver and the CIR driver do not wor

  • Flipping image left to right

    I am trying to flip photos left to right. I know how to rotate but can't seem to flip for mirror image. Any ideas?

  • How to resume downloads without starting a fresh?

    how to resume downloads without starting a fresh to down load the same file? i want a way that it can continue from where it was interms of big file size.