This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference" Watch "difference" New topic
Author

difference

chinnanu chinna
Greenhorn

Joined: May 28, 2007
Posts: 10
what is differnce between follwing:

goFast() { }

and

goFast();



thank you
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
void goFast() { } is a complete method in itself, it is indicated by the curly braces { }, although there is no code inside(it can be added later).

Whereas, void goFast(); cannot be used just as is. The semi-colon at the end instead of curly braces means this method does not have/cannot have code. When used in a class, it MUST be marked abstract such as


Java states that the class which has even one single abstract method must be itself declared abstract, else it won't compile. In this case, the first concrete subclass of this class is required to write the code for this method(Again, even it is void goFast() { }, it is legal, although it doesn't make sense to have nothing in the method)

void goFast() { } can be used with interface declaration, where the abstract keyword is optional (all methods in an interface are abstract and public by default). Again, the concrete class implementing this interface needs to compulsarily write the method body.
eg.


Hope that helps.
Vidhya
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Sorry, the above post has some correction in one line ( goFast(); to be used instead of goFast() {}).

Here is the correction:

void goFast(); can be used with interface declaration, where the abstract keyword is optional (all methods in an interface are abstract and public by default). Again, the concrete class implementing this interface needs to compulsarily write the method body.
 
GeeCON Prague 2014
 
subject: difference