Skip to main content

All Questions

0 votes
4 answers
375 views

methods that has only behavior and not manage class state, is that violation of encapsulation

This is a simple scenario in an office with employees and their manager. In this scenario, both managers and employees have a name attribute. Only the manager can change his own name Only the ...
Susantha7's user avatar
  • 406
4 votes
6 answers
2k views

The ID of an object is null at start. Does that make the object state invalid and violate encapsulation?

This question is related to How should an `Employee` class be designed? In the above question, to uniquely identify an employee, each Employee object has an id field as shown below class Employee { ...
Susantha7's user avatar
  • 406
3 votes
1 answer
319 views

Is it always optimal to preserve encapsulation in MVC?

I've just finished up a project in which I created a visual simulation of the life cycle of an ARM instruction in a single cycle processor. I used the MVC pattern in this project and ran into a design ...
Nick Borisenko's user avatar
15 votes
3 answers
23k views

Best practices for using public, protected, private?

Is it fair to say that it is good practice to default everything to private up front when coding something? And then only upgrade it to protected if a subclass needs it, or public if another class ...
AJJ's user avatar
  • 3,028
1 vote
0 answers
3k views

Change object state in different point in program

This is not about whether or not getters/setters are wrong. I understand its impact to encapsulation and that question has been raised here and SO several times already. I also do not want to just ...
bjhuffine's user avatar
  • 171
-1 votes
2 answers
1k views

Meaning of using getters and setters and Uses of parameterized Constructor. [duplicate]

I am using getters and setters to for the purpose of encapsulation. public class Student { private String studentID; private String studentName; private String address; public ...
Roovi D's user avatar
3 votes
2 answers
4k views

How is encapsulation broken by getters/setters, even when using MVC model [duplicate]

I was thinking about encapsulation in Java and then I thought how getters/setters break encapsulation. After I went through this , I saw many recommendations like to avoid getters/setters on fields ...
We are Borg's user avatar
3 votes
3 answers
548 views

'Encapsulation Vs Performance' trade-off in a lockable linked list

Main purpose of encapsulation is to protect in-variants of any class(in java). Here is the complete code for class DList/class lockDList/class DListNode/class LockDListNode. class DList and it's ...
overexchange's user avatar
  • 2,305
1 vote
1 answer
127 views

If the representation part of type abstraction is declared private, then how does sub-classing violate encapsulation?

Here, I learned that sub-classing violates encapsulation: it violates encapsulation, since the implementations of the superclass and subclass become tightly coupled For instance, If we consider ...
overexchange's user avatar
  • 2,305
0 votes
1 answer
7k views

Access fields of super class from derived classes [duplicate]

There is an abstract BaseGame class that will be inherited by specific game classes. This class has an attribute requestHandler that will be used in all subclasses and should be initialized in the ...
alexpfx's user avatar
  • 303
-2 votes
2 answers
1k views

Why public access level method get overridden in java?

This question is raised with a clarification required to decide when to declare a method protected or public during class design of a package. My point is, if one needs to override a method of a ...
overexchange's user avatar
  • 2,305
2 votes
1 answer
615 views

What's the right OO way to create a counter/inventory class that works for both differentiated and undifferentiated countables?

You are writing a videogame about trading beans. Red beans, black beans, pinto beans, you name it. As everybody knows all beans are the same. You write the "Inventory" class for a trader in that ...
CarrKnight's user avatar
4 votes
2 answers
5k views

What are the disadvantages of self-encapsulation?

Background Tony Hoare's billion dollar mistake was the invention of null. Subsequently, a lot of code has become riddled with null pointer exceptions (segfaults) when software developers try to use (...
Dave Jarvis's user avatar
26 votes
4 answers
2k views

Why Java doesn't make use of encapsulation with some classes?

My question is related with System.in and System.out classes (there might be others like those in the Standard library). Why is that? Isn't that a bad practice in OOP? Shouldn't it be used like: ...
Clawdidr's user avatar
  • 363