Re: classes and inheritance revisited
On Feb 15, 3:34 pm, "John T" <printdude1...@gmail.com> wrote:
"Lew" <l...@nospam.lewscanon.com> wrote in message
news:YLGdnaXwXYb76EnYnZ2dnUVZ_rWnnZ2d@comcast.com...
John T wrote:
You are correct. I think I'm a bit too ambitious. I need to start out
with the lowest classes, get them working first and then worry about
inheritance, encapsulation and abstract classses.
It usually makes more sense to take a roughly "top-down" approach, that
is, from the very beginning design your inheritance. You will probably
have poorer results starting at the "lowest classes". How would you even
know what is "lowest" unless you have a sense of the hierarchy?
The design or modeling phase of development is where you elicit the data
structures in your domain of discourse. If your analysis is sound, your
class design can mirror it without too very much difficulty.
Seriously though... if I have a whole bunch (there's that susinct word
again) of classes like:
houseCat
domesticDog
deer
moose
wolf
coyote
lion
tiger
bear (oh my)
snake
By nearly universal, Sun-recommended Java convention, class names begin
with an upper-case letter. If you follow that convention you will find it
easier to communicate source-code ideas.
I will get these classes working to the point where I can create new
reference objects and have arrays and ArrayLists working. Then I can
take it up one level.
Empty classes, without attributes or behaviors, are sufficient for that
goal.
- Lew
Lew,
I have done some level of analysis, in various iterations, and I have
identified the classes above as those at the lowest level. My plan is to
create detailed class instances of each of:
HouseCat
DomesticDog
Deer
Moose
Mouse
Wolf
Coyote
Lion
Tiger
Bear
Snake
Human
By detailed, I mean to have all the attributes and methods for each
initially included in the class code. Once I have all my classes coded, I
can encapsulate what varies (a design principle) and move those attributes
and methods to a super class of some sort.
So reading this thread, it appears that you are starting to understand
the Java mechanics of class hieracharies (Interface, Abstract Classes
and Classes) which is a good start. Some minor confusions or over
simplification maybe of certains aspects of these, but on the whole
you seem to have it now. I'd say you could easy see how the following
works....
public interface Animal {
void move();
String getName();
}
public abstract class AbstractAnimal implements Animal {
private String animalName;
public AbstractAnimal(String name) {
animalName = name;
}
public String getName() {
return animalName;
}
}
public class Cat extends AbstractAnimal {
public Cat(String name) {
super(name);
}
public void move() {
System.out.println( getName() + " moved!");
}
}
public class Test {
public void main(String[] args) {
Animal moggy = new Cat("Moggy");
moggy.move();
}
}
Output:
Moggy moved!
However, I do think some of your confusion is coming from trying to
model the Animal kingdom using Inheritance....
Some aspects of it should not use inheritance but instead should be
Delegation (nice word for meaning, two class instances (objects)
working together - much like 'animalName' is a String and moggy is
Cat....)
So if we tried to model your set of animals using delegation and just
a shallow inheritance tree, what would it look like?
Andrew