File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes instanceOf operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "instanceOf operator" Watch "instanceOf operator" New topic
Author

instanceOf operator

Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531


Is this approach "bad"? why?

Source:
http://www.javapractices.com/topic/TopicAction.do?Id=31
(Example modified for java. //PRIVATE classes made public here.) <---


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14337
    
  22

It is bad because if you add another Animal class to the program, you have to modify the doSomething method too.

When you use polymorphism (getting rid of using the instanceof operator) you would not have to change the doSomething method when you add another animal, and it would still work correctly.

Suppose that you have a large and complicated system and a new programmer gets assigned the task of adding a new Animal class. The new programmer could easily miss the fact that he must also change the doSomething method. Especially if this construction is used in multiple places in the software, it's hard to be sure that you've added support for the new Animal class in all places where it's necessary. So, using the instanceof approach leads to a code maintenance problem.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19755
    
  20

Jesper is right. The usual way to do this is to have the base class (Animal) be abstract and have an abstract method that all non-abstract sub classes then must implement:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
Rob Spoor wrote:Jesper is right. The usual way to do this is to have the base class (Animal) be abstract and have an abstract method that all non-abstract sub classes then must implement:


So I will have to take-off all my constructors from Animal then(in case I have them)?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19755
    
  20

I don't understand your question.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
When we make a class abstract, we can not instantiate it, right? So previously if I had constructors for my class Animal, I will have to remove them because now I am making it abstract.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
No need to remove the constructor - the abstract class might use them to store some generic information.

In below code the Animal has a name regardless of whether its a dog or a cat. So the common variable shall be declared in the base class.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4457
    
    8

Remember that a constructor doesn't actually instantiate an object. It initialises an object that's been instantiated. So there's no reason for an abstract class not to use one, and (as John's example shows) it may be needed to initialise data belonging to the abstract class.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
As we know moving is not just restricted to animals, let me try another solution. isn't this any better?

Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
If you are going to use interface inheritance then you should simply consider using the strategy pattern and create 'Interface' moveable with concrete implemenations in fish..spider..etc.
http://en.wikipedia.org/wiki/Strategy_pattern

To take advantage of the composition the animal class should be like this..


Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
Only if you could have different names. moveable (it is instance variable, it is local variable and it is also method name)



If i am getting it right, movebale is supposed to be another inteface/class.
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Yeah you are right...moveable is supposed to be another interface ..
class Animal {

private moveable mv; //<--

public Animal(moveable m)
{
this.mv = m;

}

public void performMove()
{

mv.move();

}

}

The first method is actually a constructor. that is why it did not have the return type..i made a mistake in naming it.
Just follow the strategy pattern.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceOf operator