12 Replies - 477 Views - Last Post: 11 September 2012 - 09:17 PM Rate Topic: -----

#1 Zarithras  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Abstract Class Confusion

Posted 11 September 2012 - 07:03 PM

Hello everyone,

I've been plugging away at a recent endeavor and have run into an issue with abstract classes. I have a class that, within it, has an ArrayList of an object. Said object is an abstract class. I have a few classes extending this abstract class and providing specialized functionality. Now, I need some way to get an object of a certain type, and I need some way to also remove an object of a certain type. That is, I need to be able to check my ArrayList for one of the extended classes, and then from there I can either return it or delete it depending on the method. All that sounds pretty mixed up, so I'll provide an example:

public class ClassA {
   ArrayList<ClassB> objects = new ArrayList<ClassB>();

   public ClassB getClassB(ClassB) {
      // iterate through objects ArrayList and find what I need
   }

   public void removeClassB(ClassB) {
      // iterate through objects ArrayList and delete what I need
   }
}

public abstract class ClassB {
   // abstract class stuff
}

public class ClassC extends ClassB {
   // ...
}

public class ClassD extends ClassB {
   // ...
   ClassC myClassC;
   myClassC = ClassA.getClassB(myClassC); // Here's where the issue is
   // ...
   ClassA.removeClassB(ClassC);
}



So, I have a class that's maintaining a list of abstracted classes. I need to be able to check the list to see if I have an instance of one of the abstracted classes. If anymore clarifying needs to be done, let me know.

Any help would be appreciated! Thank you all ahead of time.

Is This A Good Question/Topic? 0
  • +

Replies To: Abstract Class Confusion

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Abstract Class Confusion

Posted 11 September 2012 - 07:43 PM

You can iterate, in reverse order, an remove the Object that are instanceof

    for(int i = al.size(); --i >= 0;) {
      Object o = al.get(i);
      if(o instanceof ClassToRemove)
          al.remove(i);
    }


Was This Post Helpful? 0
  • +
  • -

#3 Zarithras  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Re: Abstract Class Confusion

Posted 11 September 2012 - 08:02 PM

I'm not sure that'll work. I'm trying to define a method that takes in a parameter of abstracted type, iterates through the ArrayList, and if it finds an object of that type, it'll remove it. I don't want to have to create an instanceof check for each abstracted type, there has to be a better way.

For example, I'll use a farm.

public class Pen {
	public ArrayList<Animal> animals = new ArrayList<Animal>();
	
	public Animal getAnimal(Animal a) {
		// iterate through array and return animal of type a
	}
	
	public void removeAnimal(Animal a) {
		// iterate through array and return animal of type a
	}
}

public abstract class Animal {
	// ...
}

public class Sheep extends Animal {
	// ...
}

public class Horse extends Animal {
	// ...
}

public class Farm {
	// create a pen and add animals to it
	Horse myHorse = myPen.getAnimal(Horse);
	removeAnimal(Sheep);
}



That's, essentially, what I'm trying to accomplish with my code, except it's not a farm. ;)

The reason I'd like to not use instanceof and declare a case for each type that I make is because I'd like my code to be as extensible as possible. For example, if I add a new animal, I'd like to be able to just create the class and not have to add anything else to my Pen code.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,682
  • Joined: 19-March 11

Re: Abstract Class Confusion

Posted 11 September 2012 - 08:02 PM

View PostZarithras, on 11 September 2012 - 09:03 PM, said:

Hello everyone,


You'll have to imagine us all shouting back "hello, Zarithras!" in unison.
(it's a nice mental image, isn't it?)

Quote

So, I have a class that's maintaining a list of abstracted classes. I need to be able to check the list to see if I have an instance of one of the abstracted classes. If anymore clarifying needs to be done, let me know.

Any help would be appreciated! Thank you all ahead of time.


I'll thank you to not clarify any further, please. I don't think my head can take it. :)

I'd suggest that you can make life a bit easier by making a HashMap <Class, ClassB> and save the bother of iterating the list and doing all that instanceof nonsense. That's assuming you need that particular instance of ClassB, ClassC, or whichever class it is, or you'd just generate them on the fly (ie, have a getInstance method in ClassB that returns an instance - either a new one or a common one, as you like)
If that assumption holds, there might be more than one instance of a particular class that you're storing. In that case, you'd want to have a Map from Class to List<ClassB>, to allow for that possibility. The bugger of it is you're going to have to do some casting somewhere along the way, I think.
Maybe not, maybe someone clever has a trick.

This post has been edited by jon.kiparsky: 11 September 2012 - 08:04 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Abstract Class Confusion

Posted 11 September 2012 - 08:22 PM

All Objects have a getClass() method. Just pass an Object of the class you want

void remove(ClassB obj) {
   Class toRemove = obj.getClass();
   for(int i = al.size(); --i >= 0;) {
       Object o = al.get(i);
       if(o.getClass().equals(toRemove))
            al.remove(i);
   }
}


Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,682
  • Joined: 19-March 11

Re: Abstract Class Confusion

Posted 11 September 2012 - 08:29 PM

Nice... but you're still iterating the list. :)
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Abstract Class Confusion

Posted 11 September 2012 - 08:35 PM

What if you have more than one object of classAbc in the ArrayList
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,682
  • Joined: 19-March 11

Re: Abstract Class Confusion

Posted 11 September 2012 - 08:38 PM

Quote

there might be more than one instance of a particular class that you're storing. In that case, you'd want to have a Map from Class to List<ClassB>, to allow for that possibility.

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Abstract Class Confusion

Posted 11 September 2012 - 08:42 PM

I guess that if you have an ArrayList you will eventuualy want to pass through all its elements
With your Map you will have to scan first for the List and then each List
Was This Post Helpful? 1
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,682
  • Joined: 19-March 11

Re: Abstract Class Confusion

Posted 11 September 2012 - 08:50 PM

As always, it depends what you want to do with the objects when you get them and how well you've done your design. The better the design, the less casting you have to do, ideally ending up at zero.

But the requirements of the program will determine what you end up with, if you're smart enough to either get them right or to design to an interface and thereby leave the wiggle room to change your mind later.
Was This Post Helpful? 0
  • +
  • -

#11 Zarithras  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Re: Abstract Class Confusion

Posted 11 September 2012 - 08:57 PM

Thanks for all of the replies everybody, much appreciated. I did end up getting something working using the getClass() method that was suggested. If I have anymore questions and/or confusions I know where to go. :)
Was This Post Helpful? 0
  • +
  • -

#12 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Abstract Class Confusion

Posted 11 September 2012 - 09:03 PM

Typically, I find designing to interfaces is what causes this kind of casting mess. Especially when you're storing all the implementations in the same container. When you come home at night you put your change in the coin jar, but then all you have is a bunch of coins and you have to sort through them to get any value from them.

What's the equivalent of coin star in Java?
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,682
  • Joined: 19-March 11

Re: Abstract Class Confusion

Posted 11 September 2012 - 09:17 PM

View PostSheph, on 11 September 2012 - 11:03 PM, said:

What's the equivalent of coin star in Java?


Something involving getClass and iteration that you don't have to write, I think.

The latter being the important part, no?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1