Friday, October 24, 2008

Polymorphism

Any Java object that can pass more than one IS-A test can be considered polymorphic.
Other than objects of type Object, all Java objects are polymorphic in that they pass the IS-A test for their own type and for class Object.
Remember that the only way to access an object is through a reference variable, and there are a few key things to remember about references:
  • A reference variable can be of only one type, and once declared, that type can never be changed (although the object it references can change).
  • A reference is a variable, so it can be reassigned to other objects, (unless the reference is declared final).
  • A reference variable's type determines the methods that can be invoked on the object the variable is referencing.
  • A reference variable can refer to any object of the same type as the declared reference, or—this is the big one—it can refer to any subtype of the declared type!
  • A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface.
In simple terms, polymorphism is the ability of one type, A, to appear as and be used like another type, B. In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B.

Object-Oriented Design

IS-A and HAS-A relationships and encapsulation are just the tip of the iceberg when it comes to object-oriented design.
The reason for the emphasis on proper design is simple: money.
The cost to deliver a software application has been estimated to be as much as ten times more expensive for poorly designed programs.
Having seen the ramifications of poor designs, I can assure you that this estimate is not far-fetched.
Even the best object-oriented designers make mistakes. It is difficult to visualize the relationships between hundreds, or even thousands, of classes. When mistakes are discovered during the implementation (code writing) phase of a project, the amount of code that has to be rewritten can sometimes cause programming teams to start over from scratch.
The software industry has evolved to aid the designer. Visual object modeling languages, like the Unified Modeling Language (UML), allow designers to design and easily modify classes without having to write code first, because object-oriented components are represented graphically. This allows the designer to create a map of the class relationships and helps them recognize errors before coding begins. Another innovation in object-oriented design is design patterns.

Designers noticed that many object-oriented designs apply consistently from project to project, and that it was useful to apply the same designs because it reduced the potential to introduce new design errors. Object-oriented designers then started to share these designs with each other. Now, there are many catalogs of these design patterns both on the Internet and in book form.
Although passing the Java certification exam does not require you to understand objectoriented design this thoroughly, hopefully this background information will help you better appreciate why the test writers chose to include encapsulation, and IS-A, and HAS-A relationships on the exam.

[- Jonathan Meeks, Sun Certified Java Programmer]

Thursday, October 23, 2008

Static Variables and Methods (Objective 1.4)

They are not tied to any particular instance of a class.
No classes instances are needed in order to use static members of the class.
There is only one copy of a static variable / class and all instances share it.
static methods do not have direct access to non-static members.