I saw a class “AttributeSelectedClassifier” was once created in the following ways:
AttributeSelectedClassifier classifier = new AttributeSelectedClassifier();
classifier.setClassifier(base);
classifier.setEvaluator(eval);
This above one looks natural to me. But how about the following one.
classifier = new AttributeSelectedClassifier();
((AttributeSelectedClassifier)classifier).setClassifier(base);
((AttributeSelectedClassifier)classifier).setEvaluator(eval);
I think it should be right, but I am not quite sure about the way of of defining classifier as ((AttributeSelectedClassifier)classifier), how to understand this usage?
Look at the below code. Person implements the
CanWalkinterface. If you assign aPersontoCanWalkinterface as shown in the main method, which is a common practice, you can only invoke the methods that are specified in theCanWalkinterface i.e.walk(). If you want to invokef(), that isn’t declared in theCanWalkinterface, then you would use the 2nd mechanism you have specified in your post. i.e. cast it toPersonclass and then invoke the method.It is a good practice for the user’s of the API (main method here) to use the correct abstraction while working with an object. For e.g. if the client is mainly focused on moving Person’s then it should use CanWalk. This way client is not effected by changes to the Person class that are not related to movement. Read this article for more details.