last day (14 days later) » 

7:41 PM
5
A: Java - A smarter way to determine the child class?

Loris SecuroYou could put the method checkPrice in your Fruit class and eventually every child could override it. public class Fruit { String name; public Fruit(String a){ this.name = a; } // just a default implementation that does nothing // (or does whatever you want) publ...

 
pms
This code won't work as parent class doesn't have checkPrice method. Use instanceof instead.
 
@pms at the start I said to put checkPrice in theFruit class...
 
pms
But that is even pointless, as he wants to just check the apples and not oranges. The code you suggested needs checkPrice of orange to do nothing which is a bad practice.You cannot force people to have a method and do nothing in it
 
@pms if Fruit has a method checkPrice that does nothing, the childs don't have to implement it, they will inherit the default implementation of Fruit
 
@pms Loris is right in general. Maybe not all instances need checkPrice, but the proper way to solve this problem is with inheritance and not some brittle use of instanceOf.
 
pms
7:41 PM
@LorisSecuro Again, you cannot gurantee that the child classes do not override the methods and also you cannot guarantee to override empty methods. THAT IS A BAD PRACTICE.
@TheThom but the proper way to solve this problem is with inheritance and not some brittle use of instanceOf please make it clear. This sentence doesn't make sense to me.
 
Having an empty checkPrice method is far less offensive than using instanceOf. An empty method body is not bad practice. Using instanceOf and downcasting is going against object oriented programming principles.
 
@pms AFAIK it is not bad practice at all, it's the CORRECT practice
 
pms
@LorisSecuro In this case it is a bad practice because you let developers who try to extend this class, override checkPrice method and when they do that, it will screw up with cartInventory method. So you have to fix it. The assumption here is that there is need for check price just on apple instance. I think that is very clear now that why your solution is bad practice here.
 
@pms It is bad practice. Obviously, because the need exists, there is a method that needs to be available in the base class so that client classes can identify what's really going on. He's using a simplified example, so I can't look into the problem domain to design the correct solution, but usually instanceOf is used to solve a design flaw in your hierarchy.
 
pms
@TheThom I am just looking at this simplified example and even with that, it is bad practice. We are not talking about designing a suffiisticated system. I don't like to use instanceOf either, unless when overriding the equals method.
@AmirAfghani As a matter of fact, inheritance is against OOP. public and private keywords for variables are just a hack for OOP. So many other things in java are either againt OOP or not properly fit with OOP. C is more OOP than C++ and Java. So don't bother yourself with OOP priciples too much. I personaly care more about design patterns or so other important principles.
 
7:41 PM
@pms how is overriding checkPrice screwing up with cartInventory? It will just make possible to improve the cartInventory functionality for others type of Fruits, without having to directly modify cartInventory
 
@AmirAfghani Inheritance is against OOP? Site your source. I've never heard that before. Granted that anything other than private variables are not good design.
 
pms
@LorisSecuro if you just do this: cart[i].checkPrice(); in cartInventory method, then all subclassess that extend the Fruit can override the checkPrice method. So if say, you add Banana and do price checking on it, you will be printing the Banana price checking output. But that is not the output you expected. You just needed Apple. Now imagine the code gets more complicated and instead of printing, do some calculation, then you add Banana to your calculations which was not supposed to be there.
 
@TheThom I never said inheritance is against OOP. Please read my comment and answer carefully.
 
@pms if you don't want Bananas to do anything then you don't override the method... if you want then you do... and if other developers creates new Fruits then in the documentation of the method you explain how it is used and how it should be used. I don't see all of that as a problem.
 
@pms Inheritance is against OOP? Site your source. I've never heard that before. Granted that anything other than private variables are not good design.
 
7:41 PM
@TheThom np, happens to me sometimes too
 
pms
@AmirAfghani en.wikipedia.org/wiki/…. Java inventor James Gosling has spoken against implementation inheritance, stating that he would not include it if he were to redesign Java.
@AmirAfghani also this speech from Uncle Bob: youtube.com/watch?v=mJ2lHLDRc6g&spfreload=10
 
@pms Why don't you post an answer to this question that you think is suitable instead of commenting and cherry picking statements. Lets see your answer to the question asked by the OP. I'd also love to hear you substantiate the claim that C is more OOP than C++ and Java. That was a true gem.
"Use instanceOf instead" - why don't you go ask Gosling what he thinks of doing that?
 
@AmirAfghani That is why he thinks it should not have been in Java, not why it's not OOP.
 
@TheThom I think you misunderstood. I am quoting pms's first suggestion to use instanceOf. Either way I'm done with this question until I see his/her solution. Holy moly what a thread here...
 

last day (14 days later) »