Why it is needed to invoke parent constructor?

public class AAA
public AAA(int a) {  }
public class BBB extends AAA
public BBB(int a) { super(a); }
public BBB() {  }
It gives an error in the second constructor of class BBB. Why? Why Java requires AAA to have an AAA() constructor???
Maciek

public class AAA
public AAA(int a) {  }
public class BBB extends AAA
public BBB(int a) { super(a); }
public BBB() {  }
It gives an error in the second constructor of class
BBB. Why? Because you have not explicitely called a parent constructor, the compiler will add an implicit call to the no-args parent constructor thus:
super();But the no-args constructor does not exist in the superclass!
The compiler only creates a default constructor if you do not explicitely declare any constructors, so since you have declared a constructor that takes an argument, and have not declared a constructor that takes no arguments, AAA does not have a no-args constructor.
Why Java requires AAA to have an AAA()
constructor???It doesn't - it only requires it to have at least one constructor. But the code you have in BBB tries to call the non-existent AAA() constructor. You must either explicitely declare the no-args constructor, or explicitely call an existing constructor from every constructor in every subclass.

Similar Messages

  • Why Do We Need Constructor With Arguments?

    I understand that constructor is used to create instances.
    We can have constructors with 0 argument or with one/multiple arguments. Why do we need a contructor with arguments? What is the purpose of doing it?
    Thank you for your help.

    There are three general ways to provide constructors, like you said:
    1) Default constructor (no arguments)
    2) One or more constructors with arguments
    3) One or more constructors with arguments as well as a default constructor
    From a design standpoint, the idea is to give as much information needed for constructing an object up front. Look at the standard Java API for some examples (and because I couldn't find a standard class with just a default constructor, one made up):
    public class Foo {
      private long createTime;
      public Foo() {
        createTime = System.currentTimeMillis();
      public String toString() {
        return "This object was created at " + createTime;
    }This code has no reason to take any arguments. It does all construction on its own and already has all the information it needs. On the other hand, look at BufferedReader. There's no possible way this object can ever be constructed without an argument. What's it buffering? It doesn't make sense to ever instantiate new BufferedReader(). It'd be useless. So BufferedReader has no default (no-arg) constructor.
    Sometimes (very often in Java) you want to do both. Let something be instantiated with no arguments, but also provide the option of creating something with more specific details. A good example is ArrayList, which has three constructors:
    public ArrayList() --> Construct an empty ArrayList with the default initial capacity;
    public ArrayList(Collection c) --> Construct an ArrayList populated with the contents of another collection;
    public ArrayList(int capacity) --> Construct an empty ArrayList with a specific initial capacity.
    Sometimes it makes sense just to use the default. Sometimes you want more control over the behavior. Those are the times to use multiple constructors.
    Hope this helps!

  • Why do we need to take a lock before invoking wait()/notify() on an Objec

    Hi,
    Why do we need to take a lock before invoking wait()/notify() on an Object? i know that we shud take otherwise illegalstateexception is thrown.
    what is the reason to take a lock bfefore invoking the above methods. why does jvm expects from the programmer?
    Cheers,
    duggana.

    Well, very often a wait or notify is conditional on the state of some flag (semaphore) which is protected by the monitor. By putting the wait in a synchronized section you guarantee that the sempaphore won't change between the final test of it and the actual wait or notify, otherwise a notify might be lost and the program hang.
    // wait on semaphor
    if (!canProcede)
       synchronized(this)
           if(!canProcede)
              wait();
    //   release semaphor
    synchronized(this)
         if(!canProceed) {
              canProede = true;
              notify();
        }If the wait above wasn't guarded by the sychrozined it's possible the second code fragment might be executed between the test and the wait, in which case the wait would be called after the notify and, hence, wait for ever.

  • Why do we need to take a lock before invoking wait()/notify() on an Object?

    Hi,
    Why do we need to take a lock before invoking wait()/notify() on an Object? i know that we shud take otherwise illegalstateexception is thrown.
    what is the reason to take a lock bfefore invoking the above methods. why does jvm expects from the programmer?

    Please do not crosspost..

  • Parent constructor calls abstract method

    Hi everybody!
    I'm wondering if there is something wrong with java or if the idea is just too ill?
    Anyway, I think it would be great if this hierachy would work...
    Two classes A and B.
    Class A defines an astract method.
    In A's constructor this abstract method is called.
    Class B extends A and provides an implementation for the abstract method in A.
    Class B also defines a member variable that is set in B's implementation of the abstract method.
    In class' B constructor the parent constructor A() is called.
    example:
    public abstract class A {
      public A() {
        createComponents();
      public abstract void createComponents();
    public class B extends A {
      private String string = null;
      public B() {
        super();
        System.out.println("B::B() " + string);
      public void createComponents() {
        System.out.println("B::createComponents() begin");
        string = new String("test");
        System.out.println("B::createComponents() " + string);
      public void describe() {
        System.out.println("B::describe() " + string);
      public static void main(String[] args) {
        B b = new B();
        b.describe();
    }running the code above produces the following output:
    B::createComponents() begin
    B::createComponents() test
    B::B() null
    B::describe() null
    why is the string member variable null in B's constructor??
    thanks in advance
    Peter Bachl
    Polytechnic University of Upper Austria, Hagenberg
    [email protected]

    The answer is that the call of the super-constructor
    is allways done before the initialization
    of the member variable. That's all and that's the
    normal behavior.
    order :
    - initialization of static variables
    - call to the super-constructor
    - initialization of the instance variables
    - execution of the constructor
    Since this is the advanced forum it is relevant to point out that that is not exactly true.
    There is a step in java that 'initializes' member variables before any constructors are called, super or other wise.
    From the JLS 12.5...
    Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (4.5.5)
    Using the following code as an example
      class MyClass
         int i1;
         int i2 = 1;
    .When creating an instance of the above there will be three 'initializations'
    // Part of object creation...
    i1 = 0; // The default value
    i2 = 0; // The default value
    // Part of construction...after super ctors called.
    i2 = 1; // The variable initializer (see step 4 of JLS 12.5)
    Unfortunately the descriptions are rather similar and so confusion can result as to when the assignment actually occurs.

  • Why do I need a TITLE to create an account?

    Why, why, why do I need a title to create an account with a billing address? Is this really industry-standard encryption? BS on that! I never needed a title to obtain the credit card in the first place. This isn't security, it is mindlessness. I don't need a title in any aspect of my life, nor do I use one. I'm sure this sort of namby pamby bureaucratic inanity of being name police must have Steve spinning in his grave. My apologies, I meant Mr. Jobs. Get with the 21st century Apple, I mean a title, come on! Oh, and the choices are: Mr., Mrs., Ms., and Dr.. I guess Sir Paul McCartney can't use iTunes. If I used some title I'd be signing in as my parents and, God rest their souls, they are unable to provide me their credit card anymore (they never liked my music anyway). Hey Apple, I've got the money to spend baby, what concern is it of yours what I am called. 

    Have you created an iCloud account?  This is not the account or ID you use for iTunes.  And where exactly are you turning on notes?  In what area within settings of your devices?

  • Why do we need MTP in the SIP trunk for CVP warm transfers

    Hi All,
    Why do we need to enable MTP in SIP trunk between CUCM and CVP for CVP based trasnfers???
    Thanks in advance!!
    Regards,
    Thammaya Gupta K.

    I saw also in the CDR logs that the IP Phone media transport going to CUBE is in G711.And as well in the wireshark capture of the IP communicator that the CUCM invoke to use the g711 codec but as per ITSP logs they are now in the g729.
    @ Jamie If I un-tick the MTP point required in SIP trunk will make the call leg from IP Phone to CUBE g729 (w/o hw resource), I have also tried to use g729 preferred originating codec, but still the IP Phone is using g711.
    I have seen a documentation states:
    " To configure G.729 codecs for use with a SIP trunk, you must use a hardware MTP or transcoder that supports the G.729 codec." - I read this on the CUCM help page under configuring SIP trunk setting.
    Our ultimate goal is to use g729 without using HW MTP/ transcoder.
    IP Phone ->CUCM SIP Trunk ->CUBE-> ITSP

  • What is the need for calling default constructor by JVM?

    What is the need for calling default constructor by JVM? why the JVM should intiializes default values to the data
    fields if the constructor is not there in our class?

    mahar wrote:
    What is the need for calling default constructor by JVM? Huh? The JVM does not need to call the default constructor. It needs to call a constructor.
    You decide which one by the way you use "new".
    why the JVM should initialize default values to the data fieldsHuh?
    ... if the constructor is not there in our class?Huh? The default constructor is always there. It may be private but it is still there.

  • How to call the grand parent constructor from the child ??

    Hi All,
    I am having 3 classes.
    Class A.
    Class B extends Class A.
    Class C extends Class A
    i need to call the constructor of ClassA from Class C. how to call ??
    Thanks,
    J.Kathir

    Each class will implicitly call the parent default constructor if you do not call it in the code ie
    super();However you can call any of the parent constructors ie
    class Parent {
        int value;
        Parent(int v) {
            value = v;
        public void display() {
            System.out.println(value);
    class Child extends Parent {
        Child(int v) {
            super(v);
        public static void main(String[] args) {
            Child c = new Child(10);
            c.display();
    }

  • Why does EJB needs two interface???

    Hi All,
    This has been asked by many ppl and many of you might be having
    the correct answer for it. Please tell me....
    Why does EJB needs two interface (Home and Remote Interface)?. Why not one?
    Thanx to all...
    Regards
    GoodieGuy

    Hi Goodie ,
    Its good question and its a very valid one , one has to have doubt
    why two , why not one ..
    Here the answer goes ..
    First of all you need to understand that its a distributed computing technique ie I mean lot of people will be accessing the bean at the same time , right ? OK , you cann't access the bean directly.
    and lastly the stateless and entity beans do instance pooling.
    The very purpose of having two interfaces is
    1) To differnentiate the bussiness logic and life cycle methods .
    2) The home interface inititates life cycle methods like creation , destruction etc .
    3) There are lot of beans in the container , and through home you are
    creating the instance or accessing one of them .
    4) Once you have home object using it we get the remote object ie the instance of the bean that you want to access if stateless any bean can be called , if statefull depending upon the parameter in create method that respective bean is created and invoked .
    In ejb 1.0 and 1.1 even though the bean and the client are in same jvm its assumed that they are remote and in 2.0 Ver we have local this is to avoid network traffic .
    Hope you have got the answer , but if u still need clarification
    read EBRoman book first and second chapters thoroughly and then proceed
    a head.
    Bye
    Mahesh L.
    ============

  • Archiving cds and why do i need id3 tags

    here comes another one of those questions looking at which codec to use to store music.....i also have tag questions.....
    i'm about to (re-)rip my cd collection and looking at some info re codecs and id3 tags
    now.....i believe i understand the benefits of alac (lower file sizes, keeping id3 tags, lossless compression) v wav(no compression)
    however (and for some out there) if we just entertain the fact that wav may have some minute benefit for me depending on my questions and answers given.....and yes i know wav and alac will sound the same to the human ear - i accept that as a given of lossless compression ......
    points i'd like to outline:
    i am looking to rip my cds, firstly and mostly, for an archiving purpose!! purpose numero uno!!
    i could well want to re-create cds with these archived records in the future
    size of files is of no concern to me at all
    i'm using a mac nowadays
    i will convert most of these files, where needed, to use with a portable music player (i'm not bothered with having, say a wav file and then also converting and having an alac file, as crazy as that sounds)
    this is where i ask for info re the benefits of embedded tags that are found in alac:
    are these tags that important?
    where do they come into play in ripping and later converting music?
    are these uses just bells and whistles that have no benefit to my needs?
    if i rip cds to wav (or aiff), say, using itunes, will cd info and track listings be available on the downloading database (eg itunes) to name these wav files?
    if i then ensure these song files are kept under the album's folder, have i just done what tags do? surely not. that is all i have needed in the past.
    this is where i feel i may be really missing what id tags (can) do
    are tags more than just managing song files and where they belong and where they have come from?
    is it just a convenience of not personally managing your song files (as per previous paragraph)?
    (in the past i had ripped cds to mp3 codec. i think i used "cd rip" or something like that on my pc.
    my mp3 files were individually named and sorted and kept under album folders under artist name folders.
    apart form the very odd occasion all track and album info i needed was found on a database that was attached to the ripping software.
    i never had a problem managing/maintaining these files and folders.)
    i welcome some enlightenment on some of my questions above and other info that may be relevant
    and yes, i understand that music will sound the same as a wav file or alac file - but humour me re using wav and tell me why i need id3 tags
    i guess the crux of it is:
    why do i need id3 tags?
    does my managing of my song file in the album folder do what tags do?
    what problems/shortcomings/headaches may i encounter by not having those tags if i use wav as opposed to alac?
    what do i not know about these little buggers?
    what codec is best for my purpose of archiving and re creating of cds (for playing in cd players)
    thanks in advance for your input and any clarity that i may experience through this
    peter t
    excuse my long windedness (i have spent some time editing this entry)

    Crows2012 wrote:
    as mentioned earlier if i just have songs (wav) in album folders, when i import these albums into itunes, do you know if these will come up under AN album with their file names purely using my filename setup (but with no artwork)?
    Yes, iTunes will read the filename, such as "Track 1" or something else and display it. But nothing else. No Album or Artist.
    so the tracks will remain grouped as an album based on their initial folder and the albums and tracks as per file name?
    Crows2012 wrote:
    also, does aiff have limitations (apart from the full size of the file)?
    I'm not sure I understand what you mean. For all practical purposes AIFF and WAV are exactly the same thing. They're just file containers.
    i thought that i had read somewhere that aiff tags could run into some issues with its tags - ie may not always be transferred 100% accurately with certain players (or hardware maybe)
    Crows2012 wrote:
    and how do these codecs work re-creating an album for the purposes of playing in a cd player? - this is a crucial question for the purpose of my archiving/backup of CDs THEMSELVES
    That's actually going to depend on what burning software you use. Once you rip the tracks to whatever file container you choose (WAV/AIFF/ALAC) you'll never really exactly re-create the album. But for archiving purposes all three file containers will do what you want, which is to create a lossless archive. AIFF has the advantage of supporting embedded ID3, which ALAC has the additional advantage of the files also being about half the size of AIFF/WAV files.
    i was thinking that maybe wav was able to recreate an album (in effect duplicate one) if my cd was lost or damaged in the future. i was thinking this IF when ripping to wav everything is unchanged (unless there is other data on the original cd). and maybe only possible with wav. the purpose for this would be to play recreated cds on my cd player
    any idea on this one?
    (i havent looked too thoroughly on this angle but i'll keep googling on this one)
    much appreciate all info thus far
    peter t

  • I want to know that if i want to download facebook on my iphone 4 or any other apps then it ask me put my card details for payment. why is that. if they are free then why do i need to give my card details?please help me anyone.

    I want to know that if i want to download facebook on my iphone 4 or any other apps then it ask me put my card details for payment. why is that. if they are free then why do i need to give my card details?please help me anyone.

    Just select no credit card as outlined here:
    http://support.apple.com/kb/ht2534

  • Why do I need to connect my Ipad mini with my Mac Book Pro via iCloud?

    Why do I need to connect my Ipad mini with my Mac Book Pro via iCloud? I don't have iCloud on my MacBook Pro and only intend to use the mini to check e-mail and use Garage Band and Photo Booth for song writing when traveling . . .

    Thanks Community! I solved the problem. In fact it was not a problem but lack of understanding. These guys at Apple are way ahead in thir thinking. I will try to explain as short as I can.
    In Lion, ihe Prefferences/Display show only two choices: 6-7 steps of resolution and some color tab. No two screens, no two display to overlap, nothing for us to do. "That was the problem"! Everything is automatic.
    When I connected the HDMI cable to the Miniport into a T-bolt slot, the TV screen showed the "spiral galaxy" how Zyriab is calling it. In fact he gave me the best clue. That shows the connection is good. But where is the Mac Book image?
    You have to drag it to the right out of the Mac Book display area, and "voila!" it will continue on the TV screen. The same with the mouse pointer, push it out ofthe Mac Book display area and you see it on the TV sreen. Good image, you can keep the max resolution, etc. The sound is still on the Book's speakers. I have to figure that out.
    Thank everybody, especialy Zyriab, ne was the closest.
    High regards to everybody

  • Keynote and pages are now free, but why do i need to pay for them still when there now free?!?

    keynote and pages are now free, but why do i need to pay for them still when there now free?!?

    Users can obtain the iWorks and iLife applications free, if they purchased a Mac after the beginning of October 2013.
    Older Mac purchases require a paid purchase of these applications.

  • Why do I need to start up Screen Sharing twice to get it to work ?

    I have an Intel Mac Mini connected to my TV, so I started using Screen Sharing from my MacBook Pro to be able to work on it when others were watching TV.
    Both Macs used to have OSX 10.5 Leopard, and everything worked fine.
    Now they both have OSX 10.6 Snow Leopard, and whenever I startup Screen Sharing, the connection-windows asking for my Name & Password comes up properly, but then it hangs... it stays in "Connecting..." for ever (litteraly).
    I'm not able to quit or force-quit the connection-window by then, so it stays in view...
    (Screen Sharing isn't even listed in the force-quit list as a running app)
    but...
    when I just try Screen Sharing again, I get connection immediately and it seems to work flawlessly...
    ...it's just that the connection-window is impossible to quit...
    it stays in view an is really annoying...
    this looks really crappy...
    can anyone tell me why I do need to startup Screen Sharing twice to get it to work ?
    Is this a bug ?
    (If so : please apple fix this a.s.a.p.)
    ...or am I doing something wrong ?
    (if so : what am I doing wrong ? and how can I get rid of this annoying screen ?)
    Thank you for your kind reply.
    Toon.B

    it's October and this really annoying problem still exists !
    the unnamed screensharing-loginwindow keeps "Connecting...", and I'm unable to shut it down...
    ...even after my screen sharing session is over
    ...even force-quitting is impossible (this 'app' just isn't showing up in the force-quit-window...)
    please Apple - fix this ; under 10.5 it was working great !
    Does anyone have any idea how to fix this ?
    (besides restarting my Mac)
    Thanks !
    Toon.B

Maybe you are looking for

  • Upgrading Oracle Patch Set

    Hello All, I am in the process of upgrading the Oracle Patch set from 9.2.0.6 to 9.2.0.8. Can anyone let me know what are the Pre and post installation steps that needs to taken care of. Thanks

  • Video editing 'special effects': can you apply a fast/slow motion effect to video clips?

    I haven't been able to find a way to create the effects of fast or slow motion in the video clips. Is there a way to do this? If not, please consider adding it in the full version - I use these effects alot when editing videos. Thanks!

  • IDVD crashed, and can't restore it form iLife 06 Disc, Help!!

    Dear all: My iDVD 6 crashed after I upgrade my Macintosh HD from 500GB to 750GB HD (I used apple's disc utility to clone all data from 500GB to 750GB HD, and it seems working just fine until today...) I try to launch iDVD but it did not response and

  • Query on treasury module

    Dear Friends, Our client use to buy and sell the shares in the large volume. So he wants to maintain all these transactions and wants control the business through SAP. I donu2019t know in which module I can map these requirements. Now we are using EC

  • JAVA Mapping (DOM)

    Hi Guys, Can i test my JAVA Mapping (DOM). I create all coding as per the requirment . Is it a way that in the NWDS in can give input XML Document and test there in Eclipse before am using it in the XI Mapping. How to pass "InputStream in" XML docume