aspose file tools*
The moose likes Beginning Java and the fly likes Class Default Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Default Method" Watch "Class Default Method" New topic
Author

Class Default Method

Mike Dennison
Greenhorn

Joined: Jun 01, 2003
Posts: 4
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
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
... 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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mike Dennison
Greenhorn

Joined: Jun 01, 2003
Posts: 4
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class Default Method