• Constructor chaining and Method overriding

    Constructor chaining

    When a class hierarchy is created constructors complete their execution in order of derivation form super class to sub class .The first task of a subclass constructor is to call super class’s constructor to ensure that the creation of subclass object starts with initialization of the data members of the super class.
    That’s why super() must be the first statement executed in a sub class constructor. If super is not used then the default constructor of each super class will be executed .There could be any number of classes in the inheritance chain because of constructor chaining every constructor calls up the chain till class at the top of reached .
    Example:
    class A
    {
    A()
    {
    System.out.println(“Inside class A”);
    }
    }
    class B extends A
    {
    B()
    {
    System.out.println(“Inside class B”);
    }
    }
    class C extends B
    {
    C()
    {
    System.out.println(“Inside class C”) ;
    }
    }
    class ConstChain
    {
    public static void main(String []s)
    {
    C x = new C();
    }
    }

    OUTPUT:
    Inside class A
    Inside class B
    Inside class C

    Method over-riding 

    In a class hierarchy when a method in a sub class has the name and type signature as a method in it super class , then the method in the super class is said to override the method in the super class when an over ridden method is called from within its subclass it will always refer to the version of that method define by the sub class. The version of the method define by super class will be hidden.

    DYNAMIC METHOD DISPATCH: A super class reference variable can refer to a sub class object . Java usage this fact to resolve calls to overridden method. Java determines which version of the method to execute based upon the type of the object referred to at the time of the call occurs .

    CODE:
    class A
    {
    void callme()
    {
    System.out.println("Inside A's callme method");
    }
    }
    class B extends A
    {
    // override callme()
    void callme()
    {
    System.out.println("Inside B's callme method");
    }
    }
    class C extends A
    {
    // override callme()
    void callme()
    {
    System.out.println("Inside C's callme method");
    }
    }
    class Dispatch
    {
    public static void main(String args[])
    {
    A a = new A(); // object of type A
    B b = new B(); // object of type B
    C c = new C(); // object of type C

    A r; // obtain a reference of type A
    r = a; // r refers to an A object
    r.callme(); // calls A's version of callme
    r = b; // r refers to a B object
    r.callme(); // calls B's version of callme
    r = c; // r refers to a C object
    r.callme(); // calls C's version of callme
    }
    }

    The output from the program is shown here:
    Inside A’s callme method
    Inside B’s callme method
    Inside C’s callme method

    INTERFACE OF OVERRIDING :

     Overridden methods allows java to support run time polymorphism , which is essential to object-oriented programming. It allows a general class to specify methods that will be common to all its derivatives while allowing subclass to define the specific implementation of some or all of those methods. Hence the runtime polymorphism of object oriented design that allows code re-use and robustness.
  • You might also like

    No comments:

    Post a Comment

search topics

NEWSLETTER

Get All The Latest Updates Delivered Straight Into Your Inbox For Free!