Book : Concept of Programming Language
Page 570
Review Question
2. The problems associated with programming using abstract data types are:
• 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. The advantage of inheritance is 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 is greatly
facilitated without requiring changes to the reused abstract data type.
4. Message protocol is the entire collection of methods of an object.
6. Describe a situation where dynamic binding is a great advantage over its absence.
- 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. Objects of this new class also need a draw method that
is like that provided by A but a bit different. With overriding, we can
directly modify B’s draw function. But without it, we either make a
specific function in A for B and inherit it.
7. 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. 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.
10. An inner class is a nonstatic class that is nested directly in another class.
12. All Smalltalk objects are allocated from the heap and are
referenced through reference variables, which are implicitly
dereferenced.
15. Smalltalk supports single inheritance; it does not allow multiple inheritance.
19. C++ heap-allocated objects are deallocated using destructor.
29. No Objective-C doesn’t support it. Objective-C only supports single inheritance.
31. The root class in Objective-C is called NSObject
38. 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.
49. Access control in Ruby is different for data than it is for
methods. All instance data has private access by default, and that
cannot be changed. If external access to an instance variable is
required, accessor methods must be defined.
Page 572
Problem Set
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.
• 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.
• The meaning of protected member access specifier is somewhat
different in Java. In Java, protected members of a class “A” are
accessible in other class “B” of same package, even if B doesn’t inherit
from A (they both have to be in the same package).
• Java uses extends keyword for inheritence. Unlike C++,
Java doesn’t provide an inheritance specifier like public, protected or
private. Therefore, we cannot change the protection level of members of
base class in Java, if some data member is public or protected in base
class then it remains public or protected in derived class. Like C++,
private members of base class are not accessible in derived
class. Unlike C++, in Java, we don’t have to remember those rules of
inheritance which are combination of base class access specifier and
inheritance specifier.
5. Compare abstract class and interface in Java.
• First and major difference between abstract class and interface is
that, 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.
• Second difference between interface and abstract class in Java is that
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.
• Third difference between abstract class and interface in Java is that
abstract class are slightly faster than interface because interface
involves a search before calling any overridden method in Java. This is
not a significant difference in most of cases but if you are writing a
time critical application than you may not want to leave any stone
unturned.
• Fourth difference between abstract class vs interface in Java is that,
interface are better suited for Type declaration and abstract class is
more suited for code reuse and evolution perspective.
• Another notable difference between interface and abstract class is
that 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 Enemy{
protected:
int Hp;
public:
void attack(){
cout<<”Enemy Attacks using GUN!!”<<endl;
}
};
class BossEnemy: public Enemy{
public:
void attack(){
cout<<”Boss Enemy attacks using MAGIC!!”<<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 superclass. This also tends to result in a better organization
of code and smaller, simpler compilation units.
17. What are the different options for object destruction in Java?
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.
Tidak ada komentar:
Posting Komentar