Tell me more ×
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It's 100% free, no registration required.

I do not have any argument opposing why we need only a single universal class. However why not we have two universal classes, say an Object and an AntiObject Class. In nature and in science we find the concept of duality - like Energy & Dark Energy; Male & Female; Plus & Minus; Multiply & Divide; Electrons & Protons; Integration & Derivation; and in set theory. There are so many examples of dualism that it is a philosophy in itself. In programming itself we see Anti-Patterns which helps us to perform work in contrast to how we use Design patterns. I am not sure, but the usefulness of this duality concept may lie in creating garbage collectors that create AntiObjects that combine with free or loose Objects to destruct themselves, thereby releasing memory. Or may be AntiObjects work along with Objects to create a self-modifying programming language - that allows us to create a safe self modifying code, do evolutionary computing using genetic programming, do hiding of code to prevent reverse engineering.

We call it object-oriented programming. Is that a limiting factor or is there something fundamental I am missing in understanding the formation of programming languages?

share|improve this question
1  
Languages like C++ do not have any root class, so you can build own Object and AntiObject classes if you like. – swegi Aug 7 '12 at 7:57
@swegi If C++ is superior in this regard then what were the reasons we are moving towards Typed languages - C# and Java. In my understanding, Typed languages provide simplicity and things like Garbage collection, etc. Also people have quoted problems with Multiple inheritance before. In this dual system I want classes to either derive from Object or AntiObject but the system does not allow multiple inheritance. – kaushal Aug 7 '12 at 8:55
2  
It is not a question of being superior or otherwise. C# and Java are more modern languages which were designed based on the lessons learned from C++ and other research programming languages. Garbage collection is orthogonal to typing. Both Java and C# avoid the multiple inheritance problem by allowing interfaces separate from classes. There is no technical reason for disallowing two roots to the object hierarchy. – Dave Clarke Aug 7 '12 at 9:27
In the application to garbage collecting or evolutionary programming, what benefit would introducing an "AntiObject" class bring to these tasks? A process which combined objects and their opposites to collect garbage could just as easily collect garbage without the auxiliary antiobject, for instance. So the short answer is simply that an AntiObject class hasn't actually suggested itself as a useful tool. As for dualism: if you're keen on philosophy as a motivator for PL design, you may as well adopt Hegel's position that the two opposites may be subsumed in a more general Synthesis. – Niel de Beaudrap Aug 7 '12 at 13:43
@kaushal: Typed languages and simplicity, garbage collection, etc are independent of each other. You can add garbage collection to C, for example by adding a custom memory allocation library. C++ is strongly typed if compared to C, but not so strongly typed if compared to most functional languages (e.g. SML, OCaml, etc) because of the casting mechanisms. Multiple Inheritance is needed in C++ to have interfaces. Protocols like in Objective-C are not possible in C++ due to its static nature (unless you build your own runtime). – swegi Aug 7 '12 at 14:39
show 3 more comments

2 Answers

up vote 6 down vote accepted

This is only an answer to answer the question in the title.

Languages such as Java have every class deriving from Object for two reasons.

Firstly, to increase the amount of polymorphism available. This was particularly required before generics were added to the language. Without Object, collection classes would be impossible to write in a useful fashion.

Secondly, there are many methods that classes are expected to have or are useful, and these are collected in Object. By ensuring that all classes inherit from Object, all classes will implement the same minimal interface.

As mentioned in a comment, C++ does not have a class like Object. C++ is in many ways untyped, so the issues I mention above are not applicable. Also, C++ templates provide a lot of polymorphism and are used to implement collections.

share|improve this answer
Thanks. Please see my comment above in the question. – kaushal Aug 7 '12 at 8:56

I think the accepted answer covers pretty much the original question, but I would like to complement it slightly by throwing in (quite informally) a few ideas regarding the secondary questions.

From an inheritance point of view, nothing prevents the class hierarchy to have several roots. As it has been pointed out by other peoples, C++, as well as many OO languages don't constraint expressiveness to a single root ancestor class.

However, from a type theoretic point of view (recall that inheritance and subtyping are not the same thing, so I am probably stepping out of the frame of the main question here), a single "top" supertype makes a lot of sense (depending on the type theory of course). For instance, in OCaml, there is a common supertype to all objects (whether class instances, or immediate objects), written < > to denote that the object type is empty. This seems indeed the most general object type we can define, since we cannot remove anything from it to make it more general. Hence, in this conception of object types, there is necessarily a supertype to all object.

Regarding the dual of the root object, scala sports a class called Nothing, which curiously is also empty, and is the subtype of every other classes. It cannot be instantiated, but holds enough useful semantic to implement the empty list, which is called Nil, and is equal to List[Nothing] (as pointed out in comments, it is possible that programmer never directly use that value in most case, making it seemingly not as useful as it is). Nothing could be considered a dual of the root type - called Any in scala, however these types not only cover classes, but also primitives types, so that everything can be upcasted to Any, for instance.

share|improve this answer
1) In Scala, List[Nothing] is the minimal inferred type for the empty list, but hardly ever useful. Usually, you want to provide type information to get a (usable) type such as List[String]. 2) You say, "a single 'top' supertype makes a lot of sense", but you don't give a reason. – Raphael Feb 2 at 13:54
@Raphael you make 2 very valid points, thank you! I hope my updated answer addresses them somehow. – didierc Feb 2 at 15:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.