setObject / getSpecificObject - when to type check?

From: VisionSet (spam_at_ntlworld.com)
Date: 10/29/04


Date: Fri, 29 Oct 2004 15:04:12 GMT

In an inheritence structure of classes the superclass has:

Object record

public void setRecord(int i) {
    record = model.getRecord(i)
}

this method is not overridden.

The subclasses have a package private getFlavouredRecord() method that
returns 'record' cast to its 'flavoured' type.

Should I override setRecord to ensure type is correct:

public void setRecord(int i) {
    super.setRecord(i);

    if(record != null && ! record instanceof Raspberry) {
        throw new ClassCastException();
    }
}

A bit bloaty for every subclass, but sensible?
Since setRecord() is public then perhaps I should, otherwise a bit OTT?

-- 
Mike W


Relevant Pages

  • Re: Requesting tips, comments for an EDT thread-safe game architecture
    ... Use a Window or JWindow and do active rendering. ... synchronize some parts of your code but the more you can avoid that the ... public void myRenderingLoop() { ...
    (comp.lang.java.gui)
  • Re: Problem with overloaded functions
    ... Then use keyword override in the derived class Point3D, ... public void ReadXml2 ... > public class Point2D: IXmlSerializable ... > class1.cs: warning CS0108: The keyword new is required on> 'Point3D.WriteXml' because it hides inherited member ...
    (microsoft.public.dotnet.languages.csharp)
  • Requesting tips, comments for an EDT thread-safe game architecture
    ... me wonder whether my general game architecture was thread safe or not. ... public static void mainthrows InterruptedException, ... public void windowActivated{ ...
    (comp.lang.java.gui)
  • Re: JTree not updating after deleting a node in DnD
    ... public void fireTreeStructureChanged(Object source, TreePath path) ... public MyTransferable(final DefaultMutableTreeNode data) { ...
    (comp.lang.java.gui)
  • Re: Wrong overload resolution ?
    ... The "new" keyword prevents Class2.Method1from being an override of Class1.Method1and the rule i mentioned above do not apply. ... So if you have a variable of type Class1 pointing to an instance of Class2, the method of Class1 will be invoked. ... > more interesting if the strongly typed overload is like override public void ...
    (microsoft.public.dotnet.languages.csharp)