Sunday, July 22, 2007

When do we go for Abstract classes and when do we go for Interfaces in java

Abstract class is a class that contains 0 or more abstract methods and also ordinary methods. Interface is specification of method prototypes(all abstract methods only).

Whenever the developers wants to use some features be shared by all objects(different classes), that time they go for abstract class.

When the developers wants different implementation for every objects then they go for interface.

We cannot create the object for abstract and interface. Instead we can create the reference of abstract class and interface.

Method Overriding versus Variable Shadowing in java

Method overriding is not like variable shadowing at all. Lets us have an example...

 class A {  
int i = 1;

int f() {
return i;
}
}

class B extends A {
int i = 2; // Shadows variable i in class A.

int f() {
return -i;
} // Overrides method f in class A.
}

public class Override_test {
public static void main(String args[]) {
B b = new B();
System.out.println(b.i); // Refers to B.i; prints 2.
System.out.println(b.f()); // Refers to B.f(); prints -2.
A a = (A) b; // Cast b to an instance of class A.
System.out.println(a.i); // Now refers to A.i; prints 1;
System.out.println(a.f()); // Still refers to B.f(); prints -2;
}
}



Another example of method overriding and variable shadowing

 class Parent {  
String classStr = "Parent";

void test() {
getClassName();
System.out.println(classStr);
}
void getClassName() {
System.out.println("Parent");
}
}

class Child extends Parent {
String classStr = "Child";

void test() {
super.test();
}
void getClassName() {
System.out.println("Child");
}
}

public class Main {
public static void main(String a[]) {
Child child = new Child();
child.test();
}
}


Output:
=======
Child
Parent

Thursday, July 19, 2007

Javascript for setting a timer for a page

Javascript for setting a timer for a page. It automatically submits the form to the next page.... This javascript code displays the timer in the taskbar.

var timerID = null;
var timerRunning = false;
var secondsleft = null;

function stopclock() {
if (timerRunning) {
clearTimeout(timerID);
timerRunning = false;
}
}

function startclock(s) {
secondsleft=s;
stopclock();
showtime();
}

function showtime() {
secondsleft = secondsleft - 1;
if (secondsleft == 60) {
alert("1 minute left");
}
if (secondsleft == 0) {
stopclock();
document.title ="Time over";
alert("Sorry Time's up");
window.document.getElementById("id_submitbutton").click();
//document.getElementById("mform1").submit();
} else {
current = secondsleft;
var hours = Math.floor( current / 3600 );
current = current - (hours*3600);
var minutes = Math.floor( current / 60 );
current = current - (minutes*60);
var seconds = current;
var timeValue = "" + hours;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
document.title = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}
}

-----------------------------------------------------

In the above code, we can use either
window.document.getElementById("id_submitbutton").click();
or
document.getElementById("mform1").submit();