I am learning about generics in Java, and was wondering if this would be considered poor coding.
If I declare an ArrayList<Object>
I know that I can put any type of object into this list, as all objects descend from Object. I also know that when I call ArrayList.remove(index)
the object that gets taken out of the array is of type Object, and that I need to typecast it to the type of object that I want to use.
Now suppose I have object of Dog
, Cat
, and Car
. Would it be bad to put all three objects into the array, considering they are not similar classes?
A generic Abstract Window Toolkit(AWT) container object is a component that can contain other AWT components. Components added to a container are tracked in a list. The order of the list will define the components' front-to-back stacking order within the container.
In Java, we can create more than one object of the same class, the syntax of the object creation will be the same as we followed in the previous example: This is how you can create multiple objects of a class.
An object can have multiple types: the type of its own class and the types of all the interfaces that the class implements. This means that if a variable is declared to be the type of an interface, then its value can reference any object that is instantiated from any class that implements the interface.
Let us revise the definition of a Collection
A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group....(continued).
Look at the bold words. That should give you your answer.
If you put objects in ArrayList
which do not belong to same implementations, you are definitely inviting trouble in situations when you want to retrieve objects from the list and typecast them to appropriate types. So you should AVOID doing that.
Consider you store objects of Dog
and Car
in ArrayList
.
Then for each object stored, you want to call some method, say barkLoudly()
. Now,this method will work fine with the Dog
object. But when this is called on Car
, boooom....An Exception
will arise.
I think the answer is "usually, but not always." There's not a lot of hard and fast rules in coding; if you look hard enough you can always find an exception. But if you pull objects out of a collection that you then have to cast to the correct type, that's usually a design error.
Try to make code so that the objects you work with already have the correct type for what you want to do. If you don't need to cast, then Object can be fine. If you do need to cast, you should probably rethink your design.
The design will be really bad, to avoid all such kind of issues, Generics were introduced. But the same you can achive using below, provided you have same type of objects.
Cat and Dog extends Animal
Car and Bike extends Vehicle
But do not mix the the above two 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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With