Advanced Database Systems

0 of 4 lessons complete (0%)

Chapter 1: Concepts for Object-Oriented DatabasesAdvanced Database Systems

1.4. Type Hierarchies and Inheritance

This is a preview lesson

Register or sign in to take this lesson.

Type Hierarchies & Inheritance – CoSc 2042 | Ethio Temari
Advanced Database Systems: Type hierarchies, inheritance, subclass/superclass, specialization/generalization. For Ethiopian students – exam prep.

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

ANIMAL HIERARCHY (example from PDF) ┌─────────────┐ │ Animal │ │ head, body │ │ feed() │ └──────┬──────┘ │ ┌───────────┴───────────┐ │ │ ┌───────▼───────┐ ┌───────▼───────┐ │ Mammal │ │ Fish │ │ four legs │ │ swim() │ │ sit() │ └───────────────┘ └───────────────┘

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

✅ C – both attributes and methods. By default, subclass inherits all properties (state and behaviour).

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

✅ B – every B is an A. That’s the ‘is‑a’ relationship. Subclasses can override methods, so C is false.

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

┌─────────────────────┐ │ Person (superclass)│ │ name, birthDate │ │ getAge() │ └──────────┬──────────┘ │ ┌────────┴────────┐ │ │ ┌─────▼─────┐ ┌───────▼─────┐ │ Student │ │ Professor │ │ major │ │ salary │ │ study() │ │ teach() │ └───────────┘ └─────────────┘

🔁 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

✅ C – overriding is allowed. Subclasses can add new properties and override methods.

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

✅ B – overriding. Dog provides its own implementation of sound().

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

SPECIALIZATION (top-down) GENERALIZATION (bottom-up) ┌────────────┐ ┌────────────┐ ┌────────────┐ │ Vehicle │ (super first) │ Car │ │ Motorcycle │ (sub first) └─────┬──────┘ └─────┬──────┘ └──────┬─────┘ │ │ │ ┌────┴────┐ └───────┬────────┘ │ │ │ ┌──▼───┐ ┌──▼───┐ ┌────▼────┐ │ Car │ │ Truck│ │ Vehicle │ (super later) └──────┘ └──────┘ └─────────┘

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

✅ B – Specialization. It’s top‑down: superclass first, then subclasses.

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

✅ B – combining subclasses into a superclass. Opposite of specialization.

♻️ 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

✅ B – code reusability. Reusing existing code/attributes is the core benefit.

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

✅ B – all attributes and methods. Manager is a subclass of Employee.

📋 1.4.5 Key terms – quick comparison

TermDefinitionExample
SuperclassGeneral class (parent)Animal
SubclassSpecialised class (child) inherits from superclassMammal, Fish
InheritanceMechanism to derive new class from existingclass Mammal extends Animal
SpecializationTop‑down: superclass → subclassesVehicle → Car, Truck
GeneralizationBottom‑up: subclasses → superclassCar, Bike → Vehicle
OverridingSubclass redefines inherited methodDog 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

✅ B – “B”. Even though reference is A, object is B, and overridden method in B executes. This is polymorphism.

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

✅ A is partially true – some OODB support multiple inheritance, but the Ethiopian curriculum focuses on single. However, among the options, A is the only one that can be true in some contexts. But check your module: They probably stick to single inheritance. Let’s check the PDF: It doesn’t discuss multiple inheritance, so we assume single. So perhaps none is correct? Wait, B, C, D are definitely false. So A might be the answer if they allow. But exam tip: In OODB, multiple inheritance is possible but not always. I’d say: according to the PDF, they show single hierarchy. So be careful. Anyway, B, C, D are false.

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

Scroll to Top