Sunday, June 29, 2008

SCJP Exam - Objective 1.1

Develop code that declares classes (including abstract and all forms of nested classes), interfaces, and enums, and includes the appropriate use of package and import statements (including static imports).

Java Source File
Sample Java source file:



You can download source code here.
  1. If the class is part of a package, the package statement must be the first in the source code file.
  2. The import statements must be after the package statement (if there is one) and must be before the class declaration.
  3. There can be only one public class per source code file.
  4. If there is a public class in a file, the name of the file must match the name of the public class.
  5. A source file can have more than one nonpublic class.
  6. Files with no public classes can have a name that does not match any of the classes in the file.
Static import

The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:

import static java.lang.Math.PI;
or:
import static java.lang.Math.*;
Once the static members have been imported, they may be used without qualification:
double r = cos(PI * theta);
The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.

Class declaration
Class declaration looks like this:
<Class modifier> class <ClassName> {
//...
}
Modifiers
Access modifiers
  1. A class can be declared as public and default access.
  2. A class never can be declared as private or protected, except the inner classes.
Non-access modifiers
  1. abstract
  2. final
  3. strictfp

Non-access modifier: abstract
An abstract class can never be instantiated. If you try it, you get Compilation fails. The abstract classes are to extend them.

If we have some classes with the same methods, but some method implementation are different then abstract class is a perfect choice:
Make an abstract class, write the methods, that will be same in the subclasses.
Mark the methods, that will have different implementations as abstract. Abstract methods end in a semicolon rather than curly braces.

Example for abstract class and its concrete classes:

You can download source code here.


Non-access modifier: final
When used in a class declaration, the final keyword means the class can't be subclassed. In other words, no other class can ever extend a final class, and any attempts to do so will give you a compiler error.

Many classes in the Java core libraries are final. For example, the string class. So notice, you never ever can extend the String class.

Example for final class and the Compilation fails when try to extend the class, marked final:

You can download source code here.


Non-access modifier: strictfp
Marking a class as strictfp means that any method code in the class will conform to the IEEE 754 standard rules for floating points. Without that modifier, floating points used in the methods might behave in a platform-dependent way.

Notice,
  • an abstract class never ever can be instantiated
  • abstract class can have constructor and it can be called from subclasses constructors trough super()
  • a final class never ever can be extended
  • a class never ever can be marked as both abstract and final
Declaring nested classes
The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:
class OuterClass {
//...
class NestedClass {
//...
}
}
Type of nested classes:
  1. static nested class
  2. inner (non-static) class
  3. local class
  4. anonymous class
Static nested class
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.

Static nested classes are accessed using the enclosing class name:

OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use this syntax:
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
Inner class
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

class OuterClass {
//...
class InnerClass {
//...
}
}

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

Local inner class
You can declare an inner class within the body of a method. Such a class is known as a local inner class:
class OuterClass {
public void someMethod() {
class InnerClass {
//..
}
}
}
Notice,
  1. a local inner class can be instantiated only within the method where the inner class is defined
  2. a local inner class can not access to the method's local variables
  3. a local inner class can access to the method's local variables, marked as final

Anonymous class
Anonymous classes extends an exists class or implements an interface, with creating a new class without name:
class Tree {
public void grow() {
//Some tree specific code
}
}

interface Door {
void closeDoor();
}

public class MyClass {
public void doStuff() {
Tree oak = new Tree {
public void grow() {
//Some oakvspecific code
}
}

Door door = new Door {
public void closeDoor() {
//Close door method
}
}
}
}



Interface declaration

Interface declaration looks like this:
<Class modifier> interface <InterfaceName> {
//...
}
Notice,
  • all interface methods are implicitly public and abstract. In other words, you do not need to actually type the public or abstract modifiers in the method declaration, but the method is still always public and abstract
  • all variables defined in an interface must be public, static, and final—in other words, interfaces can declare only constants, not instance variables

  • interface methods must not be static

  • because interface methods are abstract, they cannot be marked final, strictfp, or native

  • an interface can extend one or more other interfaces

  • an interface cannot extend anything but another interface

  • an interface cannot implement another interface or class

  • an interface must be declared with the keyword interface

  • interface types can be used polymorphically

Puzzle 1
Given the following code:
interface Tree {
void grow();
}

public class Oak implements Tree {
void grow() {
//Some oak specific code here...
}
}
What is the result?
The result is: the code does not compile, it causes Compilation fails, because methods in interface declaration are implicitly public and the accession of grow() method in Oak class is default.

Puzzle 2
Given the following code:
interface MyInterface {
int x = 67;
}

class MyClass implements MyInterface {
public void setter() {
x = 100;
}
}
What is the result?
The result is: the code does not compile, it causes Compilation fails, because variables, declared in interfaces are implicitly final and static, and you can not assign a new value to a final variable.

Declaring Enums
Using enums can help reduce the bugs in your code.

Example for declaring enumerations:

You can download source code here.

2 comments:

Unknown said...

Nice Post. Its really Helpful to me. Thanks a lot :)

Unknown said...

"And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference."

above is true only for non-static fields and methods, static members can be accessed directrly