Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference

 
chinnanu chinna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is differnce between follwing:

goFast() { }

and

goFast();



thank you
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic