Make delicious recipes!

All functions are virtual in Java



Puzzle: Print the output of the following code.


public class ClassHierarchy
{
    static class Base 
    { 
        public Base () 
        {
            System.out.println("Base");
            init();
        }
        
        public void init () {System.out.println("Base:init");};   
    }
    

    static class Derived extends Base 
    { 
        int a=0;
        
        public Derived () 
        {
            System.out.println("Derived");
            init();
        }
        
        public void init () 
        {
            if (a==0)
                System.out.println("Derived:init");
            a=1;
        };
    }
    
    
    public static void main(String[] args) 
    {
        new Derived();
    }
}



Output:

Base
Derived:init
Derived
Derived:init







Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal