Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: *why won't this compile?*

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default *why won't this compile?*

    Hi there,

    Having a slight struggle trying to work out why this code won't compile, has anyone got any ideas?**
    public class ClassA
    {
    	int x;
    	int y;
     
    	public ClassA(int theX, int theY)
    		{
    			x = theX;
    			y = theY;
    		}
    }
     
    public class ClassB extends ClassA
    {
    	int z;
     
    	public ClassB(int x, int y, int z)
    	{
    		super(x, y);
    		this.z = z;
    	}
     
    	public ClassB(int z)
    	{
    		this.z = z;
    	}
    }
    Any help is appreciated!

    tom
    Last edited by copeg; November 2nd, 2010 at 05:17 PM.


  2. #2
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: *why won't this compile?*

    Maybe have to call super in both constructors, have the one with just z for a param call super(0,0)

    And also you probably need 2 files, incase thats in one file called ClassA, public class ClassB would need to be in a file names ClassB ...or something?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: *why won't this compile?*

    What is/are the compiler error message(s)?

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: *why won't this compile?*

    I'm not getting any... Its the final question on a coursework - "following code for the two classes in the same package does not compile as expected. Why not?"

    Then there are 6 answers (only 1 correct one) -

    1.There is no zero-argument constructor in ClassB
    2.There is no two-argument constructor in ClassA
    3.There is no zero-argument constructor in ClassA
    4.The three-argument constructor in ClassB should use this instead of super
    5.There is no two-argument constructor in ClassB
    6.There is no one-argument constructor ClassA

    I'm completely rattled on this one, however I've got a feeling its 5? Does this make sense to anyone?!

    cheers

    tom

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: *why won't this compile?*

    Quote Originally Posted by dcshoecousa View Post
    I'm not getting any...
    Throw the code into the compiler and see what the error is/are. Then make an effort to fix said error. Once that is done, the solution should be obvious. If not, post back with details of what you did, how you did it, and why you are still confused.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: *why won't this compile?*

    Throw out 1,2, and 4 right away.

    I believe the answer is 3. That is based on the compiler error message I got in ClassB.

    It's this part that's causing the problem:

     public ClassB(int z)
        {
     
            this.z = z;
        }
    implicit super constructor ClassA() is undefined. Must explicitly invoke another constructor.
    However, why? I don't quite get why. I've never come across this type of error before. Do you always need a default, or 0 argument, constructor when you plan to extend that class?

    When I add

    public ClassA()
    {

    }

    to ClassA the problem goes away, though what happens if I invoke an empty default constructor?

    The answer is clearly choice 3.

  7. #7
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: *why won't this compile?*

    I guess the why is because the compiler must throw in a "super();" in any constructor that does not call super to initialize the superclass, makes sense indeed

  8. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 2nd, 2010)

Similar Threads

  1. [SOLVED] help in java i cant compile this
    By timeline in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 15th, 2010, 02:11 PM
  2. Game of Craps: won't compile
    By FreeBird in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 15th, 2010, 05:12 PM
  3. need help to compile
    By hardwarewizard in forum Java Theory & Questions
    Replies: 0
    Last Post: February 14th, 2010, 10:03 AM