Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A few really simple questions

 
Christopher Burns
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi gang,

These are really simple memory refreshers just so I don't have to spend time looking around that I don't have right now...

First, does the Java language spec MANDATE interfaces? Or is this just convention?

Second, are accessors (can you tell I am a Smalltalker?) conventionally written as getXXX and setXXX or is this a language construct that must be adhered to?

Third, is it just convention that interfaces are named the actual noun, and the implementation class is <interface name>Impl? As in Animal and AnimalImpl. Or, again, is that some sort of language construct?

Yes or no answers would be great, unless there are details that need to be addressed. Please don't feel like you have elaborate.

Cheers,

Chris
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are all just conventions.

However the getXXX() and setXXX() convention is widely used and is in fact assumed in a lot of support software. JSTL is one example.

And the last one (Animal and AnimalImpl) is somewhat of an anti-pattern. It's possible (and completely normal) to write interfaces that can be implemented by many classes. That naming convention (at least to me) presupposes that the interface will be implemented by exactly one class.
 
Paul Campbell
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Christopher Burns:
Hi gang,

These are really simple memory refreshers just so I don't have to spend time looking around that I don't have right now...

First, does the Java language spec MANDATE interfaces? Or is this just convention?

No, it isn't mandated but it is a way to insure that classes that do not inherit from the same superclass have implemented certain methods.

Originally posted by Christopher Burns:

Second, are accessors (can you tell I am a Smalltalker?) conventionally written as getXXX and setXXX or is this a language construct that must be adhered to?


It is a JavaBean standard and part of the JavaBean specs, but there is nothing to force you to do it in your code... unless you are creating Java Components to be shared in an IDE or something that would enforce the JavaBean spec.

Originally posted by Christopher Burns:

Third, is it just convention that interfaces are named the actual noun, and the implementation class is <interface name>Impl? As in Animal and AnimalImpl. Or, again, is that some sort of language construct?


There are certain best practices/standards programmers try to adhere to... and if everyone does it your code will be easier to read... but nothing to enforce it (and interfaces should be an adjective).

Classes should be nouns (i.e., Object, String)
Interfaces should be adjectives (i.e., Runnable)

Methods should typically be verb-noun pairs (i.e.,getCustomerName, setCustomerName)

Variables should be short meaningful names (customerName, depositAmount)

Constants should be uppercase letters with underscore characters (i.e., MAX_WITHDRAWAL_AMOUNT).

I'm sure I may of missed something... but I'm all sure CR will come through and correct me where I glossed over or mistated anything.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch. Please use a meaningful subject line when you post a question.
 
Christopher Burns
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Thanks for the input. Just wanted to make sure there was no compiler expectations.

Jesper: Sorry about that. Couldn't figure out how to make it more specific, but I guess I could have just used one of the questions as a starter.

Cheers,

Chris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic