Results 1 to 20 of 25
- 03-06-2012, 10:41 PM #1
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Implementation advice (instanceof and interaction between subclasses)
I always see people railing on using instanceof as an example of poor OOP. I completely understand the argument, but the examples used are typically ones involving fairly shallow polymorphism (Animal.go(), animal.talk(), etc.)
When it comes to cases of interaction between the various subclasses, can instanceof be considered more acceptable? To continue the animal analogy, let's look at
Animal_A.eat(Animal_B);
If we say that dogs eat cats, cats eat rats, and rats eat dogs (they're huge), is there a good implementation that doesn't say
if(Animal_B instanceof Cat) Animal_B.die();?
I could use some kind of enum Type{CAT, RAT, DOG}, but that seems to just be duplicating instanceof in more lines of code.
Any advice?
- 03-07-2012, 08:07 AM #2
Re: Implementation advice (instanceof and interaction between subclasses)
Method overloading? Maybe something like
dbJava Code://Animal, abstract class public boolean eat(Animal other) { die(); return false; } // Dog extends Animal public boolean eat(Cat other) { System.out.println("yum!"); // poor Cat return true; } // Cat extends Animal public boolean eat(Rat other) { System.out.println("yum!"); // good riddance return true; } // Rat extends Animal public boolean eat(Dog other) { System.out.println("yum!"); // BURP return true; }Why do they call it rush hour when nothing moves? - Robin Williams
- 03-07-2012, 03:29 PM #3
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Implementation advice (instanceof and interaction between subclasses)
That looks like exactly what I was thinking of - I didn't think to check if Java could overload with more specific sub classes, I figured it would only overload eat(Animal a);. I presume then that in your example, Cat_C.eat(Dog_D); would trigger die(); and return false? Thank you very much!
- 03-09-2012, 10:36 PM #4
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Implementation advice (instanceof and interaction between subclasses)
Db -
So I finished up the testing portions of my program. As far as I can tell, Java does not selectively choose between "better" and "worse" overloads. I appreciate the help though.
- 03-10-2012, 12:16 AM #5
Re: Implementation advice (instanceof and interaction between subclasses)
What exactly do you mean by "better" and "worse" overloads? It's not any terminology I've heard before.
Read the rules of method overloading here: Chapter*8.*Classes
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-10-2012, 12:34 AM #6
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Implementation advice (instanceof and interaction between subclasses)
Sorry, I wan't specific. What you suggested does not work - you cannot overload with different class types, even if they subclass. At least, Eclipse doesn't do it in JRE1.7
- 03-10-2012, 12:51 AM #7
Re: Implementation advice (instanceof and interaction between subclasses)
Rubbish. You're doing something else wrong. Copy this and save it as Animal.java, then compile and execute it.you cannot overload with different class types, even if they subclass.dbJava Code:public abstract class Animal { public boolean eat(Animal other) { die(); return false; } private void die() { System.out.println("RIP"); } protected void yum() { System.out.println("YUM"); } public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat(); Rat rat = new Rat(); System.out.print("Dog eat Cat: "); dog.eat(cat); System.out.print("Dog eat Rat: "); dog.eat(rat); System.out.print("Cat eat Rat: "); cat.eat(rat); System.out.print("Cat eat Dog: "); cat.eat(dog); System.out.print("Rat eat Dog: "); rat.eat(dog); System.out.print("Rat eat Cat: "); rat.eat(cat); } } class Dog extends Animal { public boolean eat(Cat other) { yum(); return true; } } class Cat extends Animal { public boolean eat(Rat other) { yum(); return true; } } class Rat extends Animal { public boolean eat(Dog other) { yum(); return true; } }Why do they call it rush hour when nothing moves? - Robin Williams
- 03-10-2012, 06:18 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,417
- Blog Entries
- 7
- Rep Power
- 17
Re: Implementation advice (instanceof and interaction between subclasses)
Yes, that works because the compiler 'knows' that a dog is a Dog and a cat is a Cat etc. If that dog, cat etc. would've been of class Animal the compiler would generate a call to the superclass method instead for all cases. Method overloading is compiler thing.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-12-2012, 02:34 PM #9
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Implementation advice (instanceof and interaction between subclasses)
Which would explain why
results in RIP RIP. Makes sense I suppose. So in the above example, would you suggest using instanceof?Java Code:Dog dog = new Dog(); Cat cat = new Cat(); Rat rat = new Rat(); Animal[] ark = {new Dog(), new Cat(), new Rat()}; ark[0].eat(ark[1]); ark[0].eat(cat);
- 03-12-2012, 02:54 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,417
- Blog Entries
- 7
- Rep Power
- 17
Re: Implementation advice (instanceof and interaction between subclasses)
Probably; but I'd do it in the Dog, Cat, Rat classes themselves; e.g. the eat(Animal victim) method in the Dog class could look like this:
the inherited methods in the other classes are similar.Java Code:public boolean eat(Animal victim) { boolean edible= victim instanceof Cat; if (edible) yum(); else die(); return edible; }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-12-2012, 02:57 PM #11
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Implementation advice (instanceof and interaction between subclasses)
Ok, that's how I've been doing it. Guess that's something of an affirmation! You've been a great help, thanks.
- 03-12-2012, 03:11 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Implementation advice (instanceof and interaction between subclasses)
:)Java Code:public abstract class Animal { protected Set<Class> edible = new HashSet<Class>(); public boolean eat(Animal other) { if (edible.contains(other.getClass())) { yum(); return true; } die(); return false; } private void die() { System.out.println("RIP"); } protected void yum() { System.out.println("YUM"); } public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat(); Rat rat = new Rat(); System.out.print("Dog eat Cat: "); dog.eat(cat); System.out.print("Dog eat Rat: "); dog.eat(rat); System.out.print("Cat eat Rat: "); cat.eat(rat); System.out.print("Cat eat Dog: "); cat.eat(dog); System.out.print("Rat eat Dog: "); rat.eat(dog); System.out.print("Rat eat Cat: "); rat.eat(cat); } } class Dog extends Animal { public Dog() { this.edible.add(Cat.class); } } class Cat extends Animal { public Cat() { this.edible.add(Rat.class); } } class Rat extends Animal { public Rat() { this.edible.add(Dog.class); } }Please do not ask for code as refusal often offends.
- 03-12-2012, 03:15 PM #13
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Implementation advice (instanceof and interaction between subclasses)
Very nice. Did not even think of that one. Thanks!
- 03-12-2012, 03:18 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,417
- Blog Entries
- 7
- Rep Power
- 17
Re: Implementation advice (instanceof and interaction between subclasses)
Cool; instead of doing an instanceof test we do a set presence test ... plus an entire set per class. ;-)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-12-2012, 03:20 PM #15
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Implementation advice (instanceof and interaction between subclasses)
- 03-12-2012, 03:26 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Implementation advice (instanceof and interaction between subclasses)
Actually, it's an entire Set per instance of a class...:)
Never do things by halves, that's what I say.
It's not actually intended as a serious answer, but more to highlight the inheritance here.
As in, the inheritance is an illusion largely due to the simplicity of the problem, hence the problem surrounding how to get animal A to eat animal B without doing instanceof or class comparisons. And the answer is to provide data (attributes) to Animal that allow it to identify predator/prey. Not to subclass Animal.Please do not ask for code as refusal often offends.
- 03-12-2012, 03:29 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Implementation advice (instanceof and interaction between subclasses)
Hit Reply too soon!
Then end result would be something akin to an AnimalSpecies.
Individual Animals would have an AnimalSpecies.
The Species defines what other Species it can eat or be eaten by.
Composition.Please do not ask for code as refusal often offends.
- 03-12-2012, 03:34 PM #18
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Implementation advice (instanceof and interaction between subclasses)
So Tolls, you would suggest having
I'm not entirely sure though where down the rabbit's hole you escape doing set- or instanceof-based class comparisonsJava Code:Animal dog = new Animal(new AnimalSpecies(AnimalSpecies.DOG)); //DOG is enum in Species, Animal declaration takes a type of species and works with it
- 03-12-2012, 03:57 PM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Implementation advice (instanceof and interaction between subclasses)
Not Enums (unless you really know this is a fixed set of Species).
You'd have 3 instances (in your case) of an AnimalSpecies class.
Each instance would hold references to the species it could eat.
You would then have any number of instances of Animal class, each of which would hold a reference to one of the AnimalSpecies objects.
Figuring who could eat who then would be easy:
canEat(AnimalSpecies) simply does a check on the Set<AnimalSpecies> of things that species can eat, similar to the one I have above involving Class.Java Code:public boolean eat(Animal other) { if (this.animalSpecies.canEat(other.getAnimalSpecies())) { yum(); return true; } else { die(); return false; } }
That shoudl strip out the need to know Classes, and makes (in my mind) a neater model.Please do not ask for code as refusal often offends.
- 03-12-2012, 04:14 PM #20
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Similar Threads
-
need help with subclasses
By thorobred in forum New To JavaReplies: 6Last Post: 02-19-2011, 05:34 AM -
instanceof
By AedonetLIRA in forum New To JavaReplies: 3Last Post: 12-01-2010, 12:34 AM -
Class, SubClass type (InstanceOf) Question
By indyjoel in forum New To JavaReplies: 6Last Post: 11-07-2010, 03:55 AM -
Use of keyword instanceof
By darek9576 in forum New To JavaReplies: 3Last Post: 03-14-2010, 10:35 PM -
super instanceof Class?
By mikeiz404 in forum New To JavaReplies: 11Last Post: 01-23-2009, 07:23 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks