Java Newbie: Constructors and sub-Classes?

TommyWillB

Registered
I have a class named Person and a sub-class named Geek. Within geek I try to create a new Geek, and am told that "The constructor Geek(String) is undefined".

I guess I'm expecting that instansiating a new Geek will call the apropriate constructor in it's partent, Person. But it seems to expect Geek to also have those same constructors.

Is that correct, or am I just missing something?

Person.java
Code:
package net.javagarage;

/**
 * net.javagarage/Person.java
 */

public class Person extends java.lang.Object {	
	
	public String name;
	public int age;
	
	// Address is a class
	public Address address;	
	
	// THREE constructors below:	
	// 1) public NO-arg constructor
	public Person() {}
	
	// 2) public (single) ARG constructor
	public Person(String name) {
		this.name = name;		
		// rest of the varables have no corresponding args
		// so let's just let them be NULL...
		this.address = new Address();
   }
	
	// 3) public (multiple) ARG constructor
	public Person(String name, int age) {
		this.name = name;
		this.age = age;		

		this.address = new Address();
		this.address.street = "1 Private St";
		this.address.city = "San Francisco";
		this.address.stateLong = "California";
		this.address.zip = "99999";
	}
	
	public static void main(String[] args) {
		// passing parameters to constructor to populate Person object JEFF
		_12_Person jeff = new _12_Person("Jeffy K");
		System.out.print(jeff.name + " (Age " + jeff.age + ") lives at:\r\t" + jeff.address.street + "\r\t" + jeff.address.city + ", " + jeff.address.stateLong + " " + jeff.address.zip + "\r\r");	

		// passing parameters to constructor to populate Person object TOM
		_12_Person tom = new _12_Person("TommyWillB", 36);	    
		System.out.print(tom.name + " (Age " + tom.age + ") lives at:\r\t" + tom.address.street + "\r\t" + tom.address.city + ", " + tom.address.stateLong + " " + tom.address.zip + "\r\r");
	
		// System.out.print(tom);
	}
	

	public String getName() {
		return this.name;
	}
	
	public void setName(String nameIn) {
		this.name = nameIn;
	}
	
	public int getAge() {
		return this.age;
	}
	
	public void setAge(int ageIn) {
		this.age = ageIn;
	}
}

Geek.java
Code:
package net.javagarage;

/**
 * <li>net.javagarage/Geek.java
 */

public class Geek extends Person {
	
	String onlineName;
	
	void writeCode(String someCode) {
		System.out.print("\rWrote this code:\r\t" + someCode);
	}
	
	void slackOff(long howLong){
		System.out.print("\rI'm slacking for the next " + howLong + " minutes.");
	}

	public static void main(String[] args) {
[color=Red]		Geek geek = new Geek("Jane");	 // This is the line throwing the error[/color]
		System.out.print("\rHi " + geek.name);
	}
}

Address.java (Used by Person)
Code:
package net.javagarage;

/**
 * <li>net.javagarage/Address.java
 */

public class Address {
	public String street;
	public String city;
	public String stateLong;
	public String stateAbrev; //convert to state Abreviation
	public String zip;
	
	
	public String getStateAbrev(String stateIn) {
		
		if (stateIn == "California") {
				stateAbrev = "CA";
			}
		else if (stateIn == "Ohio") {
			stateAbrev = "OH";
		}
		else if (stateIn == "Texas") {
			stateAbrev = "TX";
		}
		else {
			stateAbrev = "UNKNOWN";
		}
		
		return stateAbrev;
	}

	public static void main(String[] args) {
		// System.out.println("Hi from address class...");
	}
}
 
Create the constructors in Geek that correspond with the Person constructors and the use super(arg) to call the superclass constructor.

For example:

Code:
        // 3) public (multiple) ARG constructor
        public Geek(String name, int age) {
                super(name, age);
        }
 
Okay... that looks like this will get me over this hump. THANKS Eternal!

I'm just not sure why the book didn't include these extra Geek constructors. Are these unneeded in Java 1.5, but needed in my 1.4?
 
Every-time a class is instantiated, a constructor must be called. If the subclass doesn't explicitly call the super classes constructors, it will automatically call the default constructor (i.e. the no arg constructor) of the super class.

If you replaced the offending line that throws the error with the following:
Code:
Geek g = new Geek();
g.setName("Bob");

The above will work, since you are instantiating Geek g with the default constructor and then proceeding to call the setName method, which was inherited from the super class.

If you want to pass arguments to the Geek constructor, you will need to explicitly define them.
 
Back
Top