why java allows it to compile?

Pls check the below code. It compiles, but it doesn't run.

I can understand that it cannot run.

But why java allows it to compile? It is obvious that this cannot run.

import java.util.*;

public class TrialClass {

public static void main (String args[])
{
    ParentClass parentObject = new ParentClass();
    ChildClass childObject = new ChildClass();
    
    childObject = (ChildClass)parentObject;
    
    childObject.inheritMethod();
}


}

class ParentClass
{
    protected void inheritMethod()
    {
        System.out.println("from Parent class");
    }
}

class ChildClass extends ParentClass
{
    protected void inheritMethod()
    {
        System.out.println("from Child class");
    }
}


from Recent Questions - Stack Overflow https://ift.tt/2HDKIRy
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)