All Questions
Tagged with java object-oriented
485 questions
276
votes
14
answers
125k
views
Should we avoid object creation in Java?
I was told by a colleague that in Java object creation is the most expensive operation you could perform. So I can only conclude to create as few objects as possible.
This seems somewhat to defeat ...
202
votes
14
answers
141k
views
When are Getters and Setters Justified?
Getters and setters are often criticized as being not proper OO. On the other hand, most OO code I've seen has extensive getters and setters.
When are getters and setters justified? Do you try to ...
104
votes
17
answers
20k
views
Is encapsulation still one of the elephants OOP stands on?
Encapsulation tells me to make all or almost all fields private and expose these by getters/setters. But now libraries such as Lombok appear which allow us to expose all private fields by one short ...
103
votes
18
answers
69k
views
Is it poor programming practice to pass parameters as Objects? [duplicate]
So, we've got a guy who likes to write methods that take Objects as parameters, so they can be 'very flexible.' Then, internally, he either does direct casting, reflection or method overloading to ...
92
votes
10
answers
55k
views
How do you avoid getters and setters?
I'm having something of a hard time with designing classes in an oo way. I've read that objects expose their behavior, not their data; therefore, rather than using getter/setters to modify data, the ...
83
votes
12
answers
31k
views
What is the utility and advantage of getters & setters especially when they are merely used to read and assign values to properties of an object? [closed]
I’m still really new to learning to program. Just learning the syntax for a few programming languages at the moment.
The courses I viewed for C# and Java touched only very briefly on getters & ...
66
votes
11
answers
65k
views
In Object-Oriented Programming, why is it generally desirable to split a program into multiple classes? [closed]
After studying Programming through books, they have taught me concepts such as inheritance, but never explain how splitting a program into multiple classes helps with anything. Yet, many books seem to ...
59
votes
9
answers
49k
views
Should the methods of a class call its own getters and setters?
Where I work I see lots of classes that do things like this:
public class ClassThatCallsItsOwnGettersAndSetters {
private String field;
public String getField() {
return field;
}...
56
votes
3
answers
74k
views
Which is a better practice - helper methods as instance or static?
This question is subjective but I was just curious how most programmers approach this. The sample below is in pseudo-C# but this should apply to Java, C++, and other OOP languages as well.
Anyway, ...
47
votes
10
answers
28k
views
Is there any reason to use "plain old data" classes?
In legacy code I occasionally see classes that are nothing but wrappers for data. something like:
class Bottle {
int height;
int diameter;
Cap capType;
getters/setters, maybe a ...
43
votes
3
answers
63k
views
What is message passing in OO?
I've been studying OO programming, primarily in C++, C# and Java. I thought I had a good grasp on it with my understanding of encapsulation, inheritance and polymorphism.
One frequently referenced ...
43
votes
9
answers
117k
views
Why should I declare a class as an abstract class?
I know the syntax, rules applied to abstract class and I want know usage of an abstract class
Abstract class can not be instantiated directly but can be extended by other class
What is the ...
41
votes
8
answers
6k
views
Do access modifiers matter? [closed]
The theory is that access modifiers improve code safety because they support encapsulation of internal state. When doing OOP, every language I've used implements some kind of access restriction. I ...
38
votes
10
answers
14k
views
Is it a good idea to have logic in the equals method that doesn't do exact matching?
While assisting a student with a university project, we worked on a Java exercise provided by the university which defined a class for an address with the fields:
number
street
city
zipcode
And it ...
38
votes
5
answers
9k
views
When should I extend a Java Swing class?
My current understanding of Inheritance implementation is that one should only extend a class if an IS-A relation is present. If the parent class can further have more specific child types with ...