Saturday, February 9, 2013
2/9/13 Polymorphism
Have you ever wanted to create a arraylist that can only contain objects with a certain method? Believe it or not, this is completely possible using an idea called Polymorphism. You can make your arraylist only contain a certain interface, making it be so that all classes that implement the interface can be put into your arraylist. This has many advantages to it, including the ability to use the sort method without risking an error because an object in the list dose not have a compareTo method. This can also be used for objects in a game that are destroyed when they collide. In this scenario, you can have all the objects implement a collidable method and then create an arraylist of collidable objects. I want to point out that you can also do this with abstract super classes. If you have a supperclass called shape, subclasses for different shapes, and an arraylist of type shape, you could use a for each loop and print the area of each shape. I am sorry that this article is sort of short, but I have a lot of homework to get done. Anyway, I hope you learned a thing or two about java and Polymorphism.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment