wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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.
 
wood burning stoves
 
subject: difference
 
Similar Threads
Abstract class members
Figure 1-4 on SCJP Book incomplete resp. redundant
Protected members -- Access through inheritance
Protected acces override
Inner Classes