[STUB] – ABSTRACT CLASSES vs INTERFACES

10 04 2009

Interfaces to abstract classes is what pure  virtual is to virtual classes in C++

Interfaces are also used to implement multiple inheritance functionality, missing from Java but present in C++

Also good for Strategy pattern

CAN-DO / IS-A relationship

see

http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html

http://geekswithblogs.net/mahesh/archive/2006/07/05/84120.aspx

http://www.leepoint.net/notes-java/oop/interfaces/interfaces.html





“This” keyword in Java explained

8 04 2009

The keyword this is useful when you need to refer to instance of the class from its method. The keyword helps us to avoid name conflicts. As we can see in the program that we have declare the name of instance variable and local variables same. Now to avoid the confliction between them we use this keyword. Here, this section provides you an example with the complete code of the program for the illustration of how to what is this keyword and how to use it.

In the example, this.length and this.breadth refers to the instance variable length and breadth while length and breadth refers to the arguments passed in the method. We have made a program over this. After going through it you can better understand.


class Rectangle{
int length,breadth;
void show(int length,int breadth){
this.length=length;
this.breadth=breadth;
}
int calculate(){
return(length*breadth);
}
}
public class UseOfThisOperator{
public static void main(String[] args){
Rectangle rectangle=new Rectangle();
rectangle.show(5,6);
int area = rectangle.calculate();
System.out.println("The area of a Rectangle is : " + area);
}
}





An excerpt from Bruce Eckel’s “Thinking in Java”

27 11 2008

Cleanup: finalization and garbage collection

Programmers know about the importance of initialization, but often forget the importance of cleanup. After all, who needs to clean up an int? But with libraries, simply “letting go” of an object once you’re done with it is not always safe. Of course, Java has the garbage collector to reclaim the memory of objects that are no longer used. Now consider a very special and unusual case. Suppose your object allocates “special” memory without using new. The garbage collector knows only how to release memory allocated with new, so it won’t know how to release the object’s “special” memory. To handle this case, Java provides a method called finalize( ) that you can define for your class. Here’s how it’s supposed to work. When the garbage collector is ready to release the storage used for your object, it will first call finalize( ), and only on the next garbage-collection pass will it reclaim the object’s memory. So if you choose to use finalize( ), it gives you the ability to perform some important cleanup at the time of garbage collection . Διαβάστε την συνέχεια του άρθρου »