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.

Array Declarations (Objective 1.3)

Arrays can hold primitives or objects, but the array itself is always an object.
When you declare an array, the brackets can be to the left or right of the variable name.
It is never legal to include the size of an array in the declaration.
An array of objects can hold any object that passes the IS-A (or instanceof) test for the declared type of the array. For example, if Horse extends Animal,
then a Horse object can go into an Animal array.

Variable Declarations (Objective 1.3)

Instance variables can
  1. Have any access control
  2. Be marked final or transient
Instance variables can't be abstract, synchronized, native, or strictfp.
It is legal to declare a local variable with the same name as an instance variable; this is called "shadowing."
  1. final variables have the following properties:
  2. final variables cannot be reinitialized once assigned a value.
  3. final reference variables cannot refer to a different object once the object has been assigned to the final variable.
  4. final reference variables must be initialized before the constructor completes.
There is no such thing as a final object. An object reference marked final does not mean the object itself is immutable.
The transient modifier applies only to instance variables.
The volatile modifier applies only to instance variables.

Methods with var-args (Objective 1.4)

As of Java 5, methods can declare a parameter that accepts from zero to many arguments, a so-called var-arg method.
A var-arg parameter is declared with the syntax type... name; for instance: doStuff(int... x) { }
A var-arg method can have only one var-arg parameter.
In methods with normal parameters and a var-arg, the var-arg must come last.

Other Modifiers—Members (Objective 1.3)

final methods cannot be overridden in a subclass.
abstract methods are declared, with a signature, a return type, and an optional throws clause, but are not implemented.
abstract methods end in a semicolon—no curly braces.
Three ways to spot a non-abstract method:
  1. The method is not marked abstract.
  2. The method has curly braces.
  3. The method has code between the curly braces.
The first nonabstract (concrete) class to extend an abstract class must implement all of the abstract class' abstract methods.
The synchronized modifier applies only to methods and code blocks.
synchronized methods can have any access control and can also be marked final.

abstract methods must be implemented by a subclass, so they must be inheritable. For that reason:
  1. abstract methods cannot be private.
  2. abstract methods cannot be final.
  3. The native modifier applies only to methods.
  4. The strictfp modifier applies only to classes and methods.

Local Variables (Objective 1.3)

Local (method, automatic, or stack) variable declarations cannot have access modifiers.
final is the only modifier available to local variables.
Local variables don't get default values, so they must be initialized before use.

Member Access Modifiers (Objectives 1.3 and 1.4)

Methods and instance (nonlocal) variables are known as "members."
Members can use all four access levels: public, protected, default, private.
Member access comes in two forms:
Code in one class can access a member of another class.
A subclass can inherit a member of its superclass.
If a class cannot be accessed, its members cannot be accessed.
Determine class visibility before determining member visibility.
public members can be accessed by all other classes, even in other packages.
If a superclass member is public, the subclass inherits it—regardless of package.
Members accessed without the dot operator (.) must belong to the same class.
this. always refers to the currently executing object.
this.aMethod() is the same as just invoking aMethod().
private members can be accessed only by code in the same class.
private members are not visible to subclasses, so private members cannot be inherited.

Default and protected members differ only when subclasses are involved:
Default members can be accessed only by classes in the same package.
protected members can be accessed by other classes in the same package, plus subclasses regardless of package.
protected = package plus kids (kids meaning subclasses).
For subclasses outside the package, the protected member can be accessed only through inheritance; a subclass outside the package cannot access a protected member by using a reference to a superclass instance (in other words, inheritance is the only mechanism for a subclass outside the package to access a protected member of its superclass).
A protected member inherited by a subclass from another package is not accessible to any other class in the subclass package, except for the subclass' own subclasses.

Interface Implementation (Objective 1.2)

  • Interfaces are contracts for what a class can do, but they say nothing about the way in which the class must do it.
  • Interfaces can be implemented by any class, from any inheritance tree.
  • An interface is like a 100-percent abstract class, and is implicitly abstract whether you type the abstract modifier in the declaration or not.
  • An interface can have only abstract methods, no concrete methods allowed.
  • Interface methods are by default public and abstract—explicit declaration of these modifiers is optional.
  • Interfaces can have constants, which are always implicitly public, static, and final.
  • Interface constant declarations of public, static, and final are optional in any combination.
  • A legal nonabstract implementing class has the following properties:
  1. It provides concrete implementations for the interface's methods.
  2. It must follow all legal override rules for the methods it implements.
  3. It must not declare any new checked exceptions for an implementatoin method.
  4. It must not declare any checked exceptions that are broader than the exceptions declared in the interface method.
  5. It may declare runtime exceptions on any interface method implementation regardless of the interface declaration.
  6. It must maintain the exact signature (allowing for covariant returns) and return type of the methods it implements (but does not have to declare the exceptions of the interface).
  • A class implementing an interface can itself be abstract.
      • An abstract implementing class does not have to implement the interfacemethods (but the first concrete subclass must).
      • A class can extend only one class (no multiple inheritance), but it can implement many interfaces.
      • Interfaces can extend one or more other interfaces.
      • Interfaces cannot extend a class, or implement a class or interface.
      • When taking the exam, verify that interface and class declarations are legal before verifying other code logic.

Monday, October 20, 2008

Class Modifiers (Nonaccess) (Objective 1.2)

  • Classes can also be modified with final, abstract, or strictfp.
  • A class cannot be both final and abstract.
  • A final class cannot be subclassed.
  • An abstract class cannot be instantiated.
  • A single abstract method in a class means the whole class must be abstract.
  • An abstract class can have both abstract and nonabstract methods.
  • The first concrete class to extend an abstract class must implement all of its abstract methods.

Sunday, October 19, 2008

Class Access Modifier (Objective 1.1)

  • There are three access modifiers: public, protected, and private.
  • There are four access levels: public, protected, default, and private.
  • Classes can have only public or default access.
  • A class with default access can be seen only by classes within the same package.
  • A class with public access can be seen by all classes from all packages.
  • Class visibility revolves around whether code in one class can create an instance of another class extend (or subclass), another class
  • Access methods and variables of another class

Declaration Rules

Declaration Rules (Objective 1.1)
  • A source code file can have only one public class.
  • If the source file contains a public class, the filename must match the public class name.
  • A file can have only one package statement, but multiple imports.
  • The package statement (if any) must be the first (non-comment) line in a source file.
  • The import statements (if any) must come after the package and before the class declaration.
  • If there is no package statement, import statements must be the first (noncomment) statements in the source file. package and import statements apply to all classes in the file.
  • A file can have more than one nonpublic class.
  • Files with no public classes have no naming restrictions.

Identifiers

Identifiers (Objective 1.3)
  • Identifiers can begin with a letter, an underscore, or a currency character.
  • After the first character, identifiers can also include digits.
  • Identifiers can be of any length.
  • JavaBeans methods must be named using camelCase, and depending on the method's purpose, must start with set, get, is, add, or remove.

Wednesday, October 1, 2008

JavaBeans Standards

JavaBeans are the Java classes that have private properties. JavaBeans method standards are the standard way to access the properties of JavaBean classes.
We need to know the JavaBeans standard for SCJP in order to select the most suitable naming to access the private properties.

JavaBean Property Naming Rules:
- get/set for the property which is not Boolean type
- get/set or is/set for the Boolean type property
- get/set methods must be public

For example:

public class Foo {
private int iValue;
private boolean bValue;
private String sValue;

public int getIValue() {
return iValue;
}
public void setIValue(int value) {
iValue = value;
}
public boolean isBValue() {
return bValue;
}
public void setBValue(boolean value) {
bValue = value;
}
public String getSValue() {
return sValue;
}
public void setSValue(String value) {
sValue = value;
}

}

Saturday, September 27, 2008

Java Key Words

This is the complete list of java key words. There are some others important tips for keyword are as follows:

> Keywords use letters only; they do not use special characters (such as $, _, etc.) or digits.

> All keywords are in all lowercase letters.

> Watch out for keywords that are from languages other than Java, such as friend, include, and unsigned, which are not keywords in Java.

> The words goto and const are reserved to the extent that programmers cannot use them, but they have no specific meaning in the Java language.


We've to remember those rules for this kind of questions:

Which are valid declarations? (Choose all that apply.)
A. int $x;
B. int 123;
C. int _123;
D. int #dim;
E. int %percent;
F. int *divide;
G. int central_sales_region_Summer_2005_gross_sales;

Introduction to SCJP


SCJP, Sun Certified Java Programmer, is the first level of java certified program. Even though, it's the entry level exam, it covers all java programming language knowledge start from sample condition manipulation until the multi thread handling.


Refer to Sun official web site, there are 4 levels of Java Certificate Program start from SCJP. Without SCJP, you can't go further and it's required to pass SCJP first.


Since my dream is to become the Java Architect, SCJP is a must for me to start on. I was planning to take SCJP since two years after of my java experience. I was gathering all the resources for SCJP since then. I've read most of the topic and practice questions and answers. However, due to the time and other interruptions, until now I haven't done yet.


My objective of having this blog is to help myself to set up the goal to get SCJP as well as helping others to get all I have done so far.


Please kindly share me and guide me if you have found some other useful tips for SCJP.