CONCEPT OF PROGRAMMING LANGUAGES CHAPTER 12

Lecturer: Tri Djoko Wahjono, Ir, M.Sc.

Name: Adhy Wiranata Prasetyo

NIM: 1601215733

Class: 02PBT

REVIEW QUESTIONS
2. What are the problems associated with programming using abstract data types?

  • 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?

Message protocol is the entire collection of methods of an object.

5. What is an overriding method?

Overriding method is method that overrides the inherited method.

7.What is dynamic dispatch?

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?

An abstract method is an implemented method which all of descendant class should have and it is
included in Building. An abstract class is a class that includes at least one abstract method.

11. What is the message protocol of an object?

The message protocol of an objects are all the methods.

12. From where are Smalltalk objects allocated?

Smalltalk objects are allocated from the heap and are referenced through reference variables,
which are implicitly dereferenced.

18. From where can C++ objects be allocated?

The objects of C++ can be static, stack dynamic, or heap dynamic. Explicit deallocation using
the delete operator is required for heap-dynamic objects, because C++ does not include implicit
storage reclamation.

19. How are C++ heap-allocated objects deallocated?

C++ heap-allocated objects are deallocated using destructor.

29. Does Objective-C support multiple inheritance?

No Objective-C doesn’t support it. (It supports only single inheritance).

31. What is the root class in Objective-C?

The predefined root class named NS Object

33. What is the purpose of an Objective-C category?

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?

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?

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?

Java does not support the private and protected derivations of C++. One can surmise that the Java designers believed that subclasses should be subtypes, which they are not when private and protected derivations are supported. Thus, they did not include them.

2. In what ways can “compatible “ be defined for the relationship between an overridden method
and the overriding method?

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!

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
– 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 B
doesn’t inherit from A
– Java uses extends keyword for inheritance
– We don’t have to remember those rules of inheritance which are combination of base class
access specifier and inheritance specifier
– Methods are virtual by default
– Uses a separate keyword interface for interfaces and abstract keyword for abstract classes and
abstract functions

C++:

  • Private members of base class are not accessible in derived class
  • Have the forest of classes. When we create a class that doesn’t inherit from anything, we create a new tree in forest
  • Support the multiple inheritance
  • Default constructor of parent class is automatically called, but if we want to call parametrized the constructor of a parent class, we must use Initalizer list
  • Use virtual keyword

5. Compare abstract class and interface in Java!
– Abstract class is a class while interface is a interface, means by extending abstract class
you can not extend another class because Java does not support multiple inheritance but you can
implement multiple inheritance in Java.

– You can not create non abstract method in interface, every method in interface is by default
abstract, but you can create non abstract method in abstract class. Even a class which doesn’t
contain any abstract method can be abstract by using abstract keyword.

– The abstract class are slightly faster than interface because interface involves a search
before calling any overridden method in Java

– The interface are better suited for Type declaration and abstract class is more suited for
code reuse and evolution perspective

– When you add a new method in existing interface it breaks all its implementation and you need
to provide an implementation in all clients which is not good. By using abstract class you can
provide default implementation in super class

7. What is one programming situation where multiple inheritance has a significant disadvantage
over interfaces?

A situation when there are two classes derived from a common parent and those two derived class
has one child.

9. Give an example of inheritance in C++, where a subclass overrides the superclass methods!
class bird{

public:
void fly(){
cout << “Fly, Fly away!” << endl;
}
};
class ostrich:public class bird{
public:
void fly()
{
cout << “Fly, Fly away! But I can’t!” << endl;
}
}
10. Explain one advantage of inheritance.

One of the key benefits of inheritance is to minimize the amount of duplicate code in an application by sharing common code amongst several subclasses. Where equivalent code exists in two related classes, the hierarchy can usually be refactored to move the common code up to a mutual super class. This also tends to result in a better organization of code and smaller, simpler compilation units.

20. Compare the way Smalltalk provides dynamic binding with that of C++

In C++, the programmer can specify whether static binding or dynamic binding is to be used. Because static binding is faster, this is an advantage for those situations where dynamic binding is not necessary. Furthermore, even the dynamic binding in C++ is fast when compared with that of Smalltalk. Binding a virtual member function call in C++ to a function definition has a fixed cost, regardless of how distant in the inheritance hierarchy the definition appears. Calls to virtual functions require only five more memory references than statically bound calls (Stroustrup, 1988). In Smalltalk, however, messages are always dynamically bound to methods, and the farther away in the inheritance hierarchy the correct method is, the longer it takes. The disadvantage of allowing the user to decide which bindings are static and which are dynamic is that the original design must include these decisions, which may have to be changed later.

25. Study and explain private and public modifiers in C++. How do those modifiers differ in C#?

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 inheritance.

Tinggalkan komentar