Chaining Constructor using Super()

If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, there will be a whole chain of constructors called, all the way back to the constructor of Object. This is called constructor chaining, and one needs to be aware of it when there is a long line of class descent.

If one method overrides one of its superclass's methods, the overridden method could be invoked through the use of the keyword super. You can also use super to refer to a hidden field. Take for example, this class, Superclass:



public class Superclass
{

    public void printMethod()
    {
        System.out.println("Printed in Superclass.");
    }
}

   

Here is a subclass, called Subclass, that overrides printMethod():


public class Subclass extends Superclass
{

    // overrides printMethod in Superclass
    public void printMethod()
    {
        super.printMethod();
        System.out.println("Printed in Subclass");
    }
    public static void main(String[] args)
    {
        Subclass s = new Subclass();
        s.printMethod();    
    }
}
 

Within Subclass, the simple name printMethod() refers to the one declared in Subclass, which overrides the one in Superclass. So, to refer to printMethod() inherited from Superclass, Subclass must use a qualified name, using super as shown. Compiling and executing Subclass will produce the following output:


    
    Printed in Superclass.
    Printed in Subclass
    
    

References : Oracle share on :        :  

Love to hear your Views / Guidance / Recommendations on this Post…

Explore the Technology World

Real Time Analytics with Spring Application Development FrameworkSpring is the most popular application development framework for enterprise Java.Millions of developers use Spring to build simple, truly portable, fast and fle ...Read MorePath Setting in Java When an application is launched from command prompt Windows OS searches for the executable program in the current working directory. The prompt throws an error ...Read MorePicture-in-Picture App for Android devices with Source CodePicture-in-Picture is a special type of multi-window mode mostly used for video playback for Android devices.Released under Apache License 2.0 , which means you ...Read Moreopen source free template for personal or company blogsLooking for perfect theme for personal or company blogs.Released under the MIT license, which means you can use this template for any purpose, even for commerci ...Read MoreHow to Build an App for the Google AssistantApps for the Google Assistant are the gateway for your users to engage with your services through Google Home, Android phones, iPhones.Watch here to build an as ...Read MoreIntroduction to AMP - Accelerated Mobile PagesAMP, short for Accelerated Mobile Pages, is new new open source initiative to make the mobile web awesome again.Watch Here the Introduction to AMP ...Read MoreFacebook |Google+ | Twitter | LinkedIn | Youtube About | Cookies-Policy | Disclaimer | Site Map | Contact

Call / Visit for New Batch

Services

Why Join Us

  • ➯ Free Demo Classes
  • ➯ No Registration Fee
  • ➯ Interview Questions
  • ➯ Study Materials
  • ➯ Softwares
  • ➯ Aptitude & Reasoning
  • ➯ Placement Assitance

Address

  • MyJava Training Institute ,
  • Plot No.243 , shop No 14 ,
  • Secnd Floor , Zone-II ,
  • M.P.Nagar , Bhopal,
  • Madhya Pradesh ( 462011 )
  • ☎  08871209400 , 7987028543
  • 🌐 www.myjava.in
FacebookGoogle +TwitterYouTube

MyJava - java Training Institute