Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Something I just dont get about the OOP concept  RSS feed

 
Matt Mason
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thank you,

Matt
[ December 15, 2006: Message edited by: Matt Mason ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony VanHorn
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you create a new class, whether it inherits from an existing class or not, and you want to use it, then yes, there will have to be some changes to existing code.

SOMEWHERE, somebody will have to change SOMETHING to do use the new stuff. But it won't necessarily be you.

Perhaps you buy code from a third party to populate your zoo. YOUR code simply calls a method, that builds the population of your zoo array:

(this method comes from a library you bought from somebody else.)

Since all they have are Lions, Tigers and Bears, you get a LOT of each of those.

6 months later, you want to do this again for a new zoo, with the same budget. You can use your same code - but the vendor now can also supply you with monkeys, reptiles and birds.

YOUR code doesn't have to change, but you'll now get lions, tigers, bears, monkeys, reptiles and birds.
[ December 15, 2006: Message edited by: fred rosenberger ]
 
Tony VanHorn
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks for the explaination.

Tony
 
Matt Mason
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. If the program is written to load an instance of class X, then that's what it does. There's no way to make it load an instance of class Y without changing it.

And that's a good thing.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For instance:



You could invoke this program like this:

java Zoo DomesticCat

or

java Zoo Tiger


Printing "meow" and "roar".
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!