Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
Sun Certified Programmer for the java 2 platform study guide

by Kasun Herath
Article 1
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.htmlhttp://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.
http://www.sun.com/training/catalog/courses/CX-310-065.xml
Section 1: Declarations, Initialization and Scoping
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.
[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.
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
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();
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
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”);
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());
}
}
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.
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.
Great for known once.
This is great for those who knw J2SE.add download link to JVM @ SUN Microsystems for newbie.
Post new comment