Sunday, April 6, 2025

Polymorphism

 







class Parent {    static void display() {        System.out.println("Parent");
}
}

class Child extends Parent {
static void display() {
System.out.println("Child");
}
}
public class Test {
public static void main(String[] args) {
Parent obj = new Child();
obj.display();
}
}

Answer:

Parent
  • Static methods do not follow polymorphism. They are resolved at compile-time based on the reference type.
  • Here, obj is of type Parent, so Parent.display() is called.


<!DOCTYPE html>
<html>
<head>
    <title>Java Polymorphism with Instance and Class Level Variables</title>
    <style>
        body { font-family: Arial, sans-serif; line-height: 1.6; margin: 20px; }
        h2 { color: #2e6da4; }
        code, pre { background-color: #f4f4f4; padding: 6px; border-radius: 4px; display: block; }
        table { border-collapse: collapse; width: 100%; margin-top: 20px; }
        th, td { border: 1px solid #ccc; padding: 10px; text-align: left; }
        th { background-color: #f2f2f2; }
        .note { color: #d9534f; font-weight: bold; }
    </style>
</head>
<body>

    <h2>🔁 Java Polymorphism Refresher</h2>
    <p>Polymorphism means the ability to take many forms. In Java, polymorphism commonly happens with method overriding — especially when a parent class reference points to a child class object.</p>

    <pre><code>Parent p = new Child();</code></pre>

    <p>This is the base setup for runtime polymorphism.</p>

    <h2>✅ Instance-Level Variables & Polymorphism</h2>
    <p><strong>Key Point:</strong> <span class="note">Variable resolution is <em>not</em> polymorphic</span> — it depends on the reference type, not the actual object.</p>

    <pre><code>class Parent {
    int x = 10;
}

class Child extends Parent {
    int x = 20;
}

Parent p = new Child();
System.out.println(p.x);  // Output: 10</code></pre>

    <p>Even though the object is a <code>Child</code>, Java accesses <code>Parent</code>'s <code>x</code> because the reference type is <code>Parent</code>.</p>

    <h2>✅ Class-Level Variables (Static) & Polymorphism</h2>
    <p><strong>Key Point:</strong> <span class="note">Static variables are tied to the class, not the object.</span></p>

    <pre><code>class Parent {
    static int y = 100;
}

class Child extends Parent {
    static int y = 200;
}

Parent p = new Child();
System.out.println(p.y);     // Output: 100
System.out.println(Child.y); // Output: 200</code></pre>

    <p>Because <code>y</code> is static and <code>p</code> is a <code>Parent</code> reference, Java uses <code>Parent.y</code>.</p>

    <h2>⚠️ What Is Actually Polymorphic in Java?</h2>
    <p>Only <strong>instance methods</strong> that are <em>overridden</em> are polymorphic:</p>

    <pre><code>class Parent {
    void show() {
        System.out.println("Parent show()");
    }
}

class Child extends Parent {
    void show() {
        System.out.println("Child show()");
    }
}

Parent p = new Child();
p.show();  // Output: Child show()</code></pre>

    <p>This is real <strong>runtime polymorphism</strong>.</p>

    <h2>🧠 Final Summary</h2>
    <table>
        <thead>
            <tr>
                <th>Feature</th>
                <th>Polymorphic?</th>
                <th>Resolved At</th>
                <th>Based On</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Instance Variables</td>
                <td>❌ No</td>
                <td>Compile Time</td>
                <td>Reference Type</td>
            </tr>
            <tr>
                <td>Static Variables</td>
                <td>❌ No</td>
                <td>Compile Time</td>
                <td>Reference Type</td>
            </tr>
            <tr>
                <td>Instance Methods</td>
                <td>✅ Yes</td>
                <td>Runtime</td>
                <td>Actual Object Type</td>
            </tr>
            <tr>
                <td>Static Methods</td>
                <td>❌ No</td>
                <td>Compile Time</td>
                <td>Reference Type</td>
            </tr>
        </tbody>
    </table>

</body>
</html>

No comments:

Post a Comment

CompletableFuture

  Welcome back to  our concurrency series ! In our first discussion, we likely touched on the traditional models of threading. Today, we’re ...