OK, Im about half way throught "Head First Java" (good book) and there is something that is not clicking with me about OOP.
Supposedly the power of OOP is that you can you can modify classes and methods simply by subclassing them and modifying which ever features you wish. This allows you to make modification later without touching existing code. Am I right so far?
So, lets say my program creates a Cat object that has a "speak" method (amoung other things) which prints "Meow". Later someone else (Bob) wants to use the Cat object to make his cat but wants its speak method to print "Hiss" So he creates a subclass (bobsCat) that extends Cat and overwrites the "Speak" method. Am I still on the right track?
My question is: How can bob make the main program create his cat, "bobsCat", instead of "Cat" without getting into main and editing [Cat Fluffy = new Cat] too [bobsCat Fluffy = new bobsCat].
This might sound like a stupid question but I am SOO stuck on this.
Matt [ December 15, 2006: Message edited by: Matt Mason ]
There is some coupling or depedency irredeemably inherent in the creation of an object.
I have the impression that there may not be a real way to do this without changing or configuring something in your program, because after all, something must be an indicative to your system that you no longer want to create a Tiger, but a Bobcat.
There are some interesting ideas about how to reduce the level of dependency, so that when this change happens, the impact be minimum. Above all when the new classess (i.e. Cats) are added to the code base after the initial implementation of a program.
For instance, you could have a Cat interface, make all cat-like classes derived from this. And then have a set of Factory Methods that create all type of cats.
Since now you restricted the instatiation of Cats to a single class, the impact of the changes is minimum.
Since all Tigers are Cats, you should not have a problem in chaging this single line.
There is also an interesting pattern, know as Inversion of Control (aka Dependency Injection) that is intended to reduce this coupling at the minimum possible.
Study this pattern and see if this satifies your curiosity. At all events, a certain level of dependency still persists, and I daresay it is impossible to reduce to nothing.
I hope this helps. [ December 15, 2006: Message edited by: Edwin Dalorzo ]
I think the way you have your example set up, you can't. you have to modify your main like you are suggesting. But bear with me here.
what if instead of a concrete reference to a Cat object, you have a reference to an Animal. assuming Cat extends Animal, you can do this:
Then, you write a ton of code using myAnimal.
Now, Bob comes along, and creates the bobsCat class. you can now change the one line to this:
Now, you don't have to change ANY other code, and you will get the new bobsCat behavior everywhere else in you code, automatically. You don't have to go through all 800 lines of code, changing all the speak calls to bobsCatSpeak calls.
it's even better when you have an array of Animals. Write your code something like
check this out. you put in a cat, a dog, a monkey, and an elephant. you write the code to loop through the array, and have every animal speak, eat, sleep, run around it's cage, etc. Since all are Animals, they all have these behaviors defined, and all run the specific code needed for their type. Cats meow, dogs bark, monkeys howl, and elephants trumpet, just by doing something like this:
now, 6 months go by. Your zoo gets a grant for buying some lions, seals, bears, penguins and birds. you again extend the Animals class for these. you populate your Animals array with these new Animals.
YOU DON'T HAVE TO TOUCH THAT LOOP
How cool is that? Code you wrote when you had no idea lions even EXISTED will now work magically, because you extended your new classes from the existing type. NONE of the code that refers to the zoo array needs to change. (ok, that's a rather bold and probably untrue statement, but still...)
THAT is part of the power of inheritance.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
So either way you do this, you have to change (or add to) main? You have to put some way in main to call 'bobsCat' constructor, right? I think I understand the idea with using class animal in the method parameters, but I don't understand how one would make a new instance of bobsCat without changing something.
I also am reading the book and struggling with the same inheritance/polymorphism stuff.
Tony [ December 15, 2006: Message edited by: Tony VanHorn ]
So the vendor of the the original animal class changed it? They changed the method populateMyZoo() to include the constructors for the new animals. And anytime they wanted to add more animals they would need to change the code again to add in the new constructors. I think that makes sense.
Wow! Thank you for the very thorough explainations. Your examples answered many questions that have been blocking my learning process. But I would still like to clarify. While you can sublass and extend until youre blue in the face, you cannot instruct calling program to use the new class unless you have access sufficent to change edit it in. Correct? Unfortunately, the situations Im thinking of does not give me access to edit any existing code, for compatiblity reasons.
Originally posted by Matt Mason: While you can sublass and extend until youre blue in the face, you cannot instruct calling program to use the new class unless you have access sufficent to change edit it in. Correct?
*Unless* the program is specifically written to allow you to specify which classes to use. For example, the populateMyZoo method could accept a list of class names as input, und instantiate those classes using a mechanism called reflection. That way, you could even include animals from other vendors in the zoo, without any of the vendors having to change any code.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus