Several types of objects in Java containers

I study generics in Java and wonder if this would be considered bad encoding.

If I declare an ArrayList<Object> , I know that I can put any type of object on this list, since all objects descend from Object. I also know that when I call ArrayList.remove(index) , the object that is pulled from the array is of type Object, and I need to give it the type of object I want to use.

Now suppose I have a Dog , Cat and Car object. Would it be bad to put all three objects in an array, given that they do not look like classes?

+7
java containers
source share
6 answers

We redefine the definition of a Collection

A collection, sometimes called a container, is simply an object that groups several items into one unit. Collections are used to store, retrieve, process, and share aggregate data . Typically, they represent data elements that form a natural group .... (continued).

Look at the bold words. That should give you an answer.

If you put objects in an ArrayList that do not belong to the same implementation, you are certainly causing problems in situations where you want to retrieve objects from a list and infer them into the appropriate types. Therefore, you must avoid to do this.

Consider that you store Dog and Car objects in an ArrayList . Then for each saved object you want to call some method, say barkLoudly() . Now this method will work perfectly with the Dog object. But when it is called on Car , boooom .... An Exception will appear.

+4
source share

I think the answer is "usually, but not always." There are not many hard rules in coding; if you look closely enough, you can always find an exception. But if you are pulling objects from the collection, which then need to be superimposed on the correct type, this is usually a design error.

Try to make the code so that the objects you work with already have the correct type for what you want to do. If you do not need to throw, then the object may be in order. If you need to quit, you should probably rethink your design.

+2
source share

The design will be very poor in order to avoid all such problems, Generics were presented. But the same can be achieved using below if you have objects of the same type.

Cat and dog expand animals

Car and bike expand vehicle

But do not mix the above statements.

Animal Class:

 public abstract class Animal { protected String name; } 

Dog Class:

 public class Dog extends Animal { public Dog(String name) { this.name=name; } } 

Cat Class:

 public class Cat extends Animal { public Cat(String name) { this.name=name; } } 

Main class:

 public class Main { public static void main(String[] args) { Cat Cat = new Cat("C"); Dog Dog = new Dog("D"); ArrayList<Animal> list = new ArrayList<Animal>(); list.add(Cat); list.add(Dog); } } 
+1
source share

You shouldn't do that

Let's look at it this way, if we call a common Object simply β€œa thing” and you create a list of (n) ( Array ) only β€œthings”, then how useful is this for you? To store these three things, which seems to have nothing in common in one list, makes no sense. Containers have been added so programmers can store groups of similar things.

If we look at this from a greater point of view of programming, rather than from a real point of view, this will cause more problems. Your code may try to perform an action that only Car can perform, the compiler does not like that your Dog class just tried turnOnEngine() , and throws an error that you did not expect.

In short, it’s bad practice to add unrelated elements to the collection.

+1
source share

The goal of generics is to get the compiler to do all type checks for us. So we don’t need to worry about casting or remember which types are really on the list. Thus, using generics also helps document your code, making the hidden type more explicit.

You can:

  • do not use generics and force check your own type (safe downcasting with instanceof)
  • use generic files with separate lists for each type
  • use generics with abstraction for the associated class, for example: List<Animal>
+1
source share

It depends on what you want to use List for.

You should declare the value of the generic List type to be the most restrictive that suits your needs, and in this way you will get the strongest check from the compiler to fix errors during compilation.

There are times when you really want to store objects of any type in a List , in which case you would use a List<Object> . Again, it depends on what you want List to use for the elements as well.

0
source share

All Articles