zuai-logo

What is an inheritance hierarchy?

A tree-like structure representing the relationships between superclasses and subclasses.

All Flashcards

What is an inheritance hierarchy?
A tree-like structure representing the relationships between superclasses and subclasses.
What is a superclass?
The parent class in an inheritance hierarchy.
What is a subclass?
A class that inherits from a superclass.
What is a type diagram (hierarchy tree)?
A visual representation of the inheritance relationships between classes.
What is polymorphism?
The ability of an object to take on many forms; an object of a subclass can be treated as an object of its superclass.
Identify the error in the following code: ```java class A {} class B extends A {} class C extends B {} C b = new B(); ```
Cannot assign a superclass object (B) to a subclass reference (C).
Given the following class hierarchy, which of these object instantiations are valid? ```java class A {} class B extends A {} class C extends B {} 1. A a = new A(); 2. B b = new B(); 3. C c = new C(); 4. A a = new B(); 5. A a = new C(); 6. B b = new C(); 7. C c = new B(); 8. C c = new A(); ```
1, 2, 3, 4, 5, and 6 are valid. 7 and 8 are invalid because you can't assign a superclass object to a subclass reference.
Given the following class definitions, is `I e = new E();` valid? ```java public class A public class B extends A public class C extends B public class D extends C public class E extends I public class F extends I public class G extends H public class H extends A public class I extends H ```
Yes, `I e = new E();` is valid because E is a subclass of I.
Given the following class definitions, is `A g = new G();` valid? ```java public class A public class B extends A public class C extends B public class D extends C public class E extends I public class F extends I public class G extends H public class H extends A public class I extends H ```
Yes, `A g = new G();` is valid because G extends H, and H extends A, therefore G is a subclass of A.
Given the following class definitions, is `B f = new F();` valid? ```java public class A public class B extends A public class C extends B public class D extends C public class E extends I public class F extends I public class G extends H public class H extends A public class I extends H ```
No, `B f = new F();` is not valid because F does not extend B, and there is no inheritance relationship between them.
Explain the concept of inheritance.
Inheritance allows a class to inherit properties and methods from a superclass, promoting code reuse and establishing an 'is-a' relationship.
Explain how subclasses inherit from superclasses.
Subclasses inherit the non-private members (attributes and methods) of their superclasses. They can also add new members or override inherited methods.
What does it mean for a superclass and subclass to be indirect?
If class C is a subclass of B, and B is a subclass of A, then C is also indirectly a subclass of A.
Why is polymorphism useful?
It allows for more flexible and reusable code, enabling methods to work with objects of different classes in a unified way.
How does polymorphism relate to inheritance?
Polymorphism relies on inheritance, as it allows objects of subclasses to be treated as objects of their superclasses.
Why is `C b = new B();` illegal?
An object of type B is not necessarily an object of type C. You cannot assign a superclass object to a subclass reference.
How can polymorphism be helpful in method parameters?
If a method takes an object of type A, you can pass in objects of type B, C, or D if they inherit from A.
How can polymorphism be helpful when creating arrays or ArrayLists?
You can declare a data structure to hold objects of type A, then store objects of type B, C, or D if they inherit from A.