Wednesday, December 07, 2005

java vs c++ (1) object hierarchy

All classes in Java ultimately inherit from the Object class. This is significantly different from C++ where it is possible to create inheritance trees that are completely unrelated to one another.

The interface keyword in Java is used to create the equivalence of an abstract base class containing only method declarations and constants. No variable data members or method definitions are allowed. (True abstract base classes can also be created in Java.) The interface concept is not supported by C++.

Java does not support multiple inheritance. To some extent, the interface feature provides the desirable features of multiple inheritance to a Java program without some of the underlying problems.

In addition to the access specifiers applied to individual members of a class, C++ allows you to provide an additional access specifier when inheriting from a class. This latter concept is not supported by Java.

Examples:
a stack implementation in java
public interface Stack {
public void Push(Object o );
public Object Pop();
}
c++:
class Stack{
public:
virtual void Push(object&)=0;
virtual Object& Pop()=0;
}
the risk of "virtual" inheritance:
deadly diamond of death

0 Comments:

Post a Comment

<< Home