2 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
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:
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.
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 {
@[Override](https://www.fiveableKeyTerm:override)
public static void callOne() {
System.out.println("3");
}
@Override
public void callTwo() {
System.out.println("4");
}
}
public class Main {
public static void main(String[] args) {
A a = new B();
a.callOne();
a.callTwo();
}
}
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.
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.
Term 1 of 7
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.
Term 1 of 7
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.
Term 1 of 7
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.
Method Overriding: Method overriding occurs when a subclass provides its own implementation for a method that is already defined in its superclass. This allows for customization of behavior specific to each subclass.
Dynamic Binding: Dynamic binding refers to determining which implementation of an overridden method should be called at runtime based on the actual type of the object rather than its declared type. It allows for flexibility in method invocation.
Interface: An interface is a collection of abstract methods that define a contract for classes to implement. It enables polymorphism by providing a common set of methods that different classes can implement.
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.
Dynamic Type: The dynamic type refers to the actual type of an object at runtime, which may be different from its static type.
Type Casting: Type casting is the process of converting an object from one data type to another.
Variable Declaration: Variable declaration involves specifying the name and data type (static type) of a variable before using it in a program.
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.
Static Type: The static type refers to the declared type of a variable or expression at compile-time.
Type Inference: Type inference is a feature in some programming languages where the compiler automatically determines the data type of a variable based on its assigned value.
Late Binding: Late binding is a mechanism where the specific implementation of a method is determined at runtime rather than compile-time.
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.
Class: A class is a blueprint or template for creating objects in object-oriented programming. Constructors belong to classes and define how objects of that class should be initialized.
Instance variables: Instance variables are attributes or properties associated with each instance (object) of a class. Constructors often assign initial values to these instance variables.
Overloading: Constructor overloading allows multiple constructors within the same class, but with different parameter lists. This provides flexibility in creating objects with varying initialization options.
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.
Subclass: A subclass is a class that extends another class (superclass). It inherits all the attributes and behaviors from its superclass while adding its own additional features.
Inheritance: Inheritance is the mechanism in Java where one class acquires properties (fields and methods) from another class. It allows code reuse and promotes hierarchical organization of classes.
Method Overriding: Method overriding occurs when a subclass provides its own implementation of a method that already exists in its superclass. The overridden method in the subclass replaces the original implementation provided by the superclass.