Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

9.6 Polymorphism

2 min readjanuary 3, 2023

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Static and Dynamic Types

Now that we have established a little bit about polymorphism in the last topic, let's talk some more about it. Every object has a static type and a dynamic type. Let's use the hierarchy tree from the last topic:

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-buy28OuxvWUq.png?alt=media&token=fded642b-b752-4ddb-beae-77ff07362fd0

Courtesy of Wikimedia Commons.

Let's make an object a with the following constructor call:

A a = new C();

As we can see, the object has two types given to it, A, which is the type the variable is declared as, and C, which is the type that the constructor is calling. The type that the variable is declared as is known as the variable's static type, while the type of the constructor call is known as the variable's dynamic type. These will be useful to know when calling methods.

Method Calling and Polymorphism

Before we move on, consider the following code:

public class A {
  public static void callOne() {
    System.out.println("1");
  }

  public void callTwo() {
    System.out.println("2");
  }
}

public class B extends A {
  @

Here, we have two classes, A and B, where A is a superclass of B. They each have two methods, callOne(), which is static, and callTwo(), which is not static. The methods in B override those of A. We have made an object with static type A and dynamic type B. When calling callOne() and callTwo(), what will be printed?

The key lies in mentioning that callOne() is static while callTwo() is not. Because callOne() is static, when we call it from a, we use its static type, so callOne() from class A is called and "1" is printed. Meanwhile, because callTwo() is not static, when we call it from a, we use its dynamic type, so callTwo() from class B is called and "4" is printed.

Key Terms to Review (7)

Constructor

: A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.

Dynamic Type

: The dynamic type refers to the actual type of an object at runtime. It may differ from its static (declared) type and is determined based on the specific instance assigned to it during program execution.

Method Calling

: Method calling refers to invoking or executing a method in Java code. It involves using the method's name followed by parentheses and passing any required arguments inside those parentheses.

override

: Overriding refers to providing a different implementation for a method inherited from a superclass or interface in its subclass or implementing class respectively. It allows the subclass to customize the behavior of inherited methods.

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.

Static Type

: The static type refers to the declared type of a variable or expression at compile-time. It determines the set of operations that can be performed on the variable or expression.

Superclass

: A superclass, also known as a parent class or base class, is a class that is extended by another class (subclass). It provides common attributes and behaviors that can be inherited by its subclasses.

9.6 Polymorphism

2 min readjanuary 3, 2023

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Static and Dynamic Types

Now that we have established a little bit about polymorphism in the last topic, let's talk some more about it. Every object has a static type and a dynamic type. Let's use the hierarchy tree from the last topic:

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-buy28OuxvWUq.png?alt=media&token=fded642b-b752-4ddb-beae-77ff07362fd0

Courtesy of Wikimedia Commons.

Let's make an object a with the following constructor call:

A a = new C();

As we can see, the object has two types given to it, A, which is the type the variable is declared as, and C, which is the type that the constructor is calling. The type that the variable is declared as is known as the variable's static type, while the type of the constructor call is known as the variable's dynamic type. These will be useful to know when calling methods.

Method Calling and Polymorphism

Before we move on, consider the following code:

public class A {
  public static void callOne() {
    System.out.println("1");
  }

  public void callTwo() {
    System.out.println("2");
  }
}

public class B extends A {
  @

Here, we have two classes, A and B, where A is a superclass of B. They each have two methods, callOne(), which is static, and callTwo(), which is not static. The methods in B override those of A. We have made an object with static type A and dynamic type B. When calling callOne() and callTwo(), what will be printed?

The key lies in mentioning that callOne() is static while callTwo() is not. Because callOne() is static, when we call it from a, we use its static type, so callOne() from class A is called and "1" is printed. Meanwhile, because callTwo() is not static, when we call it from a, we use its dynamic type, so callTwo() from class B is called and "4" is printed.

Key Terms to Review (7)

Constructor

: A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.

Dynamic Type

: The dynamic type refers to the actual type of an object at runtime. It may differ from its static (declared) type and is determined based on the specific instance assigned to it during program execution.

Method Calling

: Method calling refers to invoking or executing a method in Java code. It involves using the method's name followed by parentheses and passing any required arguments inside those parentheses.

override

: Overriding refers to providing a different implementation for a method inherited from a superclass or interface in its subclass or implementing class respectively. It allows the subclass to customize the behavior of inherited methods.

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.

Static Type

: The static type refers to the declared type of a variable or expression at compile-time. It determines the set of operations that can be performed on the variable or expression.

Superclass

: A superclass, also known as a parent class or base class, is a class that is extended by another class (subclass). It provides common attributes and behaviors that can be inherited by its subclasses.


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