• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Class Default Method

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whilst working through a Java tutorial I noticed that the File class has a default method
i.e.
File myFile = new File("c:/dir1/myfile.txt");
System.out.println("My file is " + myFile);
displays
My file is c:\dir1\myfile.txt
Presumably toString() is the "default" method for the File class.
Q1. Do all classes have "default" methods?
Q2. As all class inherit from Object is toString() always the "default" method?
Q3. If not how do you specify a "default" method.
Mike Dennison
slightly confused in Woodford Green
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike,
Welcome to JavaRanch. All java classes have a default toString() method since they inherit it from the Object class. In the case of the File class though, toString() is not the default version but an overloaded version defined in the File class.
Try this:

Here's the output:

Note that the Object version of toString() prints the class and the memory position for the object reference.
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and, toString() isn't really a "default" method. The definition of the "+" operator when used for String concatentation specifies that a class' toString() method be called to convert the operand to a string so that it can be operated upon. Outside of this operation, there is no such concept of a "default" method.
hth,
bear
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and, toString() isn't really a "default" method. The definition of the "+" operator when used for String concatentation specifies that a class' toString() method be called to convert the operand to a string so that it can be operated upon. Outside of this operation, there is no such concept of a "default" method.
More specifically, a StringBuffer is created to concatenate the Strings by calling the appropriate append() method. To spare any confusion to someone making their first post in the Beginner forum, I saw no point in getting into a discussion on semantics. In my mind the question clearly dealt with polymorphism regarding inherited methods. But I would also argue that in some contexts the term default is used to describe a method defined in a super class that may be overridden. For example, all of the abstract Adapter classes in the java.awt.event package have empty default implementations for each event type so that subclasses can override only those events that they are interested in.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what is in fact happening is that the compiler translates
System.out.println("My file is " + myFile);
into
System.out.println(new StringBuffer().append("My file is ").append(myFile).toString());
The append method as overloaded for Objects (which gets called with myFile as parameter) calls String.valueOf(myFile), which in turn calls myFile.toString() if myFile is not null.
 
Mike Dennison
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow! I'm impressed - so many excellent replies - so quickly. That really helps me a lot.
If I may ask one further question. Ilja mentioned how the java compiler processes the "+" operator in println statements, where's the best place to read up on this sort of stuff?
Mike
 
reply
    Bookmark Topic Watch Topic
  • New Topic