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.




No comments: