Question: 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?

Answer: 
Look at the below code. Person implements the `CanWalk` interface. If you assign a `Person` to `CanWalk` interface as shown in the main method, which is a common practice, you can only invoke the methods that are specified in the `CanWalk` interface i.e. `walk()`. If you want to invoke `f()`, that **isn't** declared in the `CanWalk` interface, then you would use the 2nd mechanism you have specified in your post. i.e. **cast** it to `Person` class 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](http://www.markhneedham.com/blog/2009/10/18/coding-role-based-interfaces/) for more details.

```
interface CanWalk
{
  void walk();
}

class Person implements CanWalk
{
  void walk()
  {
     System.out.println("I am walking");
  }

  void f()
  {
    ///some arbitrary method
  }
}

public stativ void main(String a[])
{
   CanWalk cw=new Person();
   ((Person)cw).f();
}

```
By doing `((AttributeSelectedClassifier)classifier)` you are casting ("*forcing*") the `classifier` object to behave like an instance of `AttributeSelectedClassifier`.

The `classifier` variable was probably previously declared as a superclass of AttributeSelectedClassifier.