Sunday, May 11, 2008

Abstract Class and Interfaces

Abstract Classes and Interfaces allows you to create dynamic methods that act as per the object type generated at run time.

Abstract Class
  • Any class that contains one or more abstract methods needs to be declared as abstract.
  • There can be no objects for an abstract class
  • Abstract constructor or abstract static methods cannot be declared in a abstract class. You can construct regular constructor.
  • Any sub-class of an abstract class must implement all the abstract methods of the super calls or itself be declared abstract.

Interface

  • They lack instance variable, and their methods are declared without bodies.
  • Format

access interface name {

return-type method1 (param-list);
return-type method2 (param-list);
type final-var1=value;
type final-var2=value;

}

  • Each class that implements an interface must implement all of the methods defined under the interface.
  • Interface methods implemented by the class are always public.
  • A class that only implements an interface methods partially must be declared as an abstract class and all the rules of abstract class apply to it.

Example

abstract class Shape
{
//Static variable numObjects to keep count of Shape objects created
protected static int numObjects=0;

//Shape constructor
Shape()
{
++numObjects;
}
//define abstract method for area
abstract double area();

//static function to return number of shapes
protected static int numShapes()
{
return numObjects;
}

}

//Define interface for drawing
interface Drawable {
void draw();
}

//Class Circle is a sub class of Shape
class Circle extends Shape
{
protected int radius;
protected static int numberCircles=0;
Circle(int radius)
{
super(); //execute super class constructor first before initializing circle class instance variable - radius
this.radius=radius;
++numberCircles;
}

//return area of circle as double. This is the implementation for abstract method area defined in Shape
protected double area()
{
return Math.PI * radius * radius;
}

//static method to return number of circle objects created
protected static int numCircles()
{
return numberCircles;
}
}

//Class Rectangle is a sub class of Shape
class Rectangle extends Shape
{
protected int length,width;
protected static int numberRectangles=0;
Rectangle(int length, int width)
{
super(); //execute super class constructor first before initializing rectangle class instance variables length and width
this.length=length;
this.width=width;
++numberRectangles;
}

//return area of rectangle as double. This is the implementation for abstract method area defined in Shape
protected double area()
{
return length * width;
}

//static method to return number of rectangle objects created
protected static int numRectangles()
{
return numberRectangles;
}
}

//Graphic circle inherits circle class and implements interface Drawable
class GraphicCircle extends Circle
implements Drawable
{
protected int color;

GraphicCircle(int radius,int color)
{
super(radius);
this.color=color;

}
//interface implementation of draw method. method access should always be public for interface method implementation
public void draw()
{
System.out.println("Circle drawn using color:" + color);
}
}
//Graphic Rectangle inherits Rectangle class and implements interface Drawable
class GraphicRectangle extends Rectangle
implements Drawable
{
protected int color;


GraphicRectangle(int len,int wid,int color)
{
super(len,wid);
this.color=color;

}

//interface implementation of draw method. method access should always be public for interface method implementation
public void draw()
{
System.out.println("Rectangle drawn using color:" + color);
}
}

Friday, May 9, 2008

Java Terms Explained

I started learning java and I thought it would be a good idea to help the user community of this wonderful language from a beginner's perspective.
http://java.sun.com/developer/onlineTraining/new2java/programming/learn/unravelingjava.html

What is the difference between SDK and JDK.
Java 2 SDK is by popular user opinion has been reverted back to JDK where JDK stands for J2SE Development Kit. J2RE has been reverted to JRE.
http://java.sun.com/javase/namechange.html

Another thing to note is that there is no difference between 1.5.0 and 5.0 version. The leading 1. was dropped and the new 5.0 numbering format has been adopted by Sun. The latest release of Java is version 6.0 and instead of referring it by J2SE 6.0, Sun prefers to call the new version Java SE 6.0. They have dropped the 2 altogether.
http://java.sun.com/j2se/versioning_naming.html

I would blame the Sun Marketing people of confusing people with the versioning. I hope the above links would help simplify things for the beginner.