satish's blog

satish's picture

Calling subclass method from base class constructor in Java

Let us start with a simple Java program:

class A{
   String str = "Hello";

   public A(){
      init();
   }

   void init(){
      System.out.println(str);
   }

   public static void main(String[] args){
      new A();
   }
}

Pretty simple code, ain't it?
This will print "Hello" to the console.
Now see this:

class A extends B{

    public static void main(String[] args) {
        new A();
    }

    String str = "Hello";

    @Override
    void init() {
        System.out.println(str);
    }

}

abstract class B{
    public B(){
        //Do some initialization
        //and run the rest of the code
        init();
    }

    abstract void init();
}

If you think this time too it prints "Hello" then I am glad I have some one who thinks like me
Well it actually prints "null"
That is because 'str' is not yet initialized! Yes

If we call a subclass method from the super class' constructor, the subclass' variables will not be initialized to the default values. I learned this the hard way. The only workaround is to set the values in the init() method or simply not use this approach at all.

I needed to run the subclass code in a separate thread with a particular Subject

Do comment if you know any better way to do this

Update: A solution is suggested here: http://benpryor.com/blog/2008/01/02/dont-call-subclass-methods-from-a-su...

In summary, instead of the super class constructor calling a method overridden by the subclass, make the subclass constructor call a method that is defined in the super class. It is shown in the code below:

class A extends B{

    public static void main(String[] args) {
        new A();
    }

    String str = "Hello";
    
    public A(){
	//Call a method in superclass to do the initialization
	//This method will call init() back
	pre_init();
    }


    @Override
    void init() {
        System.out.println(str);
    }

}

abstract class B{
    public B(){
    }

    void pre_init(){
	//Do some initialization
        //and run the rest of the code
	init();
    }

    abstract void init();
}

 

satish's picture

Open Source software and academia

Open Source software brings a lot of advantages not just for businesses, for academic people too - students, faculty and staff.

Due to its very nature, students can access the source code, understand and learn from it. In turn they start contributing to it. As most education about Computers should be practical oriented, students learn themselves by looking at code than reading books. This gives them a direct overview of the industry standard software. Faculty usually points them to the right place to look for resources. User groups, IRC and every other possible places to meet other programmers can be utilized by these students.

Non-programmers also can reap the benefits of Open Source. Computer Science and allied branches have to learn various kinds of softwares as part of their syllabus. Usually colleges have the freedom of choosing the software they want to learn. Open Source definitely makes a big impact in this area. Universities usually prefer Open Source for learning because of their less cost, good following of international standard and ease of customization.

There are also a lot of tools for academia based on Open Source. Like the Sakai Project(http://www.sakaiproject.org/) for maintaining course work, which is co-developed by 3 major universities, there are also software to help in day-to-day workings of academics.

Some more below:
Aggregators (Aggie, Awasu,Plum),
Classroom tools (Chalksite, Engrade, Slideshare, Schoopy),
Collaboration tools (BackPack, BaseCamp, LiveText),
Course management tools (ATutor, EduTools, Sakai and even Drupal)

Thanks to Namita for giving me the above list.
As part of Twincling's commitment to academia, in improving the standards of our technical education, we are conducting a session on "Long distance teaching with Open Source Web2.0 tools" at Vellore Institute of Technology.
We are confident it will benefit the students and faculty of VIT.

Syndicate content