Wednesday, November 4, 2009

SCJP 1.6 Study Guide: Declarations and Access Controls Part 1

Objectives:
  1. Declare classes and interfaces (Normal class, abstract class and interface)
  2. Use primitives, arrays, enums and legal identifiers
  3. Use static methods, JavaBeans naming and var-args
As I've said on the parent post this is a study guide for the SCJP 1.6 Exam so I assume you have at least some knowledge of Java already. But here's a refresher:



Refresher

Class - A blueprint that defines the state and behavior for an object.

Object - Instance of a class with its own state and access to behaviors defined in its class. Usually created using the new operator.

State - Instance variables of a class.

Behavior - Methods of the class.

Example 1.1:
public class Person{
public String name;
public int age;

public void eat(){
System.out.println("Eating...");
}
}

public class TestPerson{
public static void main(String[] args){
Person p = new Person()
p.name = "John Doe";
p.age = 24;
p.eat();
}
}
In the example we have a Person class with instance variables name and age and an eat() method. When we created a Person object (Person p = new Person) we gave it a name and an age (John Doe, 24) which defines the state of object p. We also can use the eat() method since the p object can access it.

Most Java programs will be composed of objects created from different classes communicating with each other.

Identifiers and Keywords. All classes, variables and methods that we will create need names and we call them identifiers and there are rules on how we create legal identifiers.

Moreover, since the creators and developers of Java knew that it will be used by a large community they developed conventions in creating identifiers so we could understand each others code.

Remember that for our codes to compile we only need our identifiers to be legal. But when you are developing your application it is highly recommended that we follow the conventions.

In Example 1.1 class Person defined these identifiers:
  1. Person
  2. name
  3. age
  4. eat
Inheritance. Inheritance is one of the concepts of Object Orientation. It allows one class to be reused or extended by another class.

A class that is extended by another class is called the super class and the class extending the super class is called a subclass.

A subclass automatically inherits accessible instance variables and methods of the super class.

A subclass may also override methods it inherits from the super class.

Interfaces. Interfaces are 100% abstract classes. Classes implement interfaces instead of extending.

An interface defines methods that an implementing class should have but does not dictate how those methods should be written. The implementing class should define the actual code for those methods.

Packages. A typical Java application will usually use many classes. As such it is best to group related classes into packages or folders to give developers a consistent way of naming, accessing different classes and prevent naming conflicts.

That's it for this post. Part 2 will tackle Identifiers and JavaBeans.

0 comments:

Post a Comment