Why does the child class need to implement the parent classes constructor?/

As I was playing around with some code I came across this point :
First Class
public class A {
     public int x;
     A(int i){
          x=i;
          System.out.println("A is initialised");
}Second Class extending it :
public class B extends A{
     private int y;
   // Why do I need this constructor to call parents constructor?
  // My guess is so that when i make an object of class B referring to class A it should make sense?
     B(int i) {                     
          super(i); 
          y=test;
public static void main(String args[]){
      A a = new A(1);
      A b = new B(1); make an object of class B referring to class A it should work!!
      B c = new B(2);
      B d =(B) new A(2);  --> gives class cast exception!
}I am little confused here, Can someone throw more light on it.
Thanks

You don't override constructors. However, every class, in it's constructor, must call some constructor from the class it's extending. In most cases this is simply super(). However, if your class does not have a default constructor (i.e. you've declared any other constructor, or the sub class does not have access to it, I.E. you've declared it private) then you must include a call to some other constructor in the super class. The constructor in the subclass does not have to be the same as the super class one, but you do have to invoke a constructor from the super class. I.E.
class A {
  A(int i) {}
class B extends A {
  B(String b) {
    super(0);
}

Similar Messages

  • Why does it say itunes can not contact the ipod update server because you are not connected to the internet when i am connected to the internet?

    why does it say itunes can not contact the ipod update server because you are not connected to the internet when i am connected to the internet? i need to update my ipod i even changed the LAN settings and it stilll doesnt work

    Have you seen this article?
    iTunes for Windows: iTunes cannot contact the iPhone, iPad, or iPod software update server
    B-rock

  • Why does it sometimes take 30 seconds for the dock to come up after bringing the curser to the bottom of the page?

    Why does it sometimes take 30 seconds for the dock to come up after bringing the curser to the bottom of the page?

    Try doing a Dock restart using the following Terminal command:
    killall Dock
    Log out/in and test. If that doesn't work.
    You need to look in your user Library/Applications Support/Dock for the .db. Use the Finder “Go To Folder” command. Enter ~/ Library/Applications Support/Dock.  Move the .db to your desktop.
    Then try a dock reset.
    Applications/Utilities/Terminal enter the command
    killall Dock
    Log out/in test. If it works okay, delete the .db from the desktop.
    If the Dock is the same, return the .db to where you got them from, overwriting the newer ones.
    If you prefer to make your user library permanently visible, use the Terminal command found below.
    Show User Library
    You might want to bookmark the command. I had to use it again after I installed 10.8.4. I have also been informed that if you drag the user library to Finder it will remain visible.

  • HT204053 Why does Apple kick their old subscribers under the bus? I have an iMac G5 2.1 Computer using Leopard 10.5.8 system, and I am no longer able to renew my MobileMe account, Why? Maybe I should look into PC Computers in the future!

    Why does Apple kick their old subscribers under the bus? I have an iMac G5 1.9 and 2.1 computer using Leopard 10.5.8 system, and I cannot renew my MobileMe account, why? Is Apple incapable of keeping their old subscriber accounts active and still update their system? Maybe I should look into buying a PC in the future? I eagerly await your reply. Thanks

    MobileMe is being terminated in two weeks. If you have files on your iDisk which are not also on your computer you need to be downloading them as soon as possible or you will lose them.
    You can migrate to iCloud and set Mail up manually to receive your @me.com mail:
    Entering iCloud email settings manually in Snow Leopard or Leopard
    You will not be able to sync calendars or contacts. If you want to have general online file storage you will need to find a third-party alternative - this page examines some options:
    http://rfwilmut.net/migrate3

  • Why does my photo's not email on the classic or journal view?  It appears blank.

    Why does my photo's not email on the classic or journal view?  It appears blank and no photos are emailed.  I don't want to always use the themes

    Make a temporary, duplicate copy of the library and try the two fixes below in order as needed:
    Fix #1
    1 - delete  the iPhoto preference file, com.apple.iPhoto.plist, that resides in your Home/Library/Preferences folder. 
    2 - delete iPhoto's cache file, Cache.db, that is located in your Home/Library/Caches/com.apple.iPhoto folder. 
    3 - reboot, launch iPhoto and try again.
    NOTE: If you're moved your library from its default location in your Home/Pictures folder you will have to point iPhoto to its new location when you next open iPhoto by holding the the Option key.  You'll also have to reset the iPhoto's various preferences.
    Fix #2
    Launch iPhoto with the Command+Option keys depressed and follow the instructions to rebuild the library.
    Select the options identified in the screenshot and the option to rebuild the small thumbnails.
    Click to view full size
    Now those two templates again. 
    OT

  • Why does Itunes sync my purchase transfers to the wrong hard drive?

    I'm running windows 7 with a 64gb SSD boot drive and 1tb data drive. My boot drive is almost completely full. I installed itunes on my data drive and set the preferences to sync media to the data drive as well. I plug in my old ipod to tranfer purchases before an update. The tranfer never finished because it synced all of the data to my boot drive and now my boot drive is completely full and the data is nowhere to be found. I recently had the same problem with it syncing an iphone backup to the same drive, but I found a work around for that. Why does Itunes instist on loading stuff on the boot drive? How can I redirect it? This is simple preferences that should be available to change, but simply aren't.

    Your phone connected, iTunes running, under the Music tap, make sure there are no checks in the boxes labeled "Entire Music Library", & "Automatically fill free space with songs":

  • Why does my ipod no longer play all the items in a playlist?

    Why does my ipod no longer play all the items in a playlist?  I have to go to each item individually to select it as it no longer goes from one item to the next.

    what kind of connection do you have? do you use the ipod 30 pin cable that connects directly to the head unit? it so, maybe you could get help on the alpine site, or a similar site?it could just be a bad connection. you may just have to go oldschool and use rca cables and plug those in...

  • Why does iPhoto (9.0/11) not retain the Event name when exporting more than one event? (using File - Export - Album name with number).

    Why does iPhoto (9.0/11) not retain the Event name when exporting more than one event? (using File -> Export -> Album name with number).
    Exporting a single Event retains the Event name which is what I'd expect. But highlighting more than one event and exporting it renames the images to Events 001.JPG, Event 002.JPG etc.
    I was recently on holidays and had all my events nicely split on Dad's computer but when I went to export it I couldn't retain any of this information. Now I have to replicate this all again on my computer.
    It wasn't possible to export the entire library as the external drive was fat32 format an I didn't want all of it. It would be nice to export a bunch of events to someone and have it retain the name.
    Does anyone have a work around or will this be fixed at some point by Apple?

    Why does iPhoto (9.0/11) not retain the Event name when exporting more than one event? (using File -> Export -> Album name with number).
    Exporting a single Event retains the Event name which is what I'd expect. But highlighting more than one event and exporting it renames the images to Events 001.JPG, Event 002.JPG etc.
    I was recently on holidays and had all my events nicely split on Dad's computer but when I went to export it I couldn't retain any of this information. Now I have to replicate this all again on my computer.
    It wasn't possible to export the entire library as the external drive was fat32 format an I didn't want all of it. It would be nice to export a bunch of events to someone and have it retain the name.
    Does anyone have a work around or will this be fixed at some point by Apple?

  • Why does Internet Explorer display an outline around the hotspot I just clicked on?

    hi all
    Why does Internet Explorer display an outline around the hotspot I just clicked on? what do i have to do to sort this?
    can anyone help please
    gman

    hi hans-g
    thank you for your help and links, was banging my head off the wall the last day . will use forum from now on, really appreciate your help thanks again

  • Why does my exported PDF not look like the original?

    When I export a PDF to excel, the excel document doesn't look like the original. Any idea why or how to make it an exact duplicate?

    No discussion mi adob france
    Acrobat Pro, PDF Pack, Export PDF & More | A...
    Acrobat
    Adobe Document Solutions make it simple to help you get more done every day, and lets you easily create, edit, send, an...
    Envoyé depuis Molto pour iPad
    Message transféré:
    De: Allamelle
    Envoyé: jeudi, février 12, 2015 10:53 AM
    À: [email protected]
    Objet: Re:  Why does my exported PDF not look like the original?
    Stop Mdr
    Envoyé depuis Molto pour iPad
    De: Test Screen Name
    Envoyé: jeudi, février 12, 2015 10:31 AM
    À: René Allamelle
    Objet:  Why does my exported PDF not look like the original?
    Why does my exported PDF not look like the original?
    created by Test Screen Name in Adobe Acrobat.com Services - View the full discussion
    "Vaguely like" is the best we can hope for and a minor miracle in itself. PDF was never meant to be converted.
    If the reply above answers your question, please take a moment to mark this answer as correct by visiting: https://forums.adobe.com/message/7187391#7187391 and clicking ‘Correct’ below the answer
    Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page:
    Please note that the Adobe Forums do not accept email attachments. If you want to embed an image in your message please visit the thread in the forum and click the camera icon: https://forums.adobe.com/message/7187391#7187391
    To unsubscribe from this thread, please visit the message page at , click "Following" at the top right, & "Stop Following"
    Start a new discussion in Adobe Acrobat.com Services by email or at Adobe Community
    For more information about maintaining your forum email notifications please go to https://forums.adobe.com/thread/1516624.

  • HT201210 why does it say im not connected to the internet when i plug my i phone in but i am connected to the internet. what do i do?

    why does it say im not connected to the internet when i plug my i phone in but i am connected to the internet. what do i do?

    no into my labtop i have internet but for some reason when i plug my iphone into it it saids update servers could not connect to the internet please check to make sure u have internet and i do.

  • Why does my music not show up in the iCloud?

    Why does my music not show up in the icloud?

    A slight confusion on my part. I have just discovered that it is all saved on itunes. Many thanks.

  • HT201342 Why does this concern me and what are the advantages?

    Why does this concern me and what are the advantags to @Icloud.com?

    It is not an address change but an addition to your current @mac.com, @me.com email addresses.  Use whichever one you prefer.

  • Why does it take 10 days to replace the iPhone or iPad in the Czech Republic? I am considering a change to Samsung!

    Why does it take 10 days to replace the iPhone or iPad in the Czech Republic? I am considering a change to Samsung!

    It will probably take a week and three days to replace your Samsung in the Czech Republic. In the U.S., if I sent my iPhone or iPad in for warranty work, it would take 7-10 days as well. Yes, I do have the option of taking it to an Apple Store, which you may not be able to do. Samsung doesn't have stores at all in the U.S. I would be dependet on my carrier for warranty replacements.

  • Why does my Premiere Elements 9 install bypass the serial number phase and the kick out disk?

    Why does my Premiere Elements 9 install bypass the serial number phase and the kick out disk?

    Windows or Mac, and are you 100% sure you are using the appropriate Windows or Mac disc?

Maybe you are looking for

  • Alto consumo de CPU

    Esta não é exatamente uma pergunta, mas um relato para talvez servir de pista para o problema em si. Fiz o upgrade do windows 7 para o windows 8 com a opção de manter os programas e dados. Após o upgrade o Firefox passou a apresentar um alto consumo

  • Yet another No Service iPhone 4, second hand

    Hi all, Apologies for yet another topic about the No Service issue on the iPhone 4. I bought a secondhand iPhone 4 16GB 2 days ago after having a 3GS 32GB for 18 months with no problems. iPhone 4 was just over a week old when I bought it (warranty da

  • Trackpad acting weird after spill

    Hi, I am in kind of a problem right now. My trackpad is acting weird after some juice spilled over it from a drinking straw. Everything else works fine. I have turned the power off just to be safe. I need advice as to what to do. I bought my mac (lat

  • Acrobat 10 unable to install patches to base

    Log shows: Unable to create a temp copy of patch 'AcrobatUpd1001+Tier1.msp' base package installs no problem, but unable to patch, always gives error, tried several different PC's, followed tips closely in Enterprise Administration Guide, but apparen

  • Apply Filter to static content

    Hi, I am writing a filter very similar to the one outlined in http://developer.java.sun.com/developer/JDCTechTips/2001/tt0626.html#tip2. I would like this filter to be applied to all static content on the web server. I have found a problem that the f