Rabu, 26 Juni 2013

Chap 12 - Review Question and Problem Set - Concepts of Programming Languages, Sebesta

CONCEPTS OF
PROGRAMMING LANGUAGES
TENTH EDITION
Robert W. Sebesta
Chapter 12-Support for Object-Oriented Programming
Review Question

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.

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.

8. What is an abstract method ? What is an abstract class ?
Answer: a method which all descendant classes should have. An abstract class is a class which implement abstract method.

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.

15. What kind of inheritance, single or multiple, does Smalltalk support?
Answer: Smalltalk supports single inheritance; it does not allow multiple inheritance.

19. How are C++ heap-allocated objects deallocated?
Answer: C++ heap-allocated objects are deallocated using destructor.

25. What is mixins in objective-C?
Answer: Mixins are sometimes used to add certain functionalities to different classes. And, of course, the class still has a normal superclass from which it inherits members. So, mixins provide some of the benefits of multiple inheritance, without the naming collisions that could occur if modules did not require module names on their functions.

29. Does Objective-C support multiple inheritance?
Answer: No Objective-C doesn’t support it. (It supports only single inheritance).

33. What is the purpose of an Objective-C category?
Answer: The purpose of an Objective-C category is to add certain functionalities to different classes and also to provide some of the benefits of multiple inheritance, without the naming collisions that could occur if modules did not require module names on their functions.

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.

39. How are Java objects deallocated?
Answer: By implicitly calling a finalizemethod when the garbage collector is about to reclaim the storage occupied by the object.


Problem Set

1 . What important part of support for inheritance is missing in Java?
Answer: Java does not support the private and protected access control, unlike in C++ user can determine which data member can be inherit by using protected access control.

3. Compare the inheritance of C++ and Java.
Answer:
-In Java, all objects are Inherited, either directly or indirectly. While in C++ a class can be defined to stand on its own without an ancestor.
-In Java, methods are virtual by default. In C++, we explicitly use virtual keyword.
-Java uses a separate keyword interface for interfaces, and abstract keyword for abstract classes and abstract functions.
-In Java, there’s no access level specifier of inheritance, while C++ has private public and protected.
-Unlike C++, Java doesn’t support multiple inheritance. A class cannot inherit from more than one class. A class can implement multiple interfaces though.

7. What is one programming situation where multiple inheritance has a significant disadvantage over interfaces ?
Answer:
When two or more parent classes are derived from one grandparent class and they have one same child. (diamond problem)

9. Given an example of inheritance in C++, where a subclass overrides the superclass methods.
Answer:

class car{

public :
void engine()
{std::cout<<”120 hp”;}
}
class sportcar : public car{
public :
void enginge()
{std::cout<<”580 hp”;}
}


10. Explain one advantage of inheritance
Answer:
One advantage of inheritance is more easier for update of a program. Because child classes are inherited from the parent to if there are any update the programmer can only focus on the parent.
The attitude or function in the parent will automatically inherited to the children.

12. Compare inheritance and nested classes in C++. Which of these supports an is-a relationship?
Answer:
Inheritance is where one class (child class) inherits the members of another class (parent class).Nested class is a class declared entirely within the body of another class or interface.

17. What are the different options for object destruction in Java?
Answer:
Finalize is related to C++ destructor. A finalize method is implicitly called when the garbage collector is about to reclaim the storage occupied by the object. The problem with finalize is that the time it will run cannot be forced or even predicted. The alternative to using finalize to reclaim resources held by an object about to be garbage collected is to include a method that does the reclamation. The only problem with this is that all clients of the objects must be aware of this method and remember to call it.

21. Compare the support for polymorphism in C# with that of in Objective-C.
Answer:
In Objective-C, polymorphism is implemented in a way that differs from the way it is done in most other common programming languages. A polymorphic variable is created by declaring it to be of type id. Such a variable can reference any object. The run-time system keeps track of the class of the object to which
an id type variable refers. If a call to a method is made through such a variable, the call is dynamically bound to the correct method, assuming one exists.
To allow dynamic binding of method calls to methods in C#, both the base method and its corresponding methods in derived classes must be specially marked. The base class method must be marked with virtual, as in C++. To make clear the intent of a method in a subclass that has the same name and protocol as a virtual method in an ancestor class, C# requires that such methods be marked override if they are to override the parent class virtual method.

25. Study and explain private and public modifiers in C++. How do those modifiers differ in C#?
Answer:
C++ includes both classes and structs, which are nearly identical constructs. The only difference is that the default access modifier for class is private, whereas for structs it is public. C# also has structs, but they are very different from those of C++. In C#, structs are, in a sense, lightweight classes. They can have constructors, properties, methods, and data fields and can implement interfaces but do not support

Special thanks to Mr. Tri Djoko Wahjono, Ir., M.Sc.

Tidak ada komentar:

Posting Komentar