title: Method overriding in Java- assigning child class object to parent class variable
body: <pre><code>class Person{
      String name;
  
      void show(){
          System.out.println("I am parent");
      }
  }
  
  class  Student extends Person{
      String dit;
  
      void show(){
          System.out.println("I am child");
      }
  }
  
  public class NewClass {
      public static void main(String[] args) {
          Person p;
          p = new Student();
  
          p.name = "Kamal";
          p.dit = "DIT/10/c1/1234"; // I'm not allowed to do this 
          p.show(); // Here in overriding child class method is called
      }
  }
  </code></pre>
  
  <p>I am confused. As I understood 'p' variable cannot access dit (even though it is assigned with Student) because data type of p is Person which is parent class. Therefore parent class cannot access child class attributes and methods. But in overriding it calling child class method.</p>
  
  <p>I am new to java. Please help me to understand this.</p>