Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch 13. Features Found Only in Java Timothy Budd Oregon State University.

Similar presentations


Presentation on theme: "Ch 13. Features Found Only in Java Timothy Budd Oregon State University."— Presentation transcript:

1 Ch 13. Features Found Only in Java Timothy Budd Oregon State University

2 Ch 13. Features Found Only in Java2 Introduction The number of features found in Java that have no C++ counterpart is relatively small. We mention only the most notable items that have not discussed elsewhere.

3 Ch 13. Features Found Only in Java3 Wrapper Classes Java defines a “wrapper” class for each of the primitive data types. Primary reason is that primitive data types are not objects, thus cannot be assigned to a value of type Object, and hence cannot be stored in any of the standard data structures. In C++, template classes and functions eliminate most of the need for wrapper classes.

4 Ch 13. Features Found Only in Java4 Interfaces Interfaces describes the behavior associated with a class but does not provide an implementation. C++ does not have any exact equivalent; the closest is a method that does not have a body. class GraphicalObject { public: virtual draw() = 0; // every subclass must override };

5 Ch 13. Features Found Only in Java5 Interfaces Any class that inherits from a class the includes a pure virtual function must override these functions. An interface can be thought of as a class description in which every method has been declared as pure virtual.

6 Ch 13. Features Found Only in Java6 Inline Classes When need to create a class that will have only a single instance. Because the class name is not needed, Java provides a way for creating instatances of a class without naming the class: inline class. Inline classes are formed by naming the parent class and specifying whatever new or overridden methods the class requires. Cannot have constructors and cannot have more than one instance.

7 Ch 13. Features Found Only in Java7 Example of Inline Class class Example extends Frame { public Example () { Button a = new Button("quit");... a.addActionListener( new ActionListener() { // create in-line class public void actionPerformed(ActionEvent & e) { System.exit(1); // quit program } } ); } }

8 Ch 13. Features Found Only in Java8 Inline Classes While C++ does not have correspondence to the inline class feature, it is possible to create nested classes that do not have names. Such classes cannot have either constructors nor destructors.

9 Ch 13. Features Found Only in Java9 C++ Example of anonymous Class The example class contains an unnamed nested class that defines two data fields. An instance of this class is held by the field named data in the outer class: class Example { // C++ example of anonymous class public: private: class { public: int x; int y; } data; };

10 Ch 13. Features Found Only in Java10 Threads Java includes extensive facilities for multiprocessing as part of the basic language. In C++, not part of the language but instead are provided as external libraries. Thread programming is both more difficult and less portable than in Java.

11 Ch 13. Features Found Only in Java11 Reflection Java permits the program to discover information about the currently running program, this is called Reflection. Reflection begins with the class Class. Every object can be asked its class and will respond with an instance of this class. Using the class object, we can discover the methods that an object implements, and use the class object to create new instances of a class. A class object can return the class object that represents its parent class.

12 Ch 13. Features Found Only in Java12 Reflection The classes Field and Method provide similar information about data fields and members. Can even dynamically create, initialize, and load new classes into a running program. Not part of the standard language C++, although it is possible for objects to access their type as a string value. To a limited extent, reflection facilities can be implemented in a platform-specific manner by external libraries.


Download ppt "Ch 13. Features Found Only in Java Timothy Budd Oregon State University."

Similar presentations


Ads by Google