Convert object from one class to another in java [closed] - java

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 2 years ago.
Improve this question
I have 2 custom classes A and B.
Now I have an object of A let say 'a' and have to convert it to B class.
Now I have 2 approach
First is I write a transform Util which has a static method for conversion.
The second approach is to write that logic in class A with a method convertToB()
Which one is more accurate. Please suggest.

In short, it depends on the relationship between A and B.
If B extends A the conversion is done within the language when assigning an A object to a B object.
Otherwise, if there is no inheritance at play, it would be advised to either create a util class to do the conversion for the case of future changes, or have a constructor of B that has an A object as a parameter.

This is a matter of preference and a question perhaps better suited for the software engineering/design counterpart to Stack Overflow. That said, I would use the first option. If you ever add more classes and need to convert between them, you will bloat your classes with all the conversion methods. If you have one or more classes with the conversion methods, the code is cleaner.

Separation of Concerns - util class is the best approach

Related

When to use public modifier for a class field in OOP? [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 12 months ago.
Improve this question
When programming in OO languages like C# or Java, is there a good situation where declaring a public field inside a class is actually valid (I myself always use a property for not making the user of the class depend on the data and to support data protection)?
Otherwise, it feels weird that C# for example allows you to do so.
According to the C# coding conventions public field should be used sparingly:
// A public field, these should be used sparingly
public bool IsValid;
Why? I think because of:
can be edited by any other user of class
if you want to add some logic to field, then you need to create property instead of field. By doing this, you will break a contract of class
it is not possible to override variable
However, there is a case when you need to have field as #VGR said:
public const string foo = "";

Multiple Inheritance in Java . Assigning priority via extends order [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 3 years ago.
Improve this question
Oracle Java Tutorials, Multiple Inheritance of State, Implementation, and Type:
One reason why the Java programming language does not permit you to extend more than one class is to avoid the issues of multiple inheritance of state, which is the ability to inherit fields from multiple classes. For example, suppose that you are able to define a new class that extends multiple classes. When you create an object by instantiating that class, that object will inherit fields from all of the class's superclasses. What if methods or constructors from different superclasses instantiate the same field?
Couldn't this problem be easily solved by assigning priorities to all the classes a subclass extends?
class A extends B,C,D
{
}
Let us assume that priority was assigned in the ascending order in which they are mentioned in declaration of subclass .
Then priority of B < priority of C < priority of D . So if there is any state or behaviour common to any of these classes B ,C or D then priority decides what should be inherited and what should be hided.
Please advise. Thanks in advance.
Yes, roughly speaking, this how Scala supports extending a class with multiple Traits
"priorities" are determined based on a linearization equation, similar to what you mentioned so there is no ambiguity.

Is it best to call a method (from class A) in a class (class B) which then calls a method in a class (C) or to use a getter for Class C in Class B? [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 5 years ago.
Improve this question
E.g.
In Class A:
b.doThing();
In Class B:
doThing() {
c.doThing();
}
OR
In class A:
b.getClassC().doThing();
What is the convention for a situation like this?
According to the Law of Demeter, you should go the first way, i.e. only call the method of class B which itself delegates to class C. This way, you reduce dependencies between your classes which is basically a good thing for reusability and maintainability.
Usually it will be the second case unless the implementing class has do to more than executing one line for example setting/checking states or preparing other methods etc.
The big advantage of this is that you don't need to worry about implementation details by calling
b.getClassC().doThing();
You don't care if doThing will change structurally or methodically in which you would supply more parameters or else.
Object orientation is about getting objects to do things for you. Hence you're better off asking 'b' to do something, and the fact that it then calls 'c' is hidden from you, and it can be changed in the future trivially. You don't want to ask 'b' for its component objects and then do something with those.

How is spark's scala code exposed as a Java API? [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
As I know Apache Spark is written in Scala. But its functionality is also exposed as a Java API[1], which in turn can be used in Java programs.
How is this done? Can someone explain me using an example.
In other words if I write a Scala program, and I want to expose it as a java API, what steps should be taken?
[1]http://spark.apache.org/docs/latest/api/java/
It will surely depend on the type of api you are exposing, but in general
Simple methods can be called as is: class A { def doSomething(s: String) } in class A can be called just like a regular java method new A().doSomehing("hello");
Default parameters in methods will not work. You will always have to call the method with the whole parameter list.
traits with behaviour can't be implemented, but if you have common combinations you could create an abstract class and then you can extend that in java. Not sure if this will be solved with default methods for java8 in scala2.12
For object functions you need to call using the escaped path. e.g. object Container { val answer = 42 } you get the constant value from java like int answer = Container$.MODULE$.answer;.
As I said first, it mostly depends on the api you want to expose from scala to java. If you have more specific cases I can edit them in this answer.

Regarding a Class Vs Enums [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 9 years ago.
Improve this question
I was having a query , I have declared a class in which all the methods are static and it is following the utility design pattern that is it is acting like helper class Now can I replcae that class with correspond to enum also , Can I have enum having all the staic methods inside it, if Yes then what other advantages it offers ..!!
The problem with static method is: they can't be mocked for testing. At least not easily.
Putting the methods in an Enum with a single instance gets you a little closer. I'm not sure if enums can be mocked with the standard libraries, you certainly can't without using reflection.
But if you put your methods in an interface implemented by the enum, and everybody else just using the interface, accepting an instance of that interface via constructor (or setter if you have to) you can mock it as easily as you want.
Yes, you can use an enum as a utility class. There aren't many advantages to it, however: it boils down to the private constructor, which prevents uncontrolled instantiation. I would prefer sticking to the ordinary class with a private constructor since there's an expectation for an enum to be used for an enumerated type and not as a utility class. If you used enum for a singleton, that would give it only a slight bit more sense.

Categories

Resources