18 Questions
What is the problem with the code snippet: expenses = new Expenses(100, 10); employee.getDepartment().getManager().approveExpense(expenses);
?
It is a violation of the Law of Demeter
What is the effect of the tightly coupled classes in the code?
Reduced cohesion
What is the benefit of following the Law of Demeter?
Increased modularity and flexibility
What is a characteristic of a well-designed function according to the Law of Demeter?
It has a single responsibility
What is an effect of a global variable accessible by all classes?
Tighter coupling between classes
What is the main principle of the Law of Demeter?
A method of an object should only call methods of a limited set of objects
What is a challenge of object-oriented code?
It makes it hard to add new functions because all the existing classes must change.
When is procedural code more appropriate?
When we want to add new functions more than data types.
What is the principle of the Law of Demeter?
A module should not know about the inner details of the objects it manipulates.
What is a consequence of not following the Law of Demeter?
Tight coupling between objects.
What is the main idea behind the Law of Demeter?
A method should only call the methods of its own class, its parameters, and any objects created within it.
What is the alternative way to describe the Law of Demeter?
Only talk to your immediate friends.
What is the primary goal of abstraction in data structures?
To hide the internal details of the data
What is the main difference between data structures and objects?
Data structures are public and have no functions, while objects are private and have functions
What would happen if a new function is added to the Geometry class in the Procedural Shape Example?
The shape classes would be unaffected
What is the advantage of using object-oriented programming in the Polymorphic Shapes Example?
It makes it easy to add new shapes without changing the existing functions
What is the concept demonstrated by the Procedural Shape Example and the Polymorphic Shapes Example?
Data/Object Anti-Symmetry
What is the main difference between procedural code and object-oriented code in terms of adding new functions and data structures?
Procedural code makes it hard to add new data structures, while object-oriented code makes it easy to add new functions
Study Notes
Object-Oriented vs Procedural Code
- OO code makes it hard to add new functions because all existing classes must change
- OO code makes it easy to add new classes without changing existing functions
- Procedural code is the opposite: easy to add new functions, hard to add new classes
When to Use OO Code
- In complex systems where new data types are likely to be added
- When the primary focus is on adding new data types rather than functions
When to Use Procedural Code
- When new functions are likely to be added, but not new data types
- When the primary focus is on adding new functions rather than data types
The Law of Demeter (LoD)
- States that a module should not know about the inner details of objects it manipulates
- A software component or object should not have knowledge of the internal workings of other objects or components
- LoD is a specific case of loose coupling
Law of Demeter Rules
- A method M of class C should only call methods of:
- C itself
- M's parameters
- Any objects created within M
- C's direct component objects
- Global variables accessible by C, in the scope of M
Law of Demeter Principles
- "Only talk to your immediate friends"
- "Don't talk to strangers"
- Abstract data representation, hiding internal details
Data/Object Anti-Symmetry
- Objects hide their data (private) and have functions to operate on that data
- Data structures show their data (public) and have no functions
- Procedural code uses data structures, making it easy to add new functions but hard to add new data structures
- OO code uses objects, making it easy to add new data structures but hard to add new functions
Law of Demeter Violation Example
- Code that violates LoD allows method calls on objects returned by other methods, leading to tight coupling between classes
This quiz assesses understanding of Java programming principles, including static fields and the Law of Demeter. It covers concepts related to variable scope and method calling.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free