Review Questions
1. Name two functional languages that support object-oriented programming.
Answer:
C++ and Objective-c
2. What are the problems associated with programming using abstract data types?
Answer:
-In nearly all cases, the features and capabilities of the existing type are not quite right for the
new use.
-The type definitions are all independent and are at the same level.
3. What is the advantage of inheritance ?
Answer:
It can be reuse and modified
4. What is message protocol?
Answer:
Message protocol is the entire collection of methods of an object.
5. What is an overriding method?
Answer:
Overriding method is method that overrides the inherited method.
7. What is dynamic dispatch?
Answer:
Dynamic dispatch is the third characteristic (after abstract data types and inheritance) of object-oriented programming language which is a kind of polymorhphism provided by the dynamic binding of messages to method definitions.
10. What is an inner class?
Answer:
An inner class is a nonstatic class that is nested directly in another class.
12. From where are Smalltalk objects allocated?
Answer:
Smalltalk objects are allocated from the heap and are referenced through reference variables,
which are implicitly dereferenced.
19. How are C++ heap-allocated objects deallocated?
Answer:
C++ heap-allocated objects are deallocated using destructor.
29. Does Objective-C support multiple inheritances?
Answer:
No Objective-C doesn’t support it. (It supports only single inheritance).
38. What is boxing?
Answer:
Boxing is primitive values in Java 5.0+ which is implicitly coerced when they are put in object context. This coercion converts the primitive value to an object of the wrapper class of the primitive value’s type.
52. Does Ruby support multiple inheritance?
Answer:
No. it does not
PROBLEM SET
2. In what ways can “compatible “ be defined for the relationship between an overridden method and the overriding method?
Answer:
Every overriding method must have the same number of parameters as the overridden method and the types of the parameters and the return type must be compatible with those of the parent class.
3. Compare the inheritance of C++ and Java.
Answer:
– C++
◦ Forest of classes; when we create a class that doesn’t inherit from anything, we create a
new tree in forest.
◦ Private members of base class are not accessible in derived class.
◦ Provides inheritance specifier (public, protected, private).
◦ Interfaces and abstract classes are declared without using keywords.
◦ Virtual keyword are used explicitly.
◦ Default constructor of parent class is automatically called, but if we want to call
parametrized constructor of a parent class, we must use Initializer list.
◦ Support multiple inheritance.
– Java
◦ All classes inherit from the Object class directly or indirectly. If we create a class that
doesn’t inherit from any class then it automatically inherits from Object Class (root of the
tree).
◦ Members of the grandparent class are not directly accessible.
◦ Protected members of a class ‘A’ are accessible in other class ‘B’ of same package, even if
‘A’ doesn’t inherit from ‘B’ (they both have to be in the same package).
◦ Doesn’t provide inheritance specifier. Instead, extends keyword used for inheritance.
Therefore, we cannot change the protection level of members of base class in Java.
◦ Use a separate keyword interface for interfaces, and abstract keyword for abstract classes
and abstract functions.
◦ Methods are virtual by default.
◦ Default constructor of the parent class is automatically called, but if we want to call
parametrized constructor then we must use super to call the parent constructor.
◦ Doesn’t support multiple inheritance.
7. What is one programming situation where multiple inheritance has a significant disadvantage over interfaces?
Answer:
A situation when there are two classes derived from a common parent and those two derived
class has one child.
10. Explain one advantage of inheritance.
Answer:
• Inheritance offers a solution to both the modification problem posed by abstract data type
reuse and the program organization problem. If a new abstract data type can inherit the data
and functionality of some existing type, and is also allowed to modify some of those entities
and add new entities, reuse and is also allowed to modify some of those entities and add new
entities, reuse is greatly facilitated without requiring change to the reused abstract data type.
17. What are the different options for object destruction in Java?
Answer:
There is no explicit deallocation operator. A finalize method is implicitly called when the garbage collector is about to reclaim the storage occupied by the object.