Which of the following is are not true about encapsulation
After this lecture, students should: A program is a set of instructions we issue to computers to manipulate data. A programming language is a formal language that helps programmers specify precisely what are the instructions we issue to
computers, using code that are often made up of keywords, symbols, and names. Computers execute the instructions in their processing units, and store the instructions and data in their memory1. The processing units recognize the instructions based on the specific patterns of bits and manipulate data as a sequence of bits. A programming language,
however, is written at a higher level of abstraction (i.e., at a higher conceptual level), so that as a programmer, we only need to write a few lines of code to give complex instructions to the computer. A compiler or interpreter is responsible for translating these programs written in high level language to assembly code or machine code, i.e., bit patterns that the processing units can understand. There are thousands of programming languages in
existence. C is one of the languages that is a low-level language -- i.e., it provides a very thin layer of abstractions on top of machine code. On the other hand, languages such as Python and JavaScript are high-level languages. As an example, in C, you can directly manage memory allocation. In JavaScript and Python, you cannot. One of the important abstractions that is provided
by a programming language is variable. Data are stored in some location in the computer memory. But we should not be referring to the memory location all the time. First, referring to something like Let's think a bit more about how a sequence of bits is abstracted as data in a programming language. At the machine level, these bits are just, well, bits. We give the bits a semantic at the program level, e.g., we want to interpret the sequence of bits as numbers, letters, etc. E.g., the number (integer, to be exact) The type of a variable tells the compiler or the interpreter how to interpret the variable and how to manipulate the variable. For instance, supposed that in Python, if you have two variables In the last instance above, you see that assigning a type to each variable helps to keep the program meaningful, as the operation Python is a dynamically typed language. The same
variable can hold values of different types, and checking if the right type is used is done during the execution of the program. Note that, the type is associated with the values, and the type of the variable changes depending on the value it holds. C, on the other hand, is a statically typed language. We need to declare every variable we use in the program and specify its type. A variable can only hold values of the same type as the type of the variable, so we
can't assign, for instance, a string to a variable of type By annotating each variable with its type, the C compiler also knows how much memory space is needed to store a variable. Another important abstraction provided by a programming language is function (or procedure). This abstraction allows programmers to group a set of instructions and give it a name. The named set of instructions may take one or more variables as input parameters, and return
one or more values. Like all other abstractions, defining functions allow us to think at a higher conceptual level. By composing functions at increasingly higher level of abstractions, we can build programs with increasing level of complexity. Defining functions allow us to abstract away the implementation details from the caller. Once a function is defined, we can change the way the function is implemented without affecting the code that calls the function, as long as the semantic
and the interface of the function remains the same. Function, therefore, is a critical mechanism for achieving separation of concerns in a program. We separate the concerns about how a particular function is implemented, from the concerns about how the function is used to perform a higher-level task. Defining functions also allow us to reuse code. We do not have to repeatedly write the same chunk of code if we group the sequence of code into a function --
then we just need to call the function to invoke this sequence of code every time we need it. If this chunk of code is properly written and debugged, then we can be pretty sure that everywhere the function is invoked, the code is correct3. C is a procedural language. A C program consists of functions, with the Recall that the bits representing the instructions are also stored in the computer memory in an area separated from the data. The instructions that belong to the same
function are stored in adjacent memory locations. Just like we can refer to a variable using its memory address through its reference (or pointer), we can refer to a function using the memory address of the entry point to the function. Just like functions allow programmers to group instructions, give it a name, and refer to it later, a composite data type allows programmers to group
primitive types together, give it a name (a new type), and refer to it later. This is another powerful abstraction in programming languages that help us to think at a higher conceptual level without worrying about the details. Commonly used examples are mathematical objects such as complex numbers, 2D data points, multi-dimensional vectors, circles, etc, or every day objects such as a person, a product, etc. Defining composite data type allows programmers to abstract away (and be
separated from the concern of) how a complex data type is represented. For instance, a circle on a 2D plane can be represented by the center ( In C, we build composite data type with Once we have the Implementing these functions obviously requires the knowledge of how a circle is represented. Once the set of functions that operates on and manipulates circles is available, we can use circle type without worrying about the internal representation. If we decide to change the representation of a circle, then only the set of functions that operates on a circle type need to be changed, but not the code that uses circles to do other things. We can imagine an abstraction barrier between the code that uses a composite data type along with its associated set of functions, and the code that define the data type along with the implementation of the functions. Above the barrier, the concern is about using the composite data type to do useful things, while below the barrier, the concern is about how to represent and manipulate the composite data type. While many of you are used to writing a program solo, in practice, you rarely write a program with contributions from a single person. The abstraction barrier separates the role of the programmer into two: (i) an implementer, which define that data type and provide the implementation, and (ii) a client, which uses the composite data type to perform a higher level task4. Part of my aim in CS2030 is to switch your mindset into thinking in terms of these two roles. Note that the implementer and the client may very well be the same programmer. Abstraction: Class and Object (or, Encapsulation)We can further bundle the composite data type and its associated functions together in another abstraction, called a class. A class is a data type with a group of functions associated with it. We call the functions as methods and the data in the class as fields (or members, or states, or attributes4). A well-designed class maintains the abstraction barrier, properly wraps the barrier around the internal representation and implementation, and exposes just the right interface for others to use. Just like we can create variables of a given type, we can create objects of a given class. Objects are instances of a class, each allowing the same methods to be called, and each containing the same set of variables of the same types, but (possibly) storing different values. Recall that programs written in a procedural language such as a C consists of functions, with a One could argue that an object-oriented way of writing programs is much more natural, as it mirrors our world more closely. If we look around us, we see objects all around us, and each object has certain properties, exhibit certain behavior, and they allow certain actions. We interact with the objects through their interfaces, and we rarely need to know the internals of the objects we used everyday (unless we try to repair it)5. The concept of keeping all the data and functions operating on the data related to a composite data type together within an abstraction barrier is called encapsulation. Breaking the Abstraction BarrierIn the ideal case, the code above the abstraction barrier would just call the provided interface to use the composite data type. There, however, may be cases where a programmer may intentionally or accidentally break the abstraction barrier. Consider the case of implementing This code would still be correct, but the abstraction barrier is broken since we now make explicit assumption that there are two variables
Breaking Python's Abstraction Barrier Python tries to prevent accidental access to internal representation by having a convention of prefixing the internal variables with Data HidingMany OO languages allow programmers to explicitly
specify if a field or a method can be accessed from outside the abstraction barrier. Java, for instance, supports Such mechanism to protect the abstraction barrier from being broken is called data hiding or information hiding. This protection is enforced by the compiler at compile time. Example: The Circle classLet's put together the concepts of encapsulation and data hiding to define a
Here, we define Constructors, Accessors, and MutatorsWith data hiding, we completely isolate the internal
representation of a class using an abstraction barrier. With no way for the user of the class to modify the fields directly, it is common for a class to provide methods to initialize and modify these internal fields (such as the A
constructor method is a special method within the class. It cannot be called directly but is invoked automatically when an object is instantiated. In Java, a constructor method has the same name as the class and has no return type. A constructor can take in arguments just like other functions. The class
The use of accessor and mutator methods is a bit controversial. Suppose that we provide an accessor method and a mutator method for every private field, then we are actually exposing the internal representation, therefore breaking the encapsulation. For instance:
The examples above are pretty pointless. If we need to know the internal and do something with it, we are doing it wrong. The right approach is to implement a method within the class that do whatever we want the class to do. For instance, suppose that we want to know the circumference of the circle
where A better approach would be to add a new method
The better approach involves writing a few more lines of code to implement the method, but it keeps the encapsulation intact. If one fine day, the implementer of Constructor in Python and JavaScript In Python, the constructor is the Class Fields and MethodsLet's look at the implementation of In C, we define \pi as a macro constant A solution to this is to associate these global values and functions with a class instead of with an object. For instance. Java
predefines a
We call these fields and methods that are associated with a class as class fields and class methods, and fields and methods that are associated with an object as instance fields and instance methods. Class Fields and Methods in Python Note that, in Python, any variable declared within a
In the above example, Example: The Circle classNow, let revise our
Creating and Interacting with Circle objectsTo use the
We will write a complete Java program with The demonstration below loads the
Reference Type vs. Primitive TypeThe variable The other variable type supported in Java is primitive type. A variable of primitive type stores the value instead of a reference to the value. Java supports eight primitive data
types: You can read all about Java variables and primitive data types in Oracle's Java Tutorial. Type SafetySome languages are stricter in terms of type "compatibility" than others. C compilers, however, are not very strict. If it detects something strange with the type you used, it will issue a warning, but still let your code compiles and runs. Take:
In Line 4, we treat the address to a string as integer. This generates a compiler's warning. In C, you can type cast a variable from one type into another, i.e., force the compiler to treat a variable of one type as another type. The compiler would listen and do that for you. The following code would print out gibberish and would compile perfectly without error.
Such flexibility and loose rules for type compatibility could be useful, if you know what you are doing, but for most programmers, it could be a major source of unintentional bugs, especially if one does not pay attention to compiler's warning or one forces the warning to go away without fully understanding what is going on. Java is very strict when it comes to type checking, and is one of the type-safe languages. Java ensures that basic operations (such as
Exercise
Which of the following is true about encapsulation?1. Which among the following best describes encapsulation? Explanation: It is a way of combining both data members and member functions, which operate on those data members, into a single unit. We call it a class in OOP generally.
Which of the following is NOT benefit of encapsulation?Explanation: The encapsulation concept does not add the function in a user-defined structure because if the member functions are involved, then the user cannot call the structures defined by it.
Which of the following is the concept of encapsulation?By definition, encapsulation describes the idea of bundling data and methods that work on that data within one unit, like a class in Java. This concept is also often used to hide the internal representation, or state of an object from the outside. This is called information hiding.
What are the four types of encapsulation?Polymorphism, Encapsulation, Abstraction, and Inheritance are the four basic building elements of OOP.. Member Variable Encapsulation. ... . Function Encapsulation. ... . Class Encapsulation.. |