Sun Certified Programmer for the java 2 platform study guide

by Kasun Herath
Article 1
This is the first in a series of articles aimed at helping those who intend to sit for the sun certified programmer for java 2 platform (SCJP). This is not an introduction to the java language and it is assumed that the readers are already familiar with the basics of java. Some basics are provided below to get us started.
Sun maintains a coding convention that they recommend others to follow. Have a look at it.
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html
A class declaration
Package Statement;
Import Statement;
[public] class ClassName{
//class implementation
}
One file can contain many classes, but there can be only one public class (note the public keyword in front of the class declaration is optional). The name of the public class should be the name of the file. The import statement and package statement applies to all the classes in a file.
Now that some basics are covered let’s jump to the exam. The exam contains objectives and objectives are grouped into seven sections. Let’s cover the first section today. The complete set of objectives are listed here
http://www.sun.com/training/catalog/courses/CX-310-065.xml
Section 1: Declarations, Initialization and Scoping
Package statements are used to separate different classes that could have the same class name. Normal procedure is to use domain name in the reverse order. For example a class written in the digit organization could be placed in a package called lk.digit.projectname.
Declaring classes
Like in every Object Oriented programming language (OOP) a class in java is a blue print for creating object, which is the most important aspect of an OOP language.
You can declare a class as follows
[Access Modifier] [non-access Modifier]class ClassName
Access Modifiers are used to restrict access to a class or entities of a class by other classes. Entities are either variables or methods or constructors. In java there are four Access Modifiers. They are public, protected, default or private. public means the entity or the class is visible to all classes. private means a entity is visible only to the class where it is declared and not visible to the outside. default means the entity or the class is visible only to the same package where the class or the owner class of the entity resides. protected means the entities are visible to the package plus to the sub classes of the class where the entity is declared (sub classes are discussed later). The access modifier in a class declaration is optional and only public and default access modifiers are allowed in a class declaration.
Import statement is used to import classes of a different package. If there is a class called BasicTemplate in package lk.digit.templates it could be used inside a class which reside in a different package as demonstrated below.
Package lk.digit.utils;
Import lk.digit.templates. BasicTemplate;
Public class TrafficAnalyzer {
Public static void main(String args[]) {
BasicTemplate template = new BasicTemplate();
}
}
Inheritance (sub classing)
Inheritance is an important concept in any OOP language. Like the name implies it is a technique to inherit features of one class by another. Here the features are methods and variables. The inheriting class, which is known as the sub class would inherit from a more abstract super class. As an example think of a class called vehicle. It is more abstract and a more specific class called Car could inherit from the Vehicle class. Inheritance is implemented using the key word extends. It is demonstrated below.
Class Vehicle {
//declarations
}
Class Car extends Vehicle {
}
The class Car would inherit the declarations in the class Vehicle (Keep in mind that the access modifiers also play a part in deciding whether the sub class inherits the declarations in the super class. See above for access modifiers).
public abstract void getSize();
Every class that has at least one abstract method should be declared as an abstract class. But an abstract class doesn’t need to have abstract methods. This concept is really important. So keep it in mind, an abstract class may or may not have abstract methods but a class with at least one abstract method should be an abstract class. The first concrete (non-abstract) class that sub classes an abstract class must implement all the abstract methods in the abstract class. An example is shown below.
abstract Class Vehicle {
public abstract void reverse();
}
Class Car extends Vehicle {
public void reverse() {
//implementation
}
}
An interface is a special kind of an abstract class. Every method in an interface should be public and abstract. Unlike a class an Interface is inherited using the keyword implements rather than extends.
Here are some points you need to keep in mind about interfaces.
1) An interface can extends one or more interfaces
2)An interface can’t implement an interface or a class
3) Multiple interfaces could be implemented by a single class
4) Methods in an interface is implicitly public and abstract whether you declare it or not
5) Variables in interfaces are public, static and final whether you declare it or not
Below is a valid interface declaration, go through it slowly and note the rules.
interface Eatable {
public abstract void eat();
void smell();
}
class Fruit extends Food implement Eatable{
public void eat(){
//implementation
}
Public void smell(){
//implementation
}
}
Enums
Enums are used in java to define a set of values. An Enum is not a class but something like a class. Like a class an enum can contain constructors, methods and variables. An enum is declared below
enum size {SMALL, BIG, LARGE };
An enum is initialized as follows
Size shirtSize = Size.BIG;
The following enum declaration uses a method and a constructor
enum Size {
private int size;
SMALL(1), BIG(4), LARGE(8);
Size(int size){
this.size=size;
}
Public int getSize(){
return size;
}
}
The following code uses the enum declared above. The output of the following code would be 4.
class Box{
Size size;
Public static void main(String args[]){
Box box=new Box();
box.size=Size.BIG;
System.out.println(box.size.getSize());
}
}
Please note that an enum could be declared outside or inside a class but never inside a method.
Method overloading and overriding
We discussed how a subclass inherits the methods of a super class. In the subclass we can re write the inherited methods to have a different behavior than the super class. It is called method overriding. If the method in the super class is declared final then that method cannot be overridden. If the method in super class is abstract and if the sub class is concrete then the method should be always overridden.
class Vehicle{
public void start(){
}
}
class Car extends Vehicle{
public void start(){
}
}
If an object of Car is made in either way as follows,
1) Vehicle carObject = new Car();
2) Car carObject = new Car();
Then the method start() is invoked using that object then the method in the Car class would be invoked on both occasions.
Hint:The procedure for an invocation when inheritance and overriding is present is worth keeping note (as in 1). Since the object reference is type of Vehicle the compiler would look whether there is a method called start() in the class Vehicle. There is a start() method in the Vehicle class so no problem. But on runtime when the method is invoked the method in the Car class would be chosen because what is taken into consideration is the object type, not the reference type.
There are some important rules regarding overriding
1) The access-modifier should be the same or more public
2) Number of arguments and the types of the arguments should be the same
3) Return type should be the same or a subclass type
Method overloading
Method over loading is having methods with the same name, but with different arguments. Here the number of arguments could be different or the type of the arguments could be different
1) The number or the type of arguments should be different
2) Access modifier and the return type could be different
Let’s see an example
class Calculator{
public void add(int i){
System.out.println(“int”);
}
protected void add(double d){
System.out.println(“double”);
}
}
class TestAdd{
public static void main(String args[]){
Calculator cal=new Calculator();
cal.add(10);
cal.add(10.0);
}
}
In the first invocation of add ‘int’ would be printed and from the second invocation ‘double’ would be printed.
Hint:
Floating point numbers are double implicitly unless specified as float. You have to insert an ‘f’ after the number to specify it as float.
Constructors
Constructors are a special kind of methods. The big difference is constructors do not have a return type (not even void) and cannot be directly invoked. Constructors are invoked when instances are made. The constructor name should be as same as the class name. Just like methods constructors can have any access modifier, if a constructor is declared private then that class cannot be initialized outside from the class. Finally constructors could be overloaded but obviously cannot be overridden.
Vehicle vehicle =new Vehicle();
Vehicle vehicle =new Vehicle(“Toyota”);
As you might have guessed correctly on the first object creation ‘No args constructor’ would be printed and on the next object creation ‘String constructor’ would be printed.
What if a constructor is not provided in a class and an object is created of that class? Then the JVM would provide a default constructor, which is a constructor with no arguments. If there is one or more other constructor available then no default constructor would be provided. The first line of a constructor should be a call to a super class constructor or to an overloaded constructor in the same class. A super class constructor could be called as follows.
class Vehicle{
public Vehicle(){
System.out.println(“No args constructor”);
}
public vehicle(String model){
System.out.println(“String constructor”);
}
}
public Car extends Vehicle(){
super(“Toyota”);
}
If there is no explicit call to a super class constructor, the JVM would insert a call to the default constructor of the super class. Finally abstract classes do have at least one constructor but interfaces do not. Please keep in mind that a correct idea about constructors is very important in the exam.
Hint:
class Object is the super class of every class, which means that every class has a super class.
Q&A
1) Consider the following code sample
class Vehicle{
protected String brand=null;
public Vehicle(String brand){
this.brand=brand;
}
public String getBrand(){
return brand;
}
}
class Car extends Vehicle{
public Car(String brand){
this.brand=brand;
}
}
public class CarTest{
public static void main(String args[]){
Car car=new Car("toyota");
System.out.println(car.getBrand());
}
}
what would be the result?
2) Consider the following
class Calculator{
public void add(int num1,int num2){
System.out.println(num1+num2);
}
public void add(float num1,float num2){
System.out.println(num1+num2);
}
}
public class CalculatorTest{
public static void main(String args[]){
Calculator cal = new Calculator();
cal.add(10.0,20.0);
}
}
what would be the result?
3) Consider the following code
class Vehicle{
protected String brand=null;
public Vehicle(String brand){
this.brand=brand;
}
protected String getBrand(){
return brand;
}
}
class Car extends Vehicle{
public Car(String brand){
super(brand);
this.brand=brand;
}
String getBrand(){
return brand;
}
}
public class CarTest{
public static void main(String args[]){
Car car=new Car("toyota");
System.out.println(car.getBrand());
}
}
what would be the result?
4) Which is true?
A. "X extends Y" is correct if and only if X is a class and Y is an interface.
B. "X extends Y" is correct if and only if X is an interface and Y is a class.
C. "X extends Y" is correct if X and Y are either both classes or both interfaces.
D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces.
Answers
1) Compile error. On the constructor of Car class there is no call to the super class. So the JVM would insert a call to the default Vehicle constructor. But since there is already a non-default constructor in class Vehicle a default non-argument constructor cannot be provided for the Vehicle class, hence the compile error.
2) Compile error. Remember floating point numbers are defaultly double, there is no add() method that accepts two double numbers. For a successful invocation it should have been called as cal.add(10.0f,20.0f);
3) Compile error. Method getBrand() is protected in Vehicle and when it was overridden it has been changed to default which is a more private access-modifier than protected.
4) C. A class implements an interface. An interface extends an interface. A class also extends another class.



Great for known once.
This is great for those who knw J2SE.add download link to JVM @ SUN Microsystems for newbie.
Post new comment