Skip to content
Learn Netverks

Lesson

Step 16/36 44% through track

polymorphism

Polymorphism

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Polymorphism: the syntax, APIs, and habits you need before advancing in Java.

Interfaces and polymorphism are central to Spring, testing with mocks, and enterprise design patterns.

You will apply Polymorphism in contexts like: Spring Boot APIs, banking systems, Android (with Kotlin), and batch/data pipelines on the JVM.

Write Java with a public class (lessons use Main), click Run on server—the dev runner runs javac then java; fix compile errors from stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Polymorphism lets a superclass reference point to subclass objects and invoke overridden methods at runtime—Animal a = new Dog(); a.speak(); prints "Woof".

Upcasting and dynamic dispatch

The JVM resolves the actual method implementation based on the object's runtime type, not the reference type (for instance methods).

instanceof and casting

if (pet instanceof Dog d) {
    d.fetch();
}

Pattern matching for instanceof (Java 16+) avoids manual cast boilerplate.

Important interview questions and answers

  1. Q: Compile-time vs runtime type?
    A: Reference type is compile-time; object on heap is runtime—dynamic dispatch uses runtime type.
  2. Q: Can static methods be overridden?
    A: No—they are hidden, not overridden; polymorphism applies to instance methods.

Self-check

  1. Why store a Dog in an Animal variable?
  2. When do you need an explicit cast?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Runtime type example?
  • Override @Override why?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump