Linked Questions
10 questions linked to/from Why define a Java object using interface (e.g. Map) rather than implementation (HashMap)
32
votes
10
answers
8k
views
Is "Parent x=new Child();" instead of "Child x=new Child();" a bad practice if we can use the latter one?
For example,I had seen some codes that create a fragment like this:
Fragment myFragment=new MyFragment();
which declares a variable as Fragment instead of MyFragment , which MyFragment is a child ...
7
votes
5
answers
2k
views
Why are objects instantiated this way?
Some times I see an object instantiated this way.
ICustomer oCustomer = new Customer
Obvious but the Customer class is implementing ICustomer interface in this example. Are there any advantages of ...
6
votes
9
answers
4k
views
Does it make sense to declare private fields using an interface as their type?
For the fields that you have as encapsulated members of a class, does it make sense to declare their type to be of the interface that you are using? For example:
public class PayrollInfo
{
private ...
3
votes
4
answers
597
views
Do "avoid primitive obsession" and "use most abstract type as possible" contradict each other?
According to Is "avoid the yo-yo problem" a valid reason to allow the "primitive obsession"?, I should define "price" like this:
public class Main{
private Price price;
}
...
3
votes
4
answers
545
views
Does "declare the most abstract type" increase coupling actually?
According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), I know I should declare the most abstract type possible, so for example:
public interface Fruit{
}...
0
votes
3
answers
272
views
Is "my method supposes to work with specific subtype only" a reason to avoid "declare the most abstract type"?
According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), I know I should declare the most abstract type as possible. However, I found that a situation ...
1
vote
2
answers
275
views
Is "declaring most abstract type" suffers from the same problem as "primitive obsession"?
According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), when creating objects, I should declare the most abstract type, eg : Map hashMap=new HashMap(). ...
2
votes
3
answers
271
views
"use auto" and "declare most abstract type", which guideline has higher priority?
According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), I know I should declare most abstract type when possible, for example, suppose I'm using an UI ...
0
votes
1
answer
196
views
Is "Wrap child class operation into a function, then return the object, in order to declare most abstract type" necessary or over-engineering?
According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), I should declare the most abstract type as possible. However, in some cases I remember, ...
0
votes
2
answers
216
views
For non-container classes, are "better naming" and "ready for commented codes" good reasons not to declare the most abstract type?
According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), I know I should declare the most abstract type possible, but the question is about template class ...