CrazyEngineers
  • problem in java programming

    sushant005

    sushant005

    @sushant005-tyt4WK
    Updated: Oct 26, 2024
    Views: 1.2K
    class a
    { }

    class c extends a
    {
    c(){System.out.println("class B");}
    public void toString()
    {
    String g="hello";
    System.out.println(g);

    }

    public static void main(String [] arg)
    {
    a d=new a();
    System.out.println(d);

    c y=new c();
    //System.out.println(d);
    System.out.println(y);

    }
    }
    Here is my above program in which i am facing problem.
    Here in the above program if we create an object of class a and simply print it then it shows a@1888759 as the output it means that it calls to String() from the Object class and returns the text representation of object of class a but i want to override toString() to print my own text i.e hello here but while executing it gives an error.
    So how to override the toString().
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • sushant005

    MemberSep 27, 2010

    oh i got the answer
    here is my code.
    class a
    {

    }
    class c extends a
    {
    c(){System.out.println("class B");}
    public String toString()
    {
    String g="hello";
    //System.out.println(g);
    return g;
    }

    public static void main(String [] arg)
    {
    a d=new a();
    System.out.println(d);

    c y=new c();
    //System.out.println(d);
    System.out.println(y);

    }
    }
    i was just making silly mistake.
    Here i have to give the return type String and have to return the string.
    Are you sure? This action cannot be undone.
    Cancel
  • anandkumarjha

    MemberSep 28, 2010

    Hello CEans
    What is the difference between System.out.println() and System.out.write()???
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register