3.1. Answer these questions: a. Explain how encapsulation is used for the implem
ID: 3749066 • Letter: 3
Question
3.1. Answer these questions:
a. Explain how encapsulation is used for the implementation of the
Day class from the textbook and what is achieved by using that principle
(i.e. advantages versus not using it).
b. What is the role of a contract when we write a method ?
c. What are the advantages of making a class immutable ?
Discuss in principle, then in the context of the String Java class.
d. What is the argument of the Law of Demeter ? Explain why we should
(try to) follow it.
e. Explain why a lack of consistency in class design is detrimental.
Explanation / Answer
a. Encapsulation is an object oriented concept which wraps up data in a single unit. It binds together the code and data it manipulates. If we talk about it technically though, it simply controls the access of variables and methods of a class from other classes. Therefore, it is also known as data hiding.
To achieve encapsulation, we can declare all the variables in a class as private and the methods as public. Therefore, the data remains safe and we can only access them with the methods of that class.
eg:-
//in Java
import java.util.Scanner;
public class Capsule{
private String name;
private int age;
public void input(){
Scanner sc=new Scanner(System.in);
name = sc.nextInt();
age = sc.nextInt();
}
}
public class Test{
public static void main(String[] args){
Capsule obj = new Capsule();
obj.input();
}
}
Advantages of Encapsulation:
1) Saves from illegal access of data.
2) Let's us have a flexible code which can be changed or updated later.
3) Also hides complexity. Helps in abstraction.
b. In general, a contract of method states which parameters are accepted to methods of a type, what you should and should not expect this method to do, invariants supported by this method etc.
They specify conditions and restraints on methods, especially useful in method overriding. It is like, we are defining certain rules and horizons for the methods in question.
One more line we can add to this, a contract specifies what the component expects of clients and vice versa.
c. Let's first see what an immutable class is. It basically means, that once an object gets created, its content cannot be changed. In java, all wrapper classes like String, Boolean etc are immutable. We can also create our own immutable class by declaring it final , along with the data members being final, a parameterized constructor, a getter method with all variables in it and no setters.
Advantage:
1. Thread safe, so no synchronization issues.
2. Easier to design, implement and use.
3. Easier to parellerize as there is no conflict between objects.
4. Internal state remains consistent even if there are exceptions.
5. In Hashing, it provides fast operations.
String Java class is a wrapper class which is immutable too. The reasons are:
1. Security: Across many databases and languages, parameters are typically String, therefore, if String is not immutable, then anyone can change the data.
2. Loading of class: Strings are used as arguments for loading classes. If String is not immutable, wrong class could be loaded.
3. Caching: When a compiler optimizes String objects, it sees that two objects have same value, and thus, you need just one String object for execution.
4. Synchronization: String needs to be thread safe and making it immutable achieves the goal, therefore no sync issues.
d. The Law of Demeter states that "For all classes C, and for all methods M attached to C, all objects to which M sends a message must be M's argument objects, including self objects, or the instance variable objects of C". In simpler terms, the law helps in formulating what we are allowed to do in a given method. A method of an object may only call methods of the object itself, an argument of the method, any object created within the method ,any direct properties/fields of the object.
Why should we try to follow it though? :
1) Promotes flexibility of code.
2) It basically tells us that a method should not know too much. Hence, it promotes security and privacy of methods.
3) Helps in writing a cleaner code and not cluttering it.
4) Produces proper, well designed, object oriented code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.