Thursday, November 12, 2009

SCJP 1.6 Study Guide: Declarations and Access Controls Part 7

Class Members and Non-Access Modifiers

The previous post discussed how to declare class members and how to modify them using access modifiers. Now that you know how visibility affects your code we will take a look at how non-access modifiers affect our code.

You are already familiar with some of the non-access modifiers we can use with class members, final, abstract and strictfp, but we will also take a look at transient, synchronized, native and static. We'll discuss how these modifiers affect our code when applied to methods in this post and then to variables in the next post. Static methods and variables will be discussed after the next post.

Final Methods. When applied to a method, the final keyword does not allow subclasses to override the method.

Example:
public class Person{
public final void eat(){
System.out.println("A person eats using his/her mouth.");
}
}
It is legal to extend Person but you cannot override the eat() method.

Final Arguments. Arguments are the variables we declare inside the parentheses in a method declaration.

Example:
public class TaxCalculator{
private double taxRate;

public void setTaxRate(double rate){
rate = rate * 0.1;
taxRate = rate;
}
}
In the example above the argument is rate. When we declare a method argument to be final we are telling the compiler that rate cannot be reassigned with a new value.

Example:
public class TaxCalculator{
private double taxRate;

public void setTaxRate(double final rate){
rate = rate * 0.1; // compiler error
taxRate = rate;
}
}
Abstract Methods. An abstract method is generally just a method declaration without any implementation. And if you remember the discussion regarding abstract classes, if a class has an abstract method it should be declared abstract too.

Once a class extends from an abstract class it is forced to implement that abstract method unless that class is abstract as well. The general rule to follow is: The first concrete(non-abstract) class that extends an abstract class should implement all abstract methods declared in the abstract class.

Example:
public abstract class Animal{
private String name;

public abstract void makeSound();

public String getName(){
return name;
}
}

public abstract class Dog extends Animal{
private Collar collar;

public Collar getCollar(){
return collar;
}

// still abstract, no need to implement makeSound()
}

public class Basenji extends Dog{
// not abstract(concrete) class
// needs to implement bite() from Dog
// no need to really put code here
// just change semi-colon to pair of braces
public void bite(){}

// also needs to implement makeSound() from Animal
// since Dog did not implement makeSound()
public void makeSound(){
System.out.println("Yodelehihoo");
}
}
The abstract keyword when used in a method is not allowed to combine with static, final and private keywords.

Synchronized Methods. Marking a method with the synchronized keyword makes a method accessible to only one thread at a time. We'll discuss more on this when we get to Threads.

Example:
public class AccountManager{
public synchronized void withdraw(double value){}
}
You can also mix access modifiers with the synchronized keyword. What non-access modifiers can you combine with synchronized?

Native Methods. The native keyword indicates that a method contains platform-dependent code usually in C. You only need to know that this keyword can only be used with methods.

Strictfp Methods. When you use strictfp with methods you are sure how your floating points will behave regardless of platform. Check Part 4 for details.

You cannot use strictfp with variables.

Constructor Declarations. When you create a Java application you will make, instantiate or create objects to communicate with each other and when you create an object at least one constructor is invoked.

Every class in Java has a constructor. The compiler will provide one(default constructor) for you if you don't. There are several things to consider in declaring a constructor which we will discuss on a later post. But for now, let's take a look at a basic constructor declaration:

Example:
public class Person{
public Person(){} // this is a valid Person constructor

public void Person(){} // this is a valid but badly named method
}
If you notice, the Person() constructor looks like the Person() method. The main difference is that constructors cannot have a return type. Constructors also must have the same name as the class in which they are declared.

Constructors can be marked with access modifiers. Non-access modifiers like static, final and abstract cannot be used with constructors.

Be sure that you have understood what access and non-access modifiers we can use with methods and how they affect those methods. Try using and combining them as practice. The next post will handle variable declarations.

0 comments:

Post a Comment