Results 1 to 6 of 6
  1. #1
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default Overloading or Overriding...

    Hi, i'm consufed with this...
    below code class Tazan has overriden and overloaded methods, (it has both)
    Bt when it is called with reference variable, i don't get the expected output..

    My idea WAS, overloading reference type and overridding actual object type..

    Please help me to verify this Thanks a lot

    Output of following prgram is Animal


    class Animal{

    public void method(Animal A)
    {

    System.out.println("Animal");

    }

    }

    class Dog extends Animal{

    public void method(Dog d)
    {

    System.out.println("Dog");

    }


    }


    class Tazan extends Dog{


    public void method(Animal t)
    {

    System.out.println("Animal");

    }


    public void method(Tazan t)
    {

    System.out.println("Tazan");

    }


    }

    class Test{

    public static void main (String args[]){

    Animal t=new Tazan();
    t.method(t);



    }

    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default

    All that the compiler knows is that 't' is an Animal. It calls the method(Animal ...) method. That method is overridden so the method(Animal ...) method in the Tazan class is called. Note that overloading is the responsibility of the compiler, overriding is runtime business.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    Hi, is that mean, if we have both overloaded and over-ridden methods (like Tazan class)
    overloading will happen! am i correct?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by chathura87 View Post
    Hi, is that mean, if we have both overloaded and over-ridden methods (like Tazan class)
    overloading will happen! am i correct?
    I don't understand your question but I write it again: overloading is a compiler issue while overriding is done at runtime. If the compiler can't find an appropriate method it will generate an error message. Overriding always works because it has passed all compiler checks.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    overloading depends on different arguments. For examples, you could have a method that adds 2 integers, and then overload it to work on doubles and floats
    Java Code:
    public void add(int x, int y){
      System.out.println(x + y);
    }
    public void add(double x, double y){
      System.out.println(x + y);
    }
    public void add(float x, float y){
      System.out.println(x + y);
    }
    In your code you do
    Java Code:
    Animal t = new Tazan();
    The t is of type animal so it will apply the method which takes an animal, since animal is also overridden it uses the more specific method.

    Also, since you have an overridden method in tazan, try changing the animal method to print out "Tazen, Animal" This way you can tell which version of the method is being called, and this can help you understand what is happening.
    Last edited by sunde887; 02-21-2011 at 05:22 PM.

  6. #6
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

Similar Threads

  1. Method Overloading - Doubt
    By vidya lakshman in forum New To Java
    Replies: 4
    Last Post: 02-01-2011, 06:47 AM
  2. overloading
    By Bhuvan in forum Advanced Java
    Replies: 6
    Last Post: 01-02-2011, 09:15 AM
  3. Overloading qn
    By diskhub in forum New To Java
    Replies: 18
    Last Post: 06-07-2010, 05:19 PM
  4. Overloading abstract methods??!?
    By tfitz666 in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 09:45 AM
  5. Overloading?
    By padutch2 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 03:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •