problem in java programming

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().

Replies

  • sushant005
    sushant005
    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.
  • anandkumarjha
    anandkumarjha
    Hello CEans
    What is the difference between System.out.println() and System.out.write()???

You are reading an archived discussion.

Related Posts

Auto Mall 2010 will feature some of the best Automobile brands in the country along with related products and services. This will be further complemented by Premium Lounge for Luxury...
Thank you for accepting me in your forum! We are an e-Learning Solutions company and I'm looking forward in meeting online marketing enthusiasts here. Have a great day!😀 Regards,
Hai Friends our college is organizing a tech fest..i am an ece student..so i wish to present a paper based on electronics..so i am requesting u r help for suggesting...
while opening a particular project i am getting a error as "error during load refer to (a particular path where my project is stored)".What is this error about??????Not able to...
my college has asked us to write some research topics related to our branch.,,, so plz guyzz.,, what to write about that ,,, help me !!!