Call for New Batch / Any Query / Demo Classes : ☎ 08871209400 , 7987028543 , Zone-2,M.P.Nagar,Bhopal

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

How 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 MoreHadoop - Just the Basics for Big Data Rookies (Best online tutorial)This Tutorials assumes absolutely no knowledge of Apache Hadoop and will provide a complete introduction to all the major aspects of the Hadoop ecosystem of pro ...Read MoreGoogle Python online tutorial-Interested in learning python? Then you must watch this videoPython is a dynamic, interpreted (bytecode-compiled) language. There are no type declarations of variables, parameters, functions, or methods in source code.wat ...Read Morelatest product and platform innovations at Google by Sundar PichaiLearn about the latest product and platform innovations at Google in a Keynote led by Sundar Pichai.Video is also subtitled in Chinese, Indonesian, Italian, Jap ...Read MoreFuture of the Java Platform and the JDKJava has been one of the most popular programming platforms in the world for twenty-three years as One Language, One Platform, a Lifetime of Opportunity. How d ...Read More

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