Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Find what you need to study

9.5 Creating References Using Inheritance Hierarchies

3 min readjanuary 2, 2023

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Inheritance Hierarchies

Now that we are done with making our subclass, let's look at from a user standpoint.

can be thought as an upside down tree with the "root" on the top and the "branches" on the bottom. The root is the superclass while the branches are the subclasses of this superclass. A visual representation of this tree is called a or .

Here is the that we will look at for the next two topics:

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-gTiRkkb1UFII.png?alt=media&token=ae860a2d-47f4-458e-981f-526345a7d8c5

Courtesy of Wikimedia Commons

Here, we can see the relationship between classes visually which may be easier to analyze than the actual code itself. The class headers for the above showing which are subclasses of what classes are as follows:

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

Thus, we have the following relationships:

  • A is the superclass to B and H
  • B is the superclass to C
  • C is the superclass to D
  • H is the superclass to G and I
  • I is the superclass to E and F

However, superclasses and subclasses can also be indirect. Since C is a subclass of B and B is a subclass of A, then C is also a subclass of A.

This is important when we construct objects of a subclass. Let's take class C for example. There are many ways in which we can make an object c of class C. This is as follows:

C c = new C();
B c = new C();
A c = new C();

This is essentially what is. Since C is a subclass of both B and A, c is also an object of B and A. This is like saying that AP CSA, an AP subject, is also a subject.

states that we can have multiple types for one object of a subclass. However, the following is illegal:

C b = new B();
C a = new A();

This is because an object of type B or type A is not necessarily an object of type C. If the above were true, this would be like saying that any subject, even PE, is an AP subject, which makes no sense (even though AP PE should be a thing).

can be helpful when we write the list of parameters in a method header or when we create arrays or ArrayLists. For example, if we declare that a method will take in an object of type A, then we would also be able to pass in objects of type B, type C, or type D. This holds for arrays and ArrayLists as well, where we could declare one of these data structures to hold objects of type A, then also store any objects of type B, type C, or type D.

Key Terms to Review (5)

Hierarchy Tree

: A hierarchy tree is a graphical representation that illustrates the hierarchical relationship between different elements or entities. It organizes them into levels or layers based on their parent-child relationships.

Inheritance

: Inheritance is a concept in object-oriented programming where a class inherits the properties and behaviors of another class. It allows for code reuse and promotes the creation of hierarchical relationships between classes.

Key Term: Object

: An object is an instance of a class that encapsulates data and behavior. It represents a real-world entity or concept in the program.

Polymorphism

: Polymorphism refers to the ability of objects to take on multiple forms or have multiple types. In programming, it allows different objects to be treated as instances of a common superclass, enabling flexibility and extensibility.

Type Diagram

: A type diagram is a visual representation that shows the relationships between different types of objects in a program. It helps programmers understand how classes and interfaces are related to each other.

9.5 Creating References Using Inheritance Hierarchies

3 min readjanuary 2, 2023

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Inheritance Hierarchies

Now that we are done with making our subclass, let's look at from a user standpoint.

can be thought as an upside down tree with the "root" on the top and the "branches" on the bottom. The root is the superclass while the branches are the subclasses of this superclass. A visual representation of this tree is called a or .

Here is the that we will look at for the next two topics:

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-gTiRkkb1UFII.png?alt=media&token=ae860a2d-47f4-458e-981f-526345a7d8c5

Courtesy of Wikimedia Commons

Here, we can see the relationship between classes visually which may be easier to analyze than the actual code itself. The class headers for the above showing which are subclasses of what classes are as follows:

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

Thus, we have the following relationships:

  • A is the superclass to B and H
  • B is the superclass to C
  • C is the superclass to D
  • H is the superclass to G and I
  • I is the superclass to E and F

However, superclasses and subclasses can also be indirect. Since C is a subclass of B and B is a subclass of A, then C is also a subclass of A.

This is important when we construct objects of a subclass. Let's take class C for example. There are many ways in which we can make an object c of class C. This is as follows:

C c = new C();
B c = new C();
A c = new C();

This is essentially what is. Since C is a subclass of both B and A, c is also an object of B and A. This is like saying that AP CSA, an AP subject, is also a subject.

states that we can have multiple types for one object of a subclass. However, the following is illegal:

C b = new B();
C a = new A();

This is because an object of type B or type A is not necessarily an object of type C. If the above were true, this would be like saying that any subject, even PE, is an AP subject, which makes no sense (even though AP PE should be a thing).

can be helpful when we write the list of parameters in a method header or when we create arrays or ArrayLists. For example, if we declare that a method will take in an object of type A, then we would also be able to pass in objects of type B, type C, or type D. This holds for arrays and ArrayLists as well, where we could declare one of these data structures to hold objects of type A, then also store any objects of type B, type C, or type D.

Key Terms to Review (5)

Hierarchy Tree

: A hierarchy tree is a graphical representation that illustrates the hierarchical relationship between different elements or entities. It organizes them into levels or layers based on their parent-child relationships.

Inheritance

: Inheritance is a concept in object-oriented programming where a class inherits the properties and behaviors of another class. It allows for code reuse and promotes the creation of hierarchical relationships between classes.

Key Term: Object

: An object is an instance of a class that encapsulates data and behavior. It represents a real-world entity or concept in the program.

Polymorphism

: Polymorphism refers to the ability of objects to take on multiple forms or have multiple types. In programming, it allows different objects to be treated as instances of a common superclass, enabling flexibility and extensibility.

Type Diagram

: A type diagram is a visual representation that shows the relationships between different types of objects in a program. It helps programmers understand how classes and interfaces are related to each other.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.