English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

explain

2007-07-18 23:02:14 · 3 answers · asked by alpa s 1 in Computers & Internet Programming & Design

3 answers

Why is it that important?

Being fully OOP doesn't make Java more advanced that C++, on the contrary, it makes it programming language that is unable to face non-object applications, while C++ can be used to develop both library-based applications and class-based applications.

C was developed a few decades ago, when OOP didn't even exist (if you can call a structure-based program OOP, then you can say it existed) and the OOP part of C appeared with C++. and classes.

One more reason is that classes used to be slower than library-based programming some time ago. Now, even net-based languages like PHP tend to implement and encourage the use of OOP. I guess that things changed meanwhile and that they developed a faster compiling method that will enable classes to be as fast as good old function libraries.

This OOP vs non-OOP debate is useless in my opinion. If you want a well-organized application using top-notch technologies, I sugges you go for classes. On the other hand, if you just want to develop an application that works and you don't give a damn about the technology used, I sugges function libraries.

As a conclusion, these are 2 different ways of thinking and C++ supports both of them because lots of programmers still depends on libraries to get the job done and it would be a really stupid decision to just develop new compilers that will read only OOP code.

2007-07-18 23:42:00 · answer #1 · answered by Anonymous · 0 0

In fact if you all want to get down to the really technical part the only fully OOP language is Smalltalk and family.

But still true Java is more so OOP because using older non-OOP codes are not in its design criteria but C++ had that criteria in being able to use C codes, which is non-OOP plus C++ was designed and last updated in a world that OOP still meant crappy performance.

2007-07-19 06:12:44 · answer #2 · answered by Andy T 7 · 0 0

C++
OOP has been widely available to developers for about 15 years, mainly because of the popularity of the C++ programming language. However, because of its C heritage, C++ is not completely object-oriented; you can write C++ programs that do not use object-oriented features. C++ is considered as hybrid language. C++ used to be the most common OOP language,

JAVA
Java is completely object-oriented: every Java program requires the presence of at least one class (an object-oriented feature). Furthermore, Java's official language definition includes the term object-oriented. Java is a popular Internet language.

The biggest potential stumbling block is speed: interpreted Java runs in the range of 20 times slower than C.

Compile-Time vs. Runtime Type Checking

C++ and Java favor compile-time type-checking, more or less extensively.
C++ is probably the less precise, and Java the one enforcing type checking at the highest extent. The reason is that C++ maintains compatibility with the C languages, which supports compile-time type-checks, but in a very light way. For example C and C++ consider the arithmetic type as being all compatible (although when assigning a float to an int the compiler issues a warning). In Java a Boolean value is not an integer, and a characters is another different and incompatible type.

The fact that the Java virtual machine "interprets" the byte-code at runtime, doesn't mean that the language gives up the compile-time type-checking. On the contrary, in this language the checking is more thorough. Other OOP languages as Smalltalk and CLOS, instead, tend to do most if not all of the type checking at runtime

Hybrid vs. Pure OOP Languages

Another differences in among pure and hybrid OOP languages. Pure OOP languages are those allowing only one programming model, OOP. You can declare classes and methods, but you cannot have plain old functions and procedures, and global data.

Only Java is a pure OOP language (as Eiffel and Smalltalk are), which at first seems a very positive idea. However, you end up using a lot of static methods and static data, which is not that different from using global functions or data, beside the more complex syntax. In my opinion pure OOP languages offer an advantage for OOP newcomers, because programmers are forced into using (and learning) the object oriented programming model. C++ is typical example of hybrid languages, which allow programmers to use traditional C and Pascal programming approaches.

Smalltalk extended this concept of having only objects up to the level of defining as objects also the predefined data types, as integers and characters, and the language constructs (as the looping instructions). This is theoretically interesting, but reduces the efficiency quite a lot. Java stops much earlier, allowing the presence of native, non OOP, data types (although there are wrapper classes for the native types).

Plain Object Model vs. Object Reference Model
A third element discriminating OOP languages is their object model. Some traditional OOP languages allow programmers to create objects on the stack, the heap and the static storage. In these languages a variable of a class data type corresponds to an object in memory. This is how C++ works.

Lately, there seems to be a trend to use a different model , called object reference model. In this model every object is allocated dynamically on the heap, and a variable of a class type is actually a reference or a handle to the object in memory (technically something similar to a pointer). Java adopted this reference model. As we'll see in a while this means that you should remember to allocate memory for the object.

Classes, Objects, and References

Feature: Since we are discussing OOP languages, after this introduction, the starting point is to discuss classes and objects. I hope everyone clearly understands the difference between these two terms: in short, a class is a data type, an object is an instance of a class type. Now how to we use objects in languages using different object models?

C++: In C++, if we have a class MyClass with the method MyMethod, we can write
MyClass Obj;
Obj.MyMethod();
and get an object of the MyClass class named Obj. The memory for this object is typically allocated on the stack, and you can start using the object right away, as in the second line above.

Java: In Java a similar instruction allocates only the memory space for the handle to the object, not for the object itself:
MyClass Obj;
Obj = new MyClass();
Obj.MyMethod();
Before you use the object, you have to call new to allocate the memory for the object. Of course, you should declare and initialize the object with a single statement whenever possible, to avoid using un-initialized objects handles:
MyClass Obj = new MyClass();
Obj.MyMethod();

Note: If the object reference model seems to require more work for the programmer, keep in mind that in C++ you often have to use pointers to objects and references to objects. Only using pointers and reference for example, you can get polymorphism. The object reference model, instead, makes the pointers the defaults, but does a good job hiding them. In Java, in particular, there are officially no pointers, but pointers are everywhere. Only programmers have no direct control over them, so that they cannot access random memory locations, for security reasons.

The Recycle Bin

Feature: Once you have created and used an object you need to destroy it, to avoid using unnecessary memory.

C++: In C++ destroying objects allocated on the stack is fairly simple. Handling the destruction of object allocated dynamically, on the other hand, is often a complex issue. There are many solutions including reference counting and smart pointers, but this is far from a simple issue. The first impression for C++ programmers is that using a reference object model makes the situation even worse.

Java: This is certainly not the case with Java, since the virtual machine runs a garbage collection algorithm in the background. This is something programmers get for free, but something that might adversely affect the performance of applications. Not having to write destructors may lead to logical errors in the cleanup code.

Defining New Classes

Feature: Now that we've seen how to create objects of the existing classes, we can focus on the definition of new classes. A class is simply a collection of methods, operating on some local data.

C++: This is the C++ syntax of a simple class definition:
class Date {
private:
int dd;
int mm;
int yy;
public:
void Init (int d, int m, int y);
int Day ();
int Month ();
int Year ();
};
And here is the definition of one of the methods:
void Date::Init (int d, int m, int y)
{
dd = d;
mm = m;
yy = y;
}

Java: Java syntax is very similar to C++ syntax:
class Date {
int dd = 1;
int mm = 1;
int yy = 1;
public void Init (int d, int m, int y) {
dd = d; mm = m; yy = y; }
public int Day () { return dd; }
public int Month () { return mm; }
public int Year () { return yy; }
}
The biggest difference is that the code of each method is written directly when it is declared (without making the function an inline function, as happens in C++), and that you can initialize the data members of the class. Actually if you fail do to so Java initializes all the data members for you, using a default value.

Constructors

Feature: The above class is terribly simple. The first thing we can add to it is a constructor, which is a good technique for solving the problem of objects initialization.

C++: In C++, as in Java, constructors have the same name of the class. If you don't define any constructor the compiler synthesizes a default constructor, adding it to the class. In both these languages you can have multiple constructors thanks to methods overloading.

Java: Everything works as in C++, although constructors are also called initializers. This highlights the fact that is the Java virtual machine to create the objects, while the code you write in a constructor simply initializes the newly created object. (The same actually happens also in Object Pascal.)

Destructors and finalize()
Feature: A destructor has the opposite role of a constructor, and is generally called when an object is destroyed. If most classes need a constructor, only few of them need a destructor. A destructor should basically free resources allocated by the constructor (or by other methods during the life of the objects). These resources include memory, files, database tables, Windows handles, and so on.
C++: C++ destructors are automatically called when an objects goes out of scope, or when you delete a dynamically allocated object. Every class can have only one destructor.
Java: Java has no destructors. Objects without references are destroyed by the garbage collection algorithm, which runs as a background task. Prior to destroying an object the garbage collector calls the finalize() method. However there is no guarantee that this method is actually called (at least in Java 1.0). For this reason if you need to free resource you should add a custom method, and ensure it is called.

Class Encapsulation (Private and Public)

Feature: A common element of the three language is the presence of three access specifiers indicating different levels of class encapsulation, public, protected, and private. Public means visible by every other class, protected means visible by derived classes, private means no external visibility. The details, however, are different.

C++: In C++ you can use the friend keyword to by pass encapsulation. The default visibility for a class is private, for a struct is public.

Java: In Java, a syntactical difference is that access specifiers are repeated for every class member. A more concrete difference is the default in Java is friendly, which means the element is visible also by other classes of the same package (or source code file, similarly to what happens in OP). Similarly, the protected keyword indicates the visibility to subclasses, but also to other classes of the same package, while the private protected combinations corresponds to C++ protected.

Files, Units, and Packages
Feature: An important difference between the three languages is the organization of the source code in files. All three languages use files as standard mechanism for storing the source code (differently from other OOP languages, as Smalltalk), but while the C++ compiler doesn't understand files, the OP and Java compilers do. Both languages work with the idea of module, although the concept assumes a different name.
C++: In C++, programmers tend to place class definitions in header files, and method definitions in separate code files. Usually the two files have the same name and a different extension. A compilation unit generally refers (includes) its own declaration file plus the declaration files for the classes (or functions) the code refers to. These are all conventions the compiler doesn't enforce. This means that the linker has a lot of work to do, because the compiler cannot anticipate in which other module a method will be defined.
Java: In Java, each source code file, or compilation unit, is compiled separately. You can then mark a group of compilation units as being part of a single package. Differently than the other two languages, you write all of the code of the methods as you declare the class. When a file is included, with an import statement the compiler reads only its public declarations, not all of its code:
import where.myclass;
import where.* // all the classes
Note: Modules as name spaces. Another important difference is that Java compilers can read a compiled file and extract its definition, as it you were extracting the header file from the compiled code. As an aside, the C++ language introduced namespaces to overcome the absence of a module structure. In Java, in fact, when two names clash, you can generally prefix the name with the module name. This doesn't require the extra work of set up namespaces, but it's built in the languages.

Class/Static Method and Data

Feature: OOP languages generally allow to have some methods and data which relates to the class as a whole, not to specific objects. Class method generally can be called both for an object of the class or applied to the class type as a whole. Class data is data not replicated for each object, but shared among them.

C++: In C++ class method and data are indicated by the static keyword. Class data must be initialized with a specific declaration: this is one of the drawbacks of the absence of modules.
Java: Java uses the same keyword of C++, static. Static methods are used very often (and even overused) because of the absence of global functions. Static data can be directly initialized in the class declaration
Classes and Inheritance

Feature: Inheritance among classes is one of the foundations of OOP. It can be used to express generalization or specialization. The basic idea is that you define a new type extending or modifying an existing type, in other words a derived class has all of the data and methods of the base class, plus new data and method, and eventually modifies some of the existing methods. Different OOP languages have a different jargon for the mechanism (derivation, inheritance, subclassing), the class you inherit from (base class, parent class, super class), and the new inherited class (derived class, child class, subclass).

C++: C++ uses the keywords public, protected, and private to define the flavor of inheritance, and hide the inherited methods or data, turning them to private or protected. Although the public inheritance is the most used version, the default is private inheritance. C++ is the only of these three languages with multiple inheritance, as we'll see later on. Here is an example of the syntax:

class Dog: public Animal {
...
};

Java: Java uses the extends keyword to express the only kind of inheritance, which corresponds to public inheritance in C++. Java has no support for multiple inheritance. Java classes have a common base class, as we'll see later on.

class Dog extends Animal {
...
}

Note: Base class constructors and initialization. Constructors of inherited classes have a complex structure both in C++ and Java. In Object Pascal this it is the programmers responsibility to initialize the base class. This topic is quite complex, so I've skipped it in this presentation. Instead I'll focus on the common base class, access to the base class, multiple inheritance, interfaces, late binding, and other related topics.
The Mother of All Classes
Feature: In some OOP languages every class has at least a default base class. This class, often called Object or something similar, has some basic capabilities shared by all classes. This base class, in fact, is the class from which all the other classes ultimately inherit. This is a common approach because it's what Smalltalk originally did.
C++: Although the C++ language doesn't have these feature, many application frameworks based on it introduce the idea of a common base class. MFC is a notable example, with its CObject class. Actually this made more sense at the beginning, when the language lacked features as templates (and even more when it lacked multiple inheritance.
Java: all the classes implicitly inherit from the Object class. Also in this language the common base class has limited capabilities

2007-07-19 12:35:00 · answer #3 · answered by angel04 3 · 2 0

fedest.com, questions and answers