Results 1 to 2 of 2
  1. #1
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    157
    Rep Power
    5

    Default same object variable t and same methods is calling

    Hi,
    What is the difference between method overloading and method overriding please explain with examples.

    then what is this overloading or overriding
    class Test{

    public void print(String val){

    System.out.println("the value is "+val);

    }

    }

    class Checks{
    public static void main (String[] args) {
    Test t=new Test();
    t.print("ddd");
    t.print("SDFSDFSDF");
    t.print("asdasdad");
    }

    }

    the output will be
    ddd
    SDFSDFSDF
    asdasdad

    and why these are getting means
    same object variable t and same methods is calling so the last asdasdad has to get but the 3 output are getting why
    please explain

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    overloading is when you have the same method name and different return type, or paramaters

    public void print(string s)
    public int print(int i)

    those are the same method, print(), but it is "overloaded"

    overriding:

    Java Code:
    class Test {
    public void print(String s)
    {
    System.out.println(s);
    return;
    }
    
    public int print(int i)
    {
    System.out.print("" + i);
    return 1;
    }
    
    class Test1 extends Text
    {
    public void print(String s)
    {
    System.out.println("The string is: " + s);
    }
    public static void main(String args[])
    {
    Test1 one = new Test1();
    one.print("something");
    }
    The output would be:

    The string is: something

    In this example, there are two methods that are "void print(String)"
    one is in class Test, and the other Test1. the print() in Test1 is overriding the print() in Test.

Similar Threads

  1. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum Advanced Java
    Replies: 3
    Last Post: 01-24-2009, 12:58 PM
  2. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 05:10 AM
  3. Calling a variable from main to another class
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 03:35 PM
  4. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:22 AM
  5. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 09:53 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
  •