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 6 of 6

Thread: Invoking a superclass constructor in my subclass

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Invoking a superclass constructor in my subclass

    I have a superclass called AstroObj, and two subclasses, Planet and Star.
    Everything on my code works except the constructor that takes all parameters.

    I want to inherit the name, galaxy, category and mass from my superclass but I'm getting an error that says they cannot be resolved. Can someone please tell me what is wrong here? (I didn't include the rest of my code cause everything else is fine, including the application program in the main method.)

    AstroObj
    public class AstroObj 
    {
    	private String name;     //name assigned to object
    	private String galaxy;   //name of galaxy which object belongs
    	private String category; //category associated with the object
    	private double mass;     //mass of object
     
    	//default constructor, no parameters
    	public AstroObj()
    	{
    		name = "unknown";
    		galaxy = "unknown";
    		category = "unknown";
    		mass = 0.0;
    	}
     
    	//constructor with parameters
    	public AstroObj(String nameIn, String galaxyIn, String categoryIn, double massIn)
    	{
    		name = nameIn;
    		galaxy = galaxyIn;
    		category = categoryIn;
    		mass = massIn;
    	}

    Planet
    public class Planet extends AstroObj
    {
    	private int numMoons;   //additional instance variable
    	private double avgTemp; //additional instance variable
     
    	//subclass, default constructor
    	public Planet()  
    	{
    		super();
    		numMoons = 0;
    		avgTemp = 0.0;
     
    	}
     
    	//constructor with all parameters
    	public Planet(String nName, String nGalaxy, String nCategory, double nMass, int moonsIn, double tempIn)
    	{
    		super(initName, initGalaxy, initCategory, initMass);
    		numMoons = moonsIn;
    		avgTemp = tempIn;
     
    		if(moonsIn < 0)  //check for invalid number of moons
    		{
    			numMoons = 0;
    		}
     
    	}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Invoking a superclass constructor in my subclass

    What are initNAme, initGalaxy, initCategory, and initMass? When do you initialize them?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Invoking a superclass constructor in my subclass

    well they're supposed to be the variables from the superclass AstroObj, i just called em something different than what is gonna be passed from the main so i wouldn't get confused.

    main
    		//first object with no parameters
    		AstroObj astro1 = new AstroObj();
    		System.out.println(astro1.toString());
     
    		//planet object
    		Planet planet1 = new Planet("Earth", "Milky Way", "Planet", 5.97E24, 1, 60.0);
    		System.out.println(planet1.toString());

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Invoking a superclass constructor in my subclass

    Quote Originally Posted by kari4848 View Post
    well they're supposed to be the variables from the superclass AstroObj, i just called em something different than what is gonna be passed from the main so i wouldn't get confused.
    Well, you can't do that. You have to declare and initialize a variable before you can use it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    kari4848 (April 29th, 2011)

  6. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Invoking a superclass constructor in my subclass

    Ahh, I fixed it and it works great now. Thank you for your help!

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Invoking a superclass constructor in my subclass

    No problem. Glad you got it sorted.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. subclass that implements
    By speedycerv in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2011, 07:35 AM
  2. Replies: 0
    Last Post: January 17th, 2011, 05:14 AM
  3. [SOLVED] Cannot Find Constructor
    By jrookie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 8th, 2010, 02:54 PM
  4. [SOLVED] Array of objects, invoking constructor for one changes others
    By BigFoot13 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 24th, 2010, 01:30 PM
  5. Instantiating a SuperClass Map from a SubClass
    By drexasaurus in forum Collections and Generics
    Replies: 1
    Last Post: September 9th, 2010, 10:51 PM