Re: Inheritance and lousy Frame
Ian Wilson wrote:
NickName wrote:
I'm exploring the Java's Inheritance feature.
Then I'd start by exploring that in isolation from other complicated
Java features.
to demo how they
works, one needs some sort of interface like a frame or window to
display each subclass's properties, and I find the Frame, JFrame and
JPanel etc. very annoying in the sense that JBuilder provides some GUI
to use them and yet, the fields for each subclass do not appear right,
so, I did not even run the code.
Place your exploratory code within a Java console application and use
System.out.println() to display your results. Add a toString() method to
your Classes to facilitate this.
When you have explored inheritance sufficiently, then move on to
exploration of other Java concepts. Do GUI apps later. Don't try to do
too many new things at once.
-----------------------------------------------
class ExploreInheritance {
public static void main(String[] args) {
Dog fido = new Dog();
System.out.println(fido);
}
class Mammal {
int legs = 0;
Mammal(int legs) {
this.legs = legs;
}
public String toString() {
return "A mammal with "+legs+" legs";
}
}
class Dog extends Mammal {
Dog() {
super(4);
}
}
}
------------------------------------------------
Untested. Caveat emptor. Batteries not included.
First let me thank you and every one else who responded to my question.
Now, the exact code above generated "non-static variable this cannot
be referenced from a static context ..."
err msg. But being a "genius" myself I fix it :), ok, earnestly, with
a little research, I added the key word prefix, "static" to the two
classes, then the code are in "snyc" and works.
(This part is unintended gain :)
And yes, the idea of ingoring other features first while learning
Inheritance is a sound one.
Once again, many thanks.