Wednesday, December 7, 2011

Declaration - Part 2

Welcome back to the second article about OCJP(1Z0-851), hope everyone liked the first one, so the second article keep the same methodology it's give an overview of the objectives of OCJP.

"Declare Interface" - ( Exam Objectives 1.1 and 1.2)

An interface defines a "contract" for all the classes that want to implement it, all the java classes can implement an interface, by using an interface we can make different classes have things in common.

Look at an example below:



All the interface's methods declared in an interface are public abstract, think of an interface as a 100% abstract class, also all the interface's methods use semicolon at the end of declaration.

- All the interface methods are implicitly public and abstract which means we don't need to type these modifiers in the method declaration.
- All the variables defined in an interface must be public, static and final, we can't declare variable instances.

Other rules:

- Interface methods must not be static
- Because interface methods are abstract, they cannot be marked final, strictfp or native.
- An interface can extends one or more interfaces.
- An interface can only extends the other interfaces.
- An interface cannot implement anything.
- Always use the keyword interface to declare it.
- By defining an interface we get more flexibility to work polymorphically.

Legal Interface Declarations:



Both declarations are correct, the first one is using the modifier public which is not necessary because the interfaces are considered abstract by default, the access modifier public is used to make the interface visible everywhere, if we don't type public the interface has a default access.


Interface method declarations:


All the declarations above are legal and identical , when we mark an interface method as public and abstract it's redundant.

Declaring Interface constants:

We can declare interface constants which means any class that implements an interface has access a constant, however when you declare a constant usually we mark it as public, static and final but as methods declared in an interface constants are implicitly public, static and final.


Any combination of modifiers above is legal as said last time constants inside an interface are public, static and final implicitly.
Look at the method fly() inside the Duke class, once a value of constant defined we cannot change the value.


"Declare Class Members" - ( Exam Objectives 1.3 and 1.4)

We've looked at classes declaration before and as you remember the only access modifiers allowed for classes are public and default, but now we're talking about members of classes, we considered members methods and instance variables( no local variables) and those have the similar behavior when marked with access modifiers .
Unlike classes, members can use the following access modifiers .

  • public
  • protected
  • default
  • private
For example if a class Car has access to members of Engine class it means the class Engine is visible to class Car, when we try to access a class that is not "visible" the compiler will slap you.
There are two different access issue:

  • A method of one class can access a member of another class
  • A subclass can inherit a member of its superclass



Important: If a class A can't be accessed by class B no members within class A can be accessed by class B.
First of all ensure that the access level of class is visible to another class otherwise no member is visible either even though the member has a public access. Once you've confirmed that the class is visible, then it makes sense to look at access levels on individual members.

Public Members

Public methods and variables are visible to all other classes, regardless of the package they belong to( the access level of class must be visible as well)

Comparison of inheritance vs. dot operator for member access.

There are three ways to invoke a method:

  1. Invoking a method declared in the same class
  2. Invoking a method using a reference of the class
  3. Invoking an inherited method


The code above compiles well because the class Printer is public as well as it's method printAll(), so Computer class has access to Printer.
For a subclass, if a member of superclass is declared public, the subclass inherits that member even both classes are declared in different packages.



Notice there are no import statements, the method print is allowed in class Computer because Computer extends Device and print is public, which means print belongs to Computer.



Private Members

When we mark a member as private it can't be accessed by any class, just inside the class that declared a member.

Notice, printAll() is marked as private now, any attempt to invoke this method results in a error of compilation.




Monday, December 5, 2011

Declaration - Part 1

Introduction

Welcome to the first article about OCJP(1Z0-851), the main goal here is help who is planning to get the OCPJ (Oracle Certified Java Programmer), to get a better understanding the articles will be split up into sessions that can have one or more subjects depending on the complexity of that.
To provide these articles the book SCJP Sun Certified Programmer for Java 6 Study Guide is used as reference, however the aim is give an overview of every objective of the exam, furthermore think of a place where you can quickly review what you've studied.
I'd recommend reading the book and following these articles up once the articles are organized at the same way as the book.


Declarations, Initialization and Scoping

"Identifiers & Java Beans" - (Objectives 1.3 and 1.4)

What seems an easy objective of the exam, this objective should be read carefully, once this objective is the first of the book you can forget some rules, also getting a better understanding of this chapter can help you with longer question where the issue is a compiler-error.
Find below the main things that you need to know:

Legal Identifiers

  1. Use only Unicode characters, numbers, currency symbols and connecting characters(like underscores).
  2. Identifiers must start with a letter, a currency character( $ ) or a connecting character such as the underscore ( _ ).
  3. Looking at the second letter of the identifier we still keep the same rule but in addition numbers are allowed, thus a combination of letters, currency characters, connecting characters plus numbers as mentioned are allowed.
  4. Java keywords are forbidden, find a list of Java keyword here.
  5. Finally the identifiers are case-sensitive which means BAR is different from bar.

Find some examples of invalid identifiers above, keep in your mind the invalid identifiers, it's the best way to remind the rules.



Sun's Java Code Conventions

The next subject approached conventions for that there is an article that covers it clearly.

JavaBeans Standars

To achieve a goal you only need to study few basics for the exam.
Basically the variable instances are declared as private and these are used by getters and setters methods, the functionality of these methods are getting information and retrieve successively.
The JavaBeans naming rules are the following:

  1. The prefix of getters methods must be get whether the property is not a boolean for this case you can use either is or get.
  2. The prefix of setter methods must be set.
  3. Getters and setters methods must be marked as public.
  4. To declare a getter and setter method use the prefix( get or set) plus the name of property with the first letter in uppercase.
  5. Setter methods use void which means there is no returning and should have an argument that represents the property type.
  6. Getter methods have a return type which is the same as the property and take no arguments.


Also there are rules for JavaBean Listener, to describes that find above the examples for JavaBean Listener Naming Rules.



"Declare Classes" ( Exam Objective 1.1 )

Source File Declaration Rules

Look at the source below


Find the explanation for each element

  1. Packages - if the class is inside a package you must declare a package statement firstly, otherwise you can ignore it.
  2. Imports - if the class needs to use references of others classes you must declare an import statement, if there is no package statement an import statement must be the first statement of the file, otherwise it would be a second statement.
  3. Comments - Can appear everywhere you want
  4. Classes:
  • The name of public classes must be the same as the name of file.
  • You cannot have more than one public class per file.
  • Imports and packages statements affect all the class in the file.
  • The file can have more than one nonpublic class.
  • If your class doesn't have a public class you can call the file with any name.
A brief explanation of the class used by example is given below.

The first statement is "package com.blogspot.mad4technology" which means the class is inside a package, so this statement must be the first in the file, looking at the second statement we have an import statement, because we need to use a reference for the object "java.util.Date", even more a class "Address" needs the same object and we cannot declare another import statement for this class, the classes in the file share the imports and packages statements.
Talking about the class rules we have two classes into the same file, but notice we've got a public class and nonpublic class what is allowed, if you see the name of file it must be exactly the same as the identifier of the public class in this case "Person".

Class Declarations and Modifiers

Modifiers fall into two categories:
Access Modifiers: Basically this kind of modifier restricts or allows access to a class, if you look at the picture above there are three access modifiers, however there is a fourth access called default or package, when you don't declare an access modifier a class assumes a default access modifier.


Class Access

Default Access: Classes that use this kind of level access have access only within the package.

The second class "Dog" does not compile because the classes are in a different package to solve the problem you need to put these classes in the same package or mark a class Animal as public.

Public Access: Classes that use public modifier are visible everywhere, make sure if you're using the important statement for classes in a different packages.



You noticed you can use only two levels of access because the others don't make sense to use, you'll see it later.


Other nonaccess
: There are others modifiers like final, abstract and strictfp, what you have to know is access modifiers can work with nonaccess modifiers for example public and final, also strictfp and final can be used together but abstract and final cannot.

Final Classes: Using this nonaccess modifier you make a class nonsubclassed which means any class can extend it, for example a class String is a final class and none one can extend it, any attempts to do you'll get a compiler error.


Abstract Classes: The main purpose is the class marked as abstract cannot be instantiated.


Take a look at the code above, the abstract methods don't use { } in their declaration, instead they are using semicolon also you noticed there is a non-abstract method so if you don't mark a method as abstract you have to type its implementation.
If exists at least one abstract method in a class the class must be marked as abstract.

How the post is becoming too long I've decided to split it up with a second article, hope you enjoy it also I'll be waiting for advices to improve the content of blog.