On 06/08/2008 20:07, jsguru72 allegedly wrote:
Thanks for your reply. After posting, I did additional testing and
came up with a solution that seems to work. It is similar to what you
provided Joshua.
On 06/08/2008 19:58, Joshua Cranmer allegedly wrote:
> This is how I do it:
>
> public abstract class Fruit {
>
> private String type;
>
> protected Fruit(String type) {
> this.type = type;
> }
>
> public String whatAmI() {
> return this.type;
> }
> }
>
public class Apple extends Fruit {
public Apple() {
super("Apple");
}
}
Alternatively...
<code>
//Parent Abstract Class
public abstract class Fruit {
protected String fruit = "Fruit";
public String whatAmI() {
return fruit;
}
}
//Subclass
public class Apple extends Fruit {
public Apple() {
fruit = "Apple";
}
public static void main(String [] args) {
Apple myApple = new Apple();
System.out.println("-->" + myApple.whatAmI() );
}
}
</code>
...would work, too. It's not very clean, however.
It certainly is not very clean.
The sub class could forget to set the protected field.
The constructor method forces the sub class to specify a name.