Skip to content
Loading
  • Vulpes
    0
    I'd just like to add that Child's parameterless constructor wll still call Parent's parameterless constructor even if you don't chain : base() to its definition.

    Murali has just included it for clarity.
    0
  • Vulpes
    0
    Oops, just noticed that the question is about Java rather than C# !

    In that case, Murali's example will become:

    import java.io.*;

    public class Parent     
    {         
       public Parent()         
       {            
          System.out.println("Parent Constructor called");         
       }         

       public Parent (String message)         
       {             
          System.out.println(message);         
       }         

       public static void main(String[] args) 
       {        
           Child c = new Child();                 
           Child c2 = new Child("Hello from Parent");
       }     
    }

    class Child extends Parent     
    {         
       public Child()         
       {
          super(); // optional           
          System.out.println("Child Constructor called");         
       }         

       public Child(String message)         
       {
          super(message);             
          System.out.println("Child Constructor called");         
       }     
    }
    0