How does Casting limit Algorithm Reuse in java? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
We are learning about both of these things in Java class right now. I believe I understand the basic aspects of both, but not sure about how Casting ends up limiting Algorithm Reuse. Our teacher said we need to know this for the test next week. Can anyone explain this?

If you cast you are limiting your algorithm to only work with one Class (or it's children). If you were instead to use an Interface you would be able to accept a greater variety of Objects that themselves implement that Interface. Much more flexible.
Here is a related SO question: Explaining Interfaces to Students

When you use casting in your code, you must know the exact type that you cast to (during the code write phase). Hence your code can't be reused in the future with different types. Always remember to program to interface instead of to specific type.

Related

why switch or if else statement is not considered polymorphic? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Every answer on stack overflow provides information on how to replace switch or if else with polymorphism but
the switch, if else constructs also help in providing different behavior of an object with respect to context and inputs.
Why if else,switch is not considered as a part of polymorphism.
Conditional becomes a code smell when we have to check an object’s type in order to make some logic or behavior decision. It doesn’t matter whether it is a stack of if/else block or a switch statement.This violates open-closed principle.
The open closed principle states that the entities(classes, modules, functions etc) should be open for extension,but closed for modification.Which means that these entities won't be allowed to make changes in its source code.
This can be achieved through Abstraction and Polymorphism.
Benifits of Polymorphism over Conditionals
instead of asking an object about its state and then performing actions based on this, it is much easier to simply tell the object what it needs to do and let it decide for itself how to do that.
Removes duplicate code. You get rid of many almost identical conditionals.
If you need to add a new execution variant, all you need to do is add a new subclass without touching the existing code (Open/Closed Principle).
Polymorphism in a concept that allows an Object to retain behavior and properties from its parent classes. Explained further here and here
if, else and switch are procedural code constructs.
The two have nothing in common.
Edit: Thanks #Ted Hopp, correcting my post.

Java call performance vs search performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Currently my program is filled with many ugly references that often make field or method access look like this: weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2. I want to move to shorter and faster strong references like field1.field2. But my program design is such that I will also have to go for an ArrayList element-by-element search (in a for-loop) instead of accessing a WeakHashMap by get() method.
Thus, I'd like to understand, can moving to simpler references compensate for rejecting HashMap performance wise. Herewith I presume that WeakHashMap.get() is much faster than a loop-search of ArrayList.
Can someone, please, give me a rough estimate? Or maybe there's even an appropriate comparison table like this one. I'd appreciate that.
Thank you.
Currently my program is filled with many ugly references that often make field or method access look like this:
weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2
Given that the objects involved are not Data Transfer Objects
this is a violation of the law of Demeter aka Don't talk to Strangers / Tell, don't ask!
Following this LoD principle you should move the operations working with the data in field2 to a new method in the class SomeCustomObject.

Alternatives to reflection when accessing an arbitrary field [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I was using this code to create some sort of a universal changer class:
//constructor method
public Change(Object affdObj, String affdField, float modifier) {
obj = affdObj;
//...
affectedField = affdObj.getClass().getField(affdField);
//...
affectedField.setFloat(obj, affectedField.getFloat(obj) + modifier);
}
But then I was advised to avoid reflection whenever possible since it's very slow. I was suggested to pay attention to interfaces. Unfortunately I can't see how to fit interfaces to my code.
Hence my question: if one needs to access a field which name he doesn't know in advance are there any options other than using reflection?
PS
Thank you for replies, guys.
And since my question is put on hold as primarily opinion-based, I consider this to be the answer to my question, i.e. there is no other way to achieve my goal which is better than mine in every aspect. In other words, I conclude that my approach is OK. Thank you.
First of all, reflection is not slow (anymore) and is widely used (Spring uses it, Hibernate uses it, etc.). So, you use it with confidence if your only concern is speed.
Regarding other ways to do what you want, since you provide the field name as as a string and identify it like that, you cannot do it with interfaces.

can anyone tell me why java doesnt support multiple inheritances........? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
i tried to create a calc class which will have methords of mathematical operators which are not in java already..........
now after i created it ....if i wat to use this class functions i will have to make it a super class for my new program but .....if i want my new program to have multiple attributes of diffrent classes .........and simultaneously use calc functions........but i cant..............
why java doesnt have multiple inheritances.......what are its advantages and disadvantages?
tnx in advanced...
Java doesn't support multiple inheritance because of the "diamond problem" and other problems that arise from "increased complexity and ambiguity" as is explained in the wikipedia page on the matter
The creators of Java had a design goal of simplicity. That is why operator overloading with the added complexity of the "copy constructor" was also left out. That is why there is automatic memory management etc etc
most modern languages chose to discard this concept for the same reason.

how to bring abstraction in java application..logic for abstraction in project [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I know abstract classes and interfaces in java but I want to know how to bring abstraction in working software/project? how to thing in such way which brings abstraction.
Your question is vague at best, however the usage of interfaces helps abstraction because you are not working with concrete types. For instance:
IPrinter p = PrinterFactory.getPrinter(conditions);
...
p.print(content);
In the below line, you are not aware of exactly what printer you are using. Since you are just using the logic, you do not really care. All that you care about is that the factory will give you the printer you are after and that the print method will print the content to the right stream.
If you want to change the printer being used, you make the amendments in the factory class so that you get a different IPrinter implementation which does what you need (which in this case it would be to print to some other media). This would mean that you have essentially changed the outcome of a piece of code without changing much of it.

Categories

Resources