Why to Call super()

Suppose I have a class named Class1. Another class extends Class2 which extends Class1.
class Class1{
Class1(){
class Class2 extends Class1{
Class21(){
super()//Why this.. Any way, super constructor is being called no??
We sometimes call super() in the constructor of the sub class to invoke the Super Class constructor. Now, my question is, Whenever we make a new instance of a class, it is any way going to call the super class constructor as per the basic Java design. Then Why, we sometimes call super() specifically.. ???
Thanx in Advance..
Naren

When java calls the super class constructor, it calls the no argument
constructor.One reason to call the constructor is if the class was
designed without a no argument constructor. You would get an error
if you extended it and didn't call its constructor.You may also want to
call a different constructor instead of the default one.

Similar Messages

  • Why Do We Have To Call super.init(config); in init() method of servlet?

    Hi, everyone..
    I wonder why we call super.init(config) in init method of servlet... If i dont call it ; when i try to get servletcontext in service method it throws java.lang.NullPointerException...when we call super.init() , what is happening behind the scene? If anybody has a technical explanation for my question , i will be very pleased...
    THX FOR YOUR FUTURE REPLIES IN ADVANCE....

    I am sorry about the uppercases and i dont want to seem smart on java forums... Anyway, m8 this is the thing that i know... i meant; for instance when we override doGet or doPost method ; we dont need to override init method; but the server loads the servlet and we can get the context of the servlet in these methods easily by calling getServletContext() method; however when we want to call service method implicitly by jndi, servlet needs to be loaded and init method must call its parent...(i also write down in web.xml <load-on-startup>.... for that servlet).
    thx for your replies in advance....

  • Calling super.myOverriddenMethod() using reflection

    I have a method in a super class that doesn't do all I want it to do; so I need to do something like this:
    protected void myOverriddenMethod(Object param)
        super.myOverriddenMethod( param );
        doExtraNeededBit();
    }But I need to do this in a plugin class that works with 2 code lines
    The earlier superclass does not have this method, and so I can't call super.myOverriddenMethod( param );
    I have tried using reflection like this
    protected void myOverriddenMethod(Object param)
        //super.myOverriddenMethod( param );
        try{
           Class mySuperclass = Class.forName( "myPackage.MySuperClass" );
           Method overriddenMethod = mySuperclass.getDeclaredMethod("myOverriddenMethod",new Class[]{Object.class});
           overriddenMethod.invoke(this, new Object[]{ param ) });
      }catch(Exception e){e.printStackTrace();}
        doExtraNeededBit();
    }but this does not work, I get a StackOverflowException as the super method isn't being called, this method is calling itself...
    How do I get it to call the super method instead, eg something like
           overriddenMethod.invoke(super, new Object[]{ param ) });Which doesn't compile....

    So why can't you use super.myOverriddenMethod(Object
    param) ?
    What I understood from that is you are trying to call
    a method in the superclass, but the compiler won't
    let you because, as you say, the method doesn't
    exist. I hope that isn't right...thats right!
    in one code line the method exists - the earlier one doesn't have it
    You can normally use reflection to see if a superclass method exists, and call it if so (I only want to call it if it exists). But I don't know how to call a superclass method that you are overriding using reflection

  • Why we call adaptive RFC in webdynpro java..

    Hi,
    I want to know why we call adaptive RFC connection in webdynpro java whenever we import new rfc in our  java web dynpro project.  why name  ADAPTIVE RFC  is used
    if we  make any structure changes in the model which we imported in web dynpro project , like adding a new field, or changing the length/datatype of the existing field, then we always have to reimport the model in our webdynpro project so why adaptive name used. wats especial in the word ADAPTIVE RFC

    Hi Rahul,
    please read the guide:
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/10465350-b4f5-2910-61ba-a58282b3b6df?quicklink=index&overridelayout=true
    Best Regards,
    Ervin

  • Error in calling Super() method cannot reference ...

    import javax.swing.JFrame;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    public class MyFrame extends JFrame {
    String St="Rayudu";
         JPanel jp1= new JPanel();
         JButton jb1=new JButton("Hello");     
    public MyFrame() {
    super("Document :" + St);
              jp1.add(jb1);
              getContentPane().add(jp1);
    //...Then set the window size or call pack...
    setSize(300,300);
    //Set the window's location.
         setLocation(300,300);
         setVisible(true);
    public static void main(String ar[])
    MyFrame mf=new MyFrame();
    mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    C:\java>javac My*.java
    MyFrame.java:11: cannot reference St before supertype constructor has been called
    super("Document :" + St);
    ^
    1 error
    Can anyone explain?
    thanks in advance.

    Change your constructor to take an argument of type String, pass this along to the super method when you call the new instance of your class in main. ie:
    public myFrame( String title ) {
    super( "Document: " + title );
    st = title; //if you need it in that String Object assign it here.
    //etc...
    public static void main( String args[] ) {
    new myFrame( "Testing" );
    Does this answer your question?

  • Why We Call Adpative RFC MODEL?

    Hi ,
    Why We Call Adaptive RFC MODEL?
    What is RFM?

    Hi Subbarao,
    The Adaptive Remote Function Call (Adaptive RFC) is a technology that enables the Web Dynpro application developer to use the business functions encapsulated in Business APIs (BAPIs) even after a structure modification, without having to provide the new data using a second back end or a new structure with subsequent regeneration of the proxies.
    This new RFC model shows adaptive behavior. This means that the RFC proxies do not use the original design time structure definition at runtime. Instead, this Web Dynpro model automatically adapts the data of the structure passed in the Java proxy generation to the current data – that is, a structure definition in the back-end system that may have been modified
    For more info go through this link..
    [Adaptive RFC|http://help.sap.com/saphelp_nw70/helpdata/en/6a/11f1f29526944e8580c5e59333d96d/frameset.htm]
    Importing RFC's
    __http://help.sap.com/saphelp_nw70/helpdata/en/6a/11f1f29526944e8580c5e59333d96d/frameset.htm__
    RFM - stands for Remote-enabled Function Modules, amd you can learn more about it from
    http://help.sap.com/saphelpnw70/helpdata/en/22/0427c0488911d189490000e829fbbd/frameset.htm_
    Difference between BAPI and RFM --
    BAPI are RFC enabled function modules. the difference between RFc and BAPI are business objects.
    You create business objects and those are then registered in your BOR (Business Object Repository)
    which can be accessed outside the SAP system by using some other applications (Non-SAP) such as VB or JAVA.
    In this case u only specify the business object and its method from external system
    in BAPI there is no direct system call. while RFC are direct system call.
    Hope this helps,
    Reward Points if helpful,
    Regards,
    Shailesh Nagar

  • I am updating my iphone 4s to ios 5.1 now. Why it is super slow??? It will take upto 9 hours to finish!!!!

    I am updating my iphone 4s to ios 5.1 now. Why it is super slow??? It will take upto 9 hours to finish!!!!
    What should I do?? waiting or stop updating?

    "I am updating my iphone 4s to ios 5.1 now. Why it is super slow???"
    Because, Millions of other people are attempting to do the same thing.
    Stedman

  • Why are calls from my favorites not ringing when my phone is silenced at night?

    Why are calls from my favorites not ringing when my phone is silenced at night?

    You Might Have scheduled do not diturb... Turn Of Scheduled button in settings>do not disturb>scheduled..

  • Call super- super- method( )?

    I have a class CL_LION that inherits from CL_CAT which inherits from CL_ANIMAL.
    In CL_ANIMAL, I have a method shout( ), which is overwritten in each of the children.
    Now, I want to call super->super->shout() from CL_LION (which means I want to skip the shout() of CL_CAT and directly call shout() of CL_ANIMAL.
    The syntax "super->super->shout() does not work.
    Is there another way to do this?
    Best regards,
    Daniel

    Ok, so you want to go directly back to the animal class.  How about we use an INTERFACE.
    report zrich_0001.
    *       INTERFACE lif_animal
    <b>interface lif_animal .
      methods shout.
    endinterface.</b>
    *       CLASS lcl_animal  DEFINTION
    class lcl_animal definition.
      public section.
    <b>    interfaces lif_animal.</b>
        methods: shout.
    endclass.
    *       CLASS lcl_animal IMPLEMENTATION
    class lcl_animal implementation.
      method shout.
        write:/ 'This is an animal shouting'.
      endmethod.
    <b>  method lif_animal~shout.
        write:/ 'This is an animal shouting'.
      endmethod.</b>
    endclass.
    *       CLASS lcl_cat DEFINITION
    class lcl_cat definition inheriting from lcl_animal.
      public section.
        methods shout  redefinition.
    endclass.
    *       CLASS lcl_cat IMPLEMENTATION
    class lcl_cat implementation.
      method shout.
        call method super->shout( ).
      endmethod.
    endclass.
    *       CLASS lcl_lion DEFINITION
    class lcl_lion definition inheriting from lcl_cat.
      public section.
        methods shout  redefinition.
    endclass.
    *       CLASS lcl_lion IMPLEMENTATION
    class lcl_lion implementation.
      method shout.
        call method super->shout( ).
      endmethod.
    endclass.
    data: a_lion type ref to lcl_lion.
    data: a_animal type ref to lcl_animal.
    <b>data: if_animal type ref to lif_animal.</b>
    start-of-selection.
      create object a_lion.
    <b>  if_animal ?= a_lion.
      call method if_animal->shout( ).</b>
    Regards,
    Rich Heilman

  • Why not call it iStore?

    Why not call iTunes, iStore, because you can get apps for your iPhone you can download videos or even films now Apple TV has been released?

    You can enter that suggestion at the iTunes Feedback Page.

  • I wanted to know why they call me when I do not show the number but I get the words private number, and even when I call out to another private number.

    I wanted to know why they call me when I do not show the number but I get the words private number, and even when I call out to another private number.I tried it in the settings to activate id shows but nothing has changed. I hope you can help me.

    Cloud programs do not use serial numbers... you log in to your paid Cloud account to download & install & activate... you MAY need to log out of the Cloud and restart your computer and log back in to the Cloud for things to work
    Some general information for a Cloud subscription
    Log out of your Cloud account... Restart your computer... Log in to your paid Cloud account
    -Sign in help http://helpx.adobe.com/x-productkb/policy-pricing/account-password-sign-faq.html
    -http://helpx.adobe.com/creative-cloud/kb/sign-in-out-creative-cloud-desktop-app.html
    -http://helpx.adobe.com/x-productkb/policy-pricing/activation-network-issues.html
    -http://helpx.adobe.com/creative-suite/kb/trial--1-launch.html
    Does your Cloud subscription properly show on your account page?
    If you have more than one email, are you sure you are using the correct Adobe ID?
    https://www.adobe.com/account.html for subscriptions on your Adobe page

  • Why we call badi is multilevel implementation.

    hi friends,
    why we call badi is multi level implementation(i.e.,sap,customer,partner,industry version and country version).
    but comes to user exit we call two level implementation.(i.e.,sap and customer).
    what exactly the two level and multi level implementation.
    can any one tell this.

    Hi,
    See if it helps:
    Business Add-Ins are a new SAP enhancement technique based on ABAP Objects. They can be inserted into the SAP System to accommodate user requirements too specific to be included in the standard delivery. Since specific industries often require special functions, SAP allows you to predefine these points in your software.
    As with customer exits two different views are available:
    In the definition view, an application programmer predefines exit points in a source that allow specific industry sectors, partners, and customers to attach additional software to standard SAP source code without having to modify the original object.
    In the implementation view, the users of Business Add-Ins can customize the logic they need or use a standard logic if one is available.
    In contrast to customer exits, Business Add-Ins no longer assume a two-level infrastructure (SAP and customer solutions), but instead allow for a multi-level system landscape (SAP, partner, and customer solutions, as well as country versions, industry solutions, and the like). Definitions and implementations of Business Add-Ins can be created at each level within such a system infrastructure.
    SAP guarantees the upward compatibility of all Business Add-In interfaces. Release upgrades do not affect enhancement calls from within the standard software nor do they affect the validity of call interfaces. You do not have to register Business Add-Ins in SSCR.
    The Business Add-In enhancement technique differentiates between enhancements that can only be implemented once and enhancements that can be used actively by any number of customers at the same time. In addition, Business Add-Ins can be defined according to filter values. This allows you to control add-in implementation and make it dependent on specific criteria (on a specific Country value, for example).
    All ABAP sources, screens, GUIs, and table interfaces created using this enhancement technique are defined in a manner that allows customers to include their own enhancements in the standard. A single Business Add-In contains all of the interfaces necessary to implement a specific task.

  • Why does call of duty world at war crash on ios6?

    Why does call of duty world at war crash on ios6?

    You can try here. However, you umay have to wait for an update to the app.
    iOS: Troubleshooting applications purchased from the App Store

  • Calling super in subclasses

    Is there a pattern to accomplish the following?
    I have a setup() method that must be called:
    class A {
      public void setup() {
        // necessary code for A
    }I want to ensure its code gets executed even if a subclass overides it. The usual way is with a "hook":
    class A {
      public final void setup() {
        // necessary code for A
        onSetup();
      public void onSetup() {}
    }This way, a subclass can only override onSetup(), ensuring that the code in setup() gets called. However, say I want to play the same trick with subclasses of A:
    class B extends A {
      public final void onSetup() {
        // necessary code for B
        onOnSetup();
      public void onOnSetup() {}
    class C extends B {
      public final void onOnSetup() {
        // necessary code for C
        onOnOnSetup();
      public void onOnOnSetup() {}
    }Is there a pattern I can use so that each class only has to override a method called setup(), and it is guaranteed that the code in every overidden setup() method will be called? Calling super() from within each setup() is fragile, because a subclass might forget it. Using a hook each time (as in the above example) is annoying, because each time, a different name must be assigned to the non-final method at every level in the hierarchy.

    I have dealt with this. In keeping with the principle of not calling overridable methods in constructors, one may require such a method. For instance, I have objects that are ultimately part of a database, but when created they do not have the db object yet. So later they must be setup with the appropriate db object.
    I think it is understood that overriding a method involves respecting the class you are altering. You should not override a method if you do not understand what it does. That being said, the solution here is to simply put in the comment of the method that it must always be called by any subclasses. There are lots of things which can not be forced. If one overrides this method but does not call super, they are violating the contract of the class.
    You can add a private boolean isSetup, and assert if any methods are called before isSetup is true, if you wish. Personally I would throw an IdiotException :-)
    If you have time to burn and want to get buck wild, you can also use the strategy pattern.
    class AClass {
      private List setupers = new ArrayList();
      AClass(){
         Setuper su = new Setuper() {
              public void setup() {
                   //do setup for AClass
          setupers.add(su);
      public final void setup(){
          for(Iterator it = setupers.iterator();it.hasNext();){
             ((Setuper)it.next()).setup();
           setupers = null;
    class BClass extends AClass {
      BClass(){
         Setuper su = new Setuper() {
              public void setup() {
                   //do setup for BClass
          setupers.add(su);
    interface Setuper {
        public void setup();
    }But once you start down this road, you will eventually come face to face with the fact that some things are understood and a certain amount of intelligence, no, respect is required to extend a class properly. There will always be a way to force the issue, but it will involve increasing amonts of work with little pay off. You cant save people from themselves.

  • Initialize fields prior to calling super() possible?

    Problem: I referece an object in a subclass method which overwrites a superclass method. The object is initialized in the constructor of the subclass. However, I need to initialize the superclass first by calling super(). The superconstructor calls the overwritten method leading to an NPE in the overwritten method because the object has not been initialized yet. Are there patterns that avoid testing the object to be not null every time the overwritten subclass method is called?
    Thanks
    fatzopilot

    paul.miner wrote:
    jtahlborn wrote:
    this is a very bad design. a class should never call a method in its constructor which can be overridden in a subclass. the behavior in this situation is "undefined" according to the JLS, and should be avoided at all costs.I agree this is bad design, but I don't think the behavior is undefined. I think the behavior is that the subclass method will see that subclass' fields in their uninitialized state, which do have defined values (int's are 0, booleans are false, references are null, etc). Are you sure this is in the JLS?you're right, i went back and looked at the JLS (12.5 in the third edition for those following along at home). method dispatch is specified to work consistently during constructor invocations (for some reason i remember it being undefined). regardless (we both agree here), it is a terrible design, because the subclasses methods are being executed before that class is initialized.

Maybe you are looking for

  • I installed Yosemite and my mail is always crashing

    I get this as error Process:               Mail [2960] Path:                  /Applications/Mail.app/Contents/MacOS/Mail Identifier:            com.apple.mail Version:               8.0 (1990.1) Build Info:            Mail-1990001000000000~3 Code Typ

  • CLMLSvc.exe - No Disk error

    I have a new HP Pavilion p6210f (1 week old) and have an error that I cannot cancel or get rid of! "CLMLSvc.exe - No Disk, There is no disk in the drive.  Please insert a disk into driv\Device\Harddisk5\DR6" How can I get rid of this now and how to p

  • Adobe Media Encoder CS5 + .MTS Files

    Greetings! In the past I have used Adobe Media Encoder CS4 to convert .mts files from my Canon Vixia HF200.  These were taken off an external hard drive, not in the original file structure.  I converted them to Quicktime H.264 and everything worked g

  • Has anybody solved the slow wireless connections speeds of the new Imacs

    I have just purchased the new imac...and was made aware of the disastrous connections speeds using wireless.. Does anybody know if apple have come up with a solution...or are they just not dealing with the problem?

  • I want all system status for all process orders in COR3

    Hi All, I have small requirement below: In COR3 tcode after we enter Process order as input, in that screen we can see in 3rd row, System Status and User Status, Where all these system status and User Status are stored, How we will get these msgs? Pl