Click here to Skip to main content
15,884,058 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to put all the objects that i created them from different classes in one ArrayList!!

What I have tried:

i want to put all the objects that i created them from different classes in one ArrayList!!
Posted
Updated 13-May-16 12:33pm
Comments
Patrice T 13-May-16 16:19pm    
Then do it, Start working.
Comeback when you have a question.

1 solution

I don't think the term "consolidation classes" means something definitive in this context. What you are talking about is one of the most fundamental programming things, one of the core concepts of OOP, polymorphism: Polymorphism (computer science) — Wikipedia, the free encyclopedia[^].

Note that the class ArrayList<E> is a genetic class: ArrayList (Java Platform SE 7)[^].

The fact of having a generic class has nothing to do with polymorphism or OOP in general, but it gives you a way to define a compile-time type of the type of a list element. The fundamental of OOP polymorphism is this: it should be either the interface type, or a class which can be extended. Practically, the class should have virtual methods, usually abstract or pseudo-abstract; and the class is usual abstract, but not always. This interface of class type, being a compile-time type, allows to be assigned to some derived classes of several different runtime types. The set of those different types makes you the polymorphic set of objects of those different types. That already answers your question.

At this point, you have to understand OOP rules and rationale behind assignment compatibility between derived classes or classes implementing a interface. You also should understand those runtime types vs compile-time ones.

Formally, virtual methods (and properties) are not required, but without them the whole activity would be pointless. The whole idea is to operate all objects of the polymorphic set (of your list, in your case) exclusively by the members of your base class/interface. In other words, you should never inquirer what is one or another derived (runtime) type, and this is only possible via late binding (dynamic dispatch) based on virtual methods/properties: Dynamic dispatch — Wikipedia, the free encyclopedia[^].

In practice, this principle is sometimes violated via the down-casting and dynamic cast. It this is done very rarely only when it is really simplifies design without much added risk, this is considered to be normal.

Finally, added power of interface is multiple inheritance. The same very object can implement two different interface and hence participate in two different polymorphic sets, or more. This benefit is not used often though.

Please see my past answers:
what is the use of abstract class ? and why can't we create object of abstract class?[^],
How Do I Implement "String Functions" And "Poor Man's Polymophism"[^],
The use of polymorphism ?[^],
How does .net understands about type casting[^],
run-time polymorphism VS compile-time polymorphism[^].

Sorry, only fist of the answers referenced above is specific to Java, others are pretty much universal and based on .NET, so some minor points could be inapplicable to Java; you don't need to pay much attention for that; the differences are really minor.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900