Browse By Unit
3 min readβ’june 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
Now that we are done with making our subclass, let's look at inheritance from a user standpoint.Β
Inheritance 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Β type diagram orΒ hierarchy tree.Β
Here is the hierarchy tree that we will look at for the next two topics:
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:
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Β polymorphism 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.Β
Polymorphism 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).
Polymorphism 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.Β
<< Hide Menu
3 min readβ’june 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
Now that we are done with making our subclass, let's look at inheritance from a user standpoint.Β
Inheritance 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Β type diagram orΒ hierarchy tree.Β
Here is the hierarchy tree that we will look at for the next two topics:
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:
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Β polymorphism 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.Β
Polymorphism 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).
Polymorphism 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.Β
Β© 2024 Fiveable Inc. All rights reserved.