HTML5, CSS3, jQuery, JSON, Responsive Design...

Extending your classes in Domino Java (Updated)

Michael Brown   September 8 2010 10:00:00 AM
I'm making my first, fumbling excursions into the world of Java in Domino.  This is, in fact, my first real dealing with Java anywhere, a fact that will no doubt be apparent to those of you that actually know the language.  I've done a bit of OO in LotusScript, a language that is not very designed for such things, so I'm interested in how things differ in what is supposed to be the real deal.

Extending a class is something else that seems to be a bit more complicated in Java than in LotusScript.  In Lotuscript, you'd do it something like this:

Public Class TestClass
Sub new(m As String)
     Messagebox "TestClass constructor says " & m
End Sub
End Class

Public Class TestSubClass As TestClass
Sub new(m As String)
     Messagebox "TestClassSub constructor says " & m
End Sub
End Class


The constructor (called "Sub New" in LotusScript) is called for the parent class(es) automatically.  So, a call like this:

Dim testObj As New TestSubClass("hello")


will give you these dialogs, in order:

Dialog 1


Dialog 2



By contrast, the following Java code, which should be equivalent to the above, won't even compile!   The Java agent Domino Designer will give you the compile error "TestClass(java.lang.String) in TestClass cannot be applied to ()", which is pretty well meaningless (to me anyway).

public class TestClass{

public TestClass(String m) {        // Constructor, like Sub New() in LotusScipt
     System.out.println("TestClass contstructor says " + m);
}

}

public class TestClassSub extends TestClass {

public TestClassSub(String m) {     // Constructor, like Sub New() in LotusScipt
     System.out.println("TestClassSub contstructor says " + m);
}

}


This is because Java does not implicitly call the constructor function for parent classes.  (In Java, the constructor is a function that has the same name as the class itself.)  Java does, however, expect that you do this for yourself, and will give you that compile error if you don't.  You call the parent constructor using super().  The code below does compile correctly:

public class TestClass{

public TestClass(String m) {        // Constructor, like Sub New() in LotusScipt
     System.out.println("TestClass contstructor says " + m);
}

}

public class TestClassSub extends TestClass {

public TestClassSub(String m) {     // Constructor, like Sub New() in LotusScipt
      super(m); // must call the parent class's constructor function
     System.out.println("TestClassSub contstructor says " + m);
}

}


When called like so:

TestClassSub testS = new TestClassSub("hello");


it produces the correct output in the Lotus Notes Java Console:

Correct results in Java Console



Update 08/09/2010

It seems that Java does automatically call a parent class's constructor, but it calls the one with no arguments (a.k.a. the "noargs constructor").

This is another major difference between LotusScript and Java.  In LotusScript, you can only have one constructor (i.e. one "sub new()") per class.  In Java, you can have as many as you like, as long as they all have different signatures.  A signature is defined by the number and/or data type of the arguments that the constructor has, with the noargs constructor having none at all.

So, the code below will compile and produce the output "nothing going on here".  That's because I make no call to super() in the sub class and this time I have a noargs constructor defined.

public class TestClass{

public TestClass() {        // Constructor, like Sub New() in LotusScipt
     System.out.println("Nothing going on here");
}

public TestClass(String m) {        // Constructor, like Sub New() in LotusScipt
     System.out.println("TestClass constructor says " + m);
}

}

public class TestClassSub extends TestClass {

public TestClassSub(String m) {     // Constructor, like Sub New() in LotusScipt
     System.out.println("TestClassSub contstructor says " + m);
}

}



If I put my super() call back into the sub class, as in the code below, I'll get the same output that I got earlier ("TestClass says hello" followed by "TestClassSub says hello").  Java knows to call my original constructor, rather than the noargs constructor, because of the signature of the super() call; the super() contains a single argument of type String, so it runs the parent's constructor that has a single argument of type String.

public class TestClass{

public TestClass() {        // Constructor, like Sub New() in LotusScipt
     System.out.println("Nothing going on here");
}

public TestClass(String m) {        // Constructor, like Sub New() in LotusScipt
     System.out.println("TestClass constructor says " + m);
}

}

public class TestClassSub extends TestClass {

public TestClassSub(String m) {     // Constructor, like Sub New() in LotusScipt
      super(m); // must call the parent class's constructor function
     System.out.println("TestClassSub contstructor says " + m);
}

}


(Thanks to Axel for his comment that helped me clear this up.)
Comments

1axel  09/07/2010 5:11:10 AM  Extending your classes in Domino Java

In java the constructor implicitly calls the no-args constructor of the superclass.

You may change this default behaviour, using super(myArglist) as first line in your constructor.

In your example TestClass HAS NO no-args constructor!!!

Add

public TestClass() { // Constructor, like Sub New() in LotusScipt

System.out.println("TestClass contstructor says no args constructor);

}

to TestClass and comment out the super(m) line in ur TestClassSub constructor.

Similar example.

class A {

}

class B extends A {

B(String str) {

}

}

class C extends B {

}

C compiles, if you explicitly add a no args constructor for class B.

Like B() {}

class A makes no problem. If there is no constructor, the no-args constructor is added implicitedly.

2Mike Brown  09/07/2010 3:58:15 PM  Extending your classes in Domino Java

Thanks, Axel.

I did actually notice that it worked if there was a noargs constructor present, but I wasn't sure why! Thanks for clearing that up.

I've updated the post accordingly.

About