🌳 Chapter 1.4 – Type Hierarchies and Inheritance
📚 Hello, Ethio students! Today we look at one of the most powerful ideas in OODB: inheritance and type hierarchies. We will understand how classes are organized like a family tree. Let’s go deep! 🇪🇹
🧬 1.4.1 What is Inheritance?
From the PDF: “Object oriented database system permits the definition of new types based on other predefined types which leads to type (class) hierarchy. Data and functions are organized in a hierarchy. Objects inherit characteristics and functions of their ancestor objects.”
In simple words: A subclass (child) gets all properties (attributes + methods) from its superclass (parent). Then it can add its own unique properties. This is called reusability – we don’t need to rewrite common things.
Explanation: Mammal inherits head, body, feed() from Animal, and adds four legs + sit(). Fish inherits from Animal and adds swim(). Every mammal is an Animal, but not every Animal is a mammal.
🎓 Important point: If class B is a subclass of class A, then every instance of B is automatically an instance of A. The reverse is not true. Example: every dog (subclass) is an animal, but not every animal is a dog.
📝 Practice – inheritance basics
Q1 (Mid): In an OODB, a subclass inherits which of the following from its superclass?
A) only attributes
B) only methods
C) both attributes and methods
D) only the OID
Q2 (Exit style): Which statement is TRUE?
A) If B is a subclass of A, then every A instance is also a B instance
B) If B is a subclass of A, then every B instance is also an A instance
C) Subclasses cannot override inherited methods
D) Inheritance only works for methods, not attributes
🧩 1.4.2 Superclass, Subclass – the is‑a relationship
The PDF says: “These special classes are known as subclass, and the more general classes are known as superclass.” Example: Vehicle is a superclass; Car, Bike are subclasses. They inherit common features (wheels, speed) and add specifics.
Key exam point: A subclass can redefine (override) inherited methods. For instance, a Bird class inherits move() from Animal, but may override it with flying behaviour.
🔁 Overriding example
Suppose Person has a method greet() that prints “Hello”. Student may override it to print “Hello, I’m a student”. The method name and signature stay the same, but implementation changes.
Q3 (Final): Which of the following is TRUE about a subclass in OODB?
A) It cannot have its own unique properties
B) It inherits only the state, not the behaviour
C) It can redefine inherited methods (override)
D) It cannot be used as a type
Q4: Given: class Animal { void sound() { print(“generic”); } }
class Dog extends Animal { void sound() { print(“bark”); } }
What concept does this demonstrate?
A) encapsulation
B) overriding
C) overloading
D) persistence
📊 1.4.3 Type (Class) Hierarchy
The PDF says: “It classifies entities into subclasses.” A hierarchy shows the relationships among classes. There are two ways to build a hierarchy: specialization and generalization. They are opposites.
🔻 Specialization (top‑down)
We start with a superclass and then define subclasses that are more specific. Example: start with Vehicle, then create Car, Truck. The superclass is defined first.
🔺 Generalization (bottom‑up)
We start with subclasses and then extract common features into a superclass. Example: we have Car and Motorcycle; we generalize them into Vehicle. Subclasses are defined first.
Both result in a hierarchy. The exam may ask: “Distinguish between specialization and generalization.”
Q5 (Mid): Which process involves defining subclasses starting from an existing superclass?
A) Generalization
B) Specialization
C) Aggregation
D) Encapsulation
Q6 (Exit): Generalization is the process of:
A) defining subclasses from a superclass
B) combining subclasses into a higher‑level superclass
C) hiding attributes
D) creating objects
♻️ 1.4.4 Code Reusability through Inheritance
One major benefit: we don’t have to repeat code. Common features go into superclass; subclasses reuse them. This is called code reusability. The PDF states: “Sharing of data within hierarchy scope supports code reusability.”
📌 Exam tip: They may ask: “Why is inheritance called a reusability mechanism?” Answer: Because subclasses automatically get the properties of superclasses – you write once, use many times.
Example from the PDF: Animals hierarchy again
Animals: head, body, feed() Mammals: head, body, feed + four legs, sit() Fish: head, body, feed + swim() Mammals reuse the head, body, feed from Animals – no need to rewrite.
Also note: multiple inheritance? The PDF doesn’t explicitly mention, but in some OODB a class can inherit from more than one superclass. However, the Ethiopian module focuses on single inheritance tree.
Q7 (Final): “Subclass inherits all properties of its superclass.” This directly supports which principle?
A) encapsulation
B) code reusability
C) persistence
D) object identity
Q8: In the hierarchy Employee → Manager, what does Manager inherit?
A) only employee ID
B) all attributes and methods of Employee
C) nothing, it’s independent
D) only methods, not attributes
📋 1.4.5 Key terms – quick comparison
| Term | Definition | Example |
|---|---|---|
| Superclass | General class (parent) | Animal |
| Subclass | Specialised class (child) inherits from superclass | Mammal, Fish |
| Inheritance | Mechanism to derive new class from existing | class Mammal extends Animal |
| Specialization | Top‑down: superclass → subclasses | Vehicle → Car, Truck |
| Generalization | Bottom‑up: subclasses → superclass | Car, Bike → Vehicle |
| Overriding | Subclass redefines inherited method | Dog overrides sound() |
📝 Advanced practice (Exit level)
Q9: Consider: class A { void show() { print(“A”); } }
class B extends A { void show() { print(“B”); } }
A obj = new B(); obj.show(); What is printed?
A) A
B) B
C) error
D) nothing
Q10: Which statement is TRUE about a type hierarchy?
A) A class can be a subclass of multiple classes (multiple inheritance) – but not in all OODB
B) Every class must have exactly one subclass
C) Subclasses cannot have additional methods
D) Inheritance reduces reusability
🎯 Exam tips – type hierarchies & inheritance
- Understand the is‑a relationship: subclass object is also an instance of superclass.
- Know the difference between specialization (top‑down) and generalization (bottom‑up).
- Inheritance promotes code reusability – you don’t repeat common features.
- Subclasses can override methods.
- Be able to draw a simple hierarchy (like Animal → Mammal → Dog) and explain.
✅ Chapter 1.4 – summary
- Inheritance: subclass inherits from superclass (attributes + methods).
- Every subclass instance is also a superclass instance.
- Type hierarchy: classification into superclasses/subclasses.
- Specialization: defining subclasses (super first).
- Generalization: combining subclasses into superclass (sub first).
- Overriding allowed, code reusability main advantage.
📚 Keep going! You are building strong OODB knowledge. Share ethiotemari.com with friends.