Re: getting used to Java - question about "style"

From: Chris Smith (cdsmith_at_twu.net)
Date: 06/30/04


Date: Wed, 30 Jun 2004 07:00:15 -0600

glunk wrote:
> They go on to explain
>
> "Subclasses may override nonfinal methods and Java will dispatch a call to
> such a mrethod according to the actual type of the constructed object -
> before executing the derived class constructors. This means when the
> constructor invokes the derived method, the derived class may be in an
> invalid state. To prevent this, call only final methods from the
> constructor."
>
> I cannot understand what this says or means. I know that a final method is
> one that cannot be overridden. I do not understand what this parapgraph is
> intending to warn me against doing or why. Can someone explain, preferably
> with an example?

Sure:

public class A
{
    public A()
    {
        init();
    }

    protected void init()
    {
    }
}

public class B extends A
{
    private int var = 7;

    public B()
    {
    }

    protected void init()
    {
        System.out.println(var);
        var = 42;
    }
}

public class Main
{
    public static void main(String[] args)
    {
        new B();
    }
}

It's not obvious to most people that the code above prints "0" and not
"7", or that after the object has finished initializing, var will have a
value of 7 and not 42. Because this is confusing, you're advised to
avoid it. At the very least, if you do call a non-final (and non-
private, since private methods implicitly can't be overridden; and non-
static, since static methods aren't polymorphic) method from a
constructor, you should document this copiuously in the API docs for
that method, so people will know that they should not access any
instance state from that method.

-- 
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Relevant Pages

  • Re: Adventures in C# Unit Testing
    ... public class ChildClass:ParentClass ... private IMyInterface mockMyObject; ... ChildClass overrides createMyObject to return a mock object created by the ChildClass constructor. ... Subclass and override. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How can I override a public virtual method to be private in my derived class?
    ... The only real reason why I want to override it in the derived class is so ... public class ClassB: ClassA ... > superclass as a "contract" not to be violated. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: calling base constructor
    ... where to call a base constructor. ... public class ChildClass: BaseClass ... the base class clearly expects the derived class to override the initialization behavior. ... In that case, it seems obvious to me that the addition of new elements to the Members field belongs in an override of the InitializeMembersmethod, given the existing design. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: override default values in a constructor
    ... I have to explicitly add "MyBase.new" to the constructor, ... If you wish to 'override' a constructor, ... > in the derived class, and if you wish to still run the constructor logic ... > Microsoft MVP - ASP/ASP.NET ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: what is the RIGHT THING to make a constructor of a subclass, using super()
    ... If you are left with that requirement as a necessary or desirable part of the design, then the usual approach is probably to write a static method in the class that you call as an argument to the call to superfrom your constructor. ... public class TestDerived extends TestBase ... public Customer(String firstName, String lastName) { ... public class SlashSeparatedCustomer extends Customer { ...
    (comp.lang.java.programmer)

Loading