CHAPTER 12 REVIEW
QUESTIONS
1. Name 2 functional languages that support OOP.
- classes and objects
2. What are the problems associated with programming
using abstract data types?
- In some cases, the design of an inheritance hierarchy
results in one or more classes that are so high in the hierarchy that an
instantiation of them would not make sense.
3. What is the advantage of inheritance?
- For example, suppose a program defined a Building class
and a collection of subclasses for specific types of buildings, for instance,
French_Gothic. It probably would not make sense to have an implemented draw
method in Building. But because all of its descendant classes should have such
an implemented method, the protocol (but not the body) of that
method is included in Building.
4. What is message protocol?
- The calls to methods are sometimes called messages. The
entire collection of methods of an object is called the message protocol, or
message interface, of the object.
5. What is overriding method?
- The purpose of an overriding
method is to provide an operation in the subclass that is similar to one in the
parent class,
but is customized for objects
of the subclass.
6. Describe a situation where
dynamic building is a great advantage over its absence.
- The third characteristic
(after abstract data types and inheritance) of objectoriented programming
languages is a kind of polymorphism3 provided by the dynamic binding of
messages to method definitions.
7. What is dynamic dispatch?
- This is sometimes called
dynamic dispatch. Consider the following situation: There is a base class, A,
that defines a method draw that draws some figure associated with the base
class. A second class, B, is defined as a subclass of A.
8. What is abstract
method/class?
- Such a method is often
called an abstract method ( pure virtual method in C++). A class that includes
at least one abstract method is called an abstract class (abstract base class
in C++). Such a class usually cannot be instantiated, because some of its
methods are declared but are not defined (they do not have bodies).
9. Describe briefly the eight
design issues used in this chapter for OOL.
- A number of issues must be
considered when designing the programming language features to support
inheritance and dynamic binding. Those that we consider most important are
discussed in this section.
10. What is inner class?
- Java has several varieties
of nested classes, all of which have the advantage of hidden from all classes
in their package, except for the nesting class. Nonstatic classes that are
nested directly in another class are called inner classes.
11. What is the message
protocol of an object?
- Passing a message is indeed
different from calling a subprogram. A subprogram typically processes data that
is either passed by its caller as a parameter or is accessed nonlocally or
globally. A message is sent to an object is a request to execute one of its
methods. At least part of the data on which the method is to operate is the
object itself.
12. From where smalltalk objects
allocated?
- Many think of Smalltalk as
the definitive object-oriented programming language. It was the first language
to include complete support for that paradigm. Therefore, it is natural to
begin a survey of language support for object-oriented programming with
Smalltalk
13. Explain how smalltalk
messages are bound to methods. When does this take place?
- In Smalltalk, the concept of
an object is truly universal. Virtually everything, from items as simple as the
integer constant 2 to a complex file-handling system, is an object. As objects,
they are treated uniformly. They all have local memory, inherent processing
ability, the capability to communicate with other objects, and the possibility
of inheriting methods and instance variables from ancestors. Classes cannot be
nested in Smalltalk.
PROBLEM SET
1. What important part of
support for inheritance is missing in java?
- There has long been pressure
on software developers to increase their productivity. This pressure has been
intensified by the continuing reduction in the cost of computer hardware. By
the middle to late 1980s, it became apparent to many software developers that
one of the most promising opportunities for increased productivity in their
profession was in software reuse.
2. In what ways can
“compatible” be defined for the relationship between an overridden method and
the overriding method?
- The overridden method can be
called in another method of the subclass through super, a reference to the
parent object. There is no way to prevent the overriding of an inherited
method. As in Smalltalk, in Objective-C any method name can be called on any
object. If the run-time system discovers that the object has no such method
(with the proper protocol), an error occurs.
3. Compare the inheritance of
C++ and Java.
- C#, which is based on C++
and Java, supports object-oriented programming. Objects can be instantiated
from either classes or structs. The struct objects are stack dynamic and do not
support inheritance. Methods in a derived class can call the hidden methods of
the parent class by including base on the method name. Methods that can be
overridden must be marked virtual, and the overriding methods must be marked
with override. All classes (and all
primitives) are derived from
Object.
4. Compare the class entity
access controls of Java and Ada 95.
- Ada 95 provides
support for object-oriented programming through tagged types, which can support
inheritance. Dynamic binding is supported with classwide pointer types. C#,
which is based on C++ and Java, supports object oriented programming. Objects
can be instantiated from either classes or structs. The struct objects are
stack dynamic and do not support inheritance.
5.
Compare abstract class and interfence in Java.
-
The collection of all instance variables for which access methods have been
provided forms the public interface to the class. Such instance data are called
attributes. Ruby classes are dynamic in the sense that they are executable and
can be changed at any time. Ruby supports only single inheritance, and
subclasses are not necessarily subtypes.
14.
Explain type checking in Java.
- The
process of verifying and enforcing the constraints of types – type checking –
may occur either at compile-time (a static check) or run-time (a dynamic
check). If a language specification requires its typing rules strongly (i.e.,
more or less allowing only those automatic type conversions that do not lose
information), one can refer to the process as strongly typed, if not, as weakly
typed. The terms are not usually used in a strict sense.
17.
What are the different options for object destrcutions in java?
- The
technique Java uses to get rid of objects once they are no longer needed is
called garbage collection. It is a technique that has been around for years in
languages such as Lisp. The Java interpreter knows what objects it has
allocated. It can also figure out which variables refer to which objects, and
which objects refer to which other objects. Thus, it can figure out when an
allocated object is no longer referred to by any other object or variable. When
it finds such an object, it knows that it can destroy it safely, and does so.
The garbage collector can also detect and destroy "cycles" of objects
that refer to each other, but are not referred to by any other objects.