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
Geek.java
Address.java (Used by Person)
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...");
}
}