Beruflich Dokumente
Kultur Dokumente
Class inheritance it’s a mechanism for code sharing. (when a class uses code constructed
within another class.)
*PICTURE:
Straight out of the book
Shows simple implementation of inheritance
By class hierarchy using linked list.
The elements are linked from the subclass to its immediate superclass
(parent class)
Each element represents a class, the pointers are highlighting the
different implementation of the methods that defined or redefined in
that class.
SLIDE:
Java and C# enable single inheritance only
C++ support multiple inheritance.
Single inheritance:
only allows one class to be derived from the base class.
PICTURE:
1. B is a subclass of A,
2. B’s vtable contains a copy of A’s vtable and redefined methods are
appropriately modified.
3. The method invocation costs only two indirect accesses.
4. IT comes to show that it is possible to access an object with a
reference that belongs statically to one of its super classes.
Multiple inheritance:
DEFINITION: Multiple inheritance is when a class can inherit attributes and
methods from more than one parent class.
This can allow programs have more complex relations,
but it can also introduce a lot of confusion when two base classes
implement a method with the same name.
In EXAMPLE:
Coral_reef is child class inherits from a Coral class and
a Sea_anemone class.
We can create a method in each and then use the pass keyword in
the Coral_reef child class:
The object great_barrier is assigned to CoralReef object, and now can use the
methods in both parent classes.
Dynamic Method:
&& with each variable and method name comes recorded information including the
class where the names are defined and their type.
Every time that the source code uses the name, the intermediate code of the
JVM looks up the index(by IDX to save space) of that name in the constant pool
Execution happens when reference is made to a name for the first time by
INDEX. JVM loads up the necessary classes in which that name is introduced,
visibility checks, type checks are performed. Then JVM saves a pointer to this
info. So, the next time same name is used. Don't have to perform all the
checks again.