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 9 years ago.
Improve this question
Reflection breaks the Encapsulation principle.
Can we protect encapsulation principle being break from reflection? Is there any API through which we can protect encapsulation from reflection ?
Technically, yes. A quick search on SO gives an example of preventing Reflection: https://stackoverflow.com/a/770672/2372767. However saying,
Reflection breaks the Encapsulation principle...
implies a misunderstanding of the principle. The point of Encapsulation is not to protect your code from malicious other code, or protect your implementation, or even create some level of security.
When you make an interface (class, module, object, etc.) with public and private methods, you're actually making two interfaces: one that is easy to use, and one that isn't. Essentially, when you make something private, what you're really saying is "this is part of the messy, complicated details of getting something done, and it may be dangerous to call this directly."
The point I want to drive home is this: your private interface is still an interface, and should be treated with the same care as your public methods. While you should never encourage another programmer to use private members, you don't know when someone else is going to need to use one of those messy, complicated steps.
As other users have mentioned, there are other ways to get a private class members. Reflection is an easy-to-use API to accomplish the same task.
On of many definitions of encapsulations says this A language mechanism for restricting access to some of the object's components. You use this mechanism while you design your application. It was never a task for it to secure the application anyway. Therefore you may not use this concept as a secure mechanism and you for sure may not claim that Reflection brakes it. Only developer may brake the reflection by leaving something not encapsulated.
Related
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
Today I had an interview for test automation in one of the MNC.
They asked me "why do we need to create an object?"
I explained about OOPs concepts with example of individual bank account holders. But he is not convinced. He just need a definition.
What could be a suitable answer for that question?
You require an object to represent state.
At the most simple definition, a class defines behaviour and an instance of a class (an object) represents state.
Of course there are other things like static contexts which can also maintain state, which you can mention also, but above is the clearest answer which I believe they were looking for.
It also always helps to give an example. You could talk about, for example, an Employee class. You would need an object to represent John and another to represent Jane.
I think that this question is kind of generic and does not give much value to an interview. But some generic question should have a generic answer, and here is mine:
We need to create objects in java so we can get instances that have a state inside our application. This allows us to have persistent encapsulated elements that contain any required information, and methods that operate with it.
Just plain basic OOP theory.
There are many reasons why we create a object apart from basic oops
1) To bring up persistent state data to transactional state to perform action (curd and other) and persist back to data storage.(EJB, POJO,etc )
2) Creating handler to serve service and send fluid data across wire like web-service.
3)Stuctural behavior in action.for example you designed a class for a workflow and to make in action state we create a object and serve the behavior example validation , authorization , etc class
All in all to make design time architecture to response based live system
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 6 years ago.
Improve this question
I was looking at some code today and I came across a piece of code using reflection to take a generic object and do different things with it based on the the type. I have never seen anything like this before, and I am wondering what are the pros and cons to using reflection in java?
There are no pros or cons of reflection in Java. It's a tool which you should use in a specific situation. For example:
When you create a library which needs runtime manipulation with code.
When you have compiled jar without source code and author of jar made a mistake and didn't expose proper API.
So basically there is even no question should you use or not use reflection, it's a matter of situation. You should NOT use reflection if it possible to do the job without using it in 99.99% of cases.
UPD
Couldn't you use it for everything though? Like if you were a really big jerk you could use it to invoke every method you call, so what is stopping you from just doing that?
Mostly slowness, unmaintainable code, losing of compile time code check, breaking of encapsulation.
using reflection to take a generic object and do different things with
it based on the the type
In general, this is usually a bad idea, for reasons of performance, clarity, and robustness.
It throws away the advantages of a static type system; if you pass in types that the reflection code doesn't handle then you will get runtime errors rather than compile-time errors. If one of the classes changes implementation (e.g. renaming a method) then this will also not be detected at compile time.
If these various types have something in common, then it is usually better to handle this using polymorphism: abstract out the commonality into an interface or abstract class; each subclass can then implement the specific behaviour it needs, without other code needing to poke into the internals using reflection.
If these various types don't have anything in common, then why are they being handled together?
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 8 years ago.
Improve this question
Recently I am working on an applications (in Java and C#, but I think the problem is not closed to those languages) where I use a few container classes (which responsibilities are storing data in a proper order) and services being method packages, operating on data stored in container classes. All of the classes mentioned above should have only one copy existing in memory, and all of them are expected to be in the memory for the whole time the application is running.
I used to think that a singleton is a good idea here, as I am sure there is only one instance of each class, so it meets my expectations. However, I learned that the Singleton pattern is deprecated, as it hides dependencies and so on. Then I heard that for such usage (always available container class or method package) static classes may be a good idea. On the other hand I recently looked at a few projects where people refused to use any static stuff, as if it was an awful practice to do so.
My question is simple (at least in its formula): are static classes a good idea for creating always available, easy to hanlde containers and method packages? If not, what should I use instead (if not singletons)?
You don't really say where the data comes from. If the data is static, then a static class is a fine solution. For example, I could envision a static class to represent the 50 US states.
In contrast, for a class that represents a list of authorized users, I would use a singleton pattern. Although there is only 1 list, that list could change while the app is running.
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'm have to use 50 different custom datatypes(/classes) which are defined in a document(xml/json), they have only fields and no methods and maybe strong validations.
My question is should i go ahead and create(/generate) 50 classes or use some generic data structure (like HashMap<String,Object>)?
Update: My fear is if i go with class geneartion, then my codebase might increased by very much
and if go with schema-less way, my data integrity might be compromised, so which one is lesser evil.
Unless it is just ridiculous, more code is more forgivable, in general. There are a few different reasons:
If you give them base classes at the right points, you can have it both ways, as your handling code can hold the base classes, and may have anchor points for extracting, validating or cleaning information stored in the different formats. Surely some of the processing can be shared.
If absolutely everything really falls to the base class, you can refactor the sub-classes out of existence without pain. On the other hand, if you start the amorphous way, gathering the special cases back into separate classes is more likely to go wrong.
Excessively large code is only bad if the extra volume does not clarify the logic for readers. I would have the classes, if they constitute units in which people think.
Also, actual functionality is more important than format or even readability. So if the risk is to data integrity vs code bloat, protect the content, not the form.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
If you are familiar with the Java RDF and OWL engine Jena, then you have run across their philosophy that everything should be specified as an interface when possible. This means that a Resource, Statement, RDFNode, Property, and even the RDF Model, etc., are, contrary to what you might first think, Interfaces instead of concrete classes.
This leads to the use of Factories quite often. Since you can't instantiate a Property or Model, you must have something else do it for you -- the Factory design pattern.
My question, then, is, what is the reasoning behind using this pattern as opposed to a traditional class hierarchy system given the nature of the content the library aims to serve? It is often perfectly viable to use either one. For example, if I want a memory backed Model instead of a database-backed Model I could just instantiate those classes, I don't need to ask a Factory to give me one.
As an aside, I'm in the process of writing a library for manipulating Pearltrees data, which is exported from their website in the form of an RDF/XML document. As I write this library, I have many options for defining the relationships present in the Peartrees data. What is nice about the Pearltrees data is that it has a very logical class system: A tree is made up of pearls, which can be either Page, Reference, Alias, or Root pearls.
My question comes from trying to figure out if I should adopt the Jena philosophy in my library which uses Jena, or if I should disregard it, pick my own design philosophy, and stick with it.