🐻
berkeley
  • #Despair
  • 16A/B
    • Thevenin and Norton Equivalence
    • Resistive Touchscreen
    • Trilateration and Correlation
    • RC Circuits & Transient Analysis
  • 61B
    • Testing
    • References in Java
    • Linked Lists in Java
    • Implementation Inheritance
    • Comparables and Iterators
    • Casting
    • Lists and Sets
    • Asymptotics
    • Trees
    • Hashing
    • PriorityQueue and Heap
    • Graphs, Tree Traversals
    • Tries
    • Sorting
  • Discrete Mathematics
    • Sets + Mathematical Notation
    • Logic and Proofs
    • Induction
    • Cryptography/RSA
    • Modular Arithmetic
    • Discrete Probability
  • Linear Algebra
    • Linear Equations
    • Matrix Algebra
    • Determinants
    • Vector Spaces
    • Eigenvectors and Eigenvalues
    • Projections and Orthogonality
    • SVD/PCA
  • Math 275 — Ordinary Differential Equations
    • Math 275 — Overview
    • Modeling via Differential Equations
    • Linear Systems
  • INTEGBI 35AC
    • Humans Came From Fish
    • Evolution's History
    • Typology
    • The Human Diaspora
  • 170
    • Basics
    • Divide and Conquer
    • Fast Fourier Transform
    • Graphs
    • Greedy
  • 61C
    • Pointers and Memory
    • Floating Point
    • Compliation, Assembly, Linking, Loading
    • RISC-V, Assembly
Powered by GitBook
On this page

Was this helpful?

  1. 61B

Casting

Casting is a method of forcing the compile-time type of any expression.

//Assuming that there is some bethod
public Weapon maxWeapon(Weapon firstWeapon, Weapon secondWeapon);

//We can cast to a subclass if we know that the resultant weapon will have 
//the type we want.

Polearm maxPole = (Polearm) maxWeapon(new Polearm(13), new Polearm(34));

Note, however, that casting can run into some issues at runtime. For example, the following

Polearm maxPole = (Polearm) maxWeapon(new Claymore(445), new Catalyst(45));

would run into issues at runtime, although it would be fine at compile time.

PreviousComparables and IteratorsNextLists and Sets

Last updated 4 years ago

Was this helpful?