Review Questions
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.
12. From where are Smalltalk objects allocated?
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?
Smalltalk supports single inheritance; it does not allow multiple inheritance.
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).
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
3. Compare the inheritance of C++ and Java.
- In Java, all classes inherit from the Object class directly or
indirectly. Therefore, there is always a single inheritance tree of
classes in Java, and Object class is root of the tree. In Java, if we
create a class that doesn’t inherit from any class then it automatically
inherits from Object Class. In C++, there is forest of classes; when we
create a class that doesn’t inherit from anything, we create a new tree
in forest.
- In Java, members of the grandparent class are not directly accessible.
- 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.
- 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.
- Unlike C++, Java doesn’t support multiple inheritance. A class
cannot inherit from more than one class. A class can implement multiple
interfaces though.
– In C++, default constructor of parent class is automatically
called, but if we want to call parametrized constructor of a parent
class, we must use Initalizer list. Like C++, default constructor of the
parent class is automatically called in Java, but if we want to call
parametrized constructor then we must use super to call the parent
constructor
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 becauseJava
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 plan{
public:
void fly(){
cout << “fly” << endl;
}
};
class jet:public class plan{
public:
void fly()
{
cout << “Fly! Rocket jet activated!” << endl;
}
}
10. Explain one advantage of inheritance.
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. Programmers can begin
with an existing abstract data type and design a modified descendant of
it to fit a new problem requirement. Furthermore, inheritance provides a
framework for the definition of hierarchies of related classes that can
reflect the descendant relationship in the problem space.
12. Compare inheritance and nested classes in C++. Which of these supports an is-a relationship?
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.
Inheritance does.
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