Computer Science
Encapsulation programming
Encapsulation programming is a fundamental concept in object-oriented programming that involves bundling data and methods that operate on that data within a single unit. This unit is known as a class, and it provides a way to hide the implementation details of the class from other parts of the program. Encapsulation helps to improve the security, maintainability, and flexibility of software systems.
Written by Perlego with AI-assistance
Related key terms
1 of 5
8 Key excerpts on "Encapsulation programming"
- eBook - PDF
Computer Programming NQF4 Student's Book
TVET FIRST
- S Sasti D Sasti(Author)
- 2020(Publication Date)
- Troupant(Publisher)
Figure 1.7: Class diagram demonstrating OOP concepts (drawn with MS Visio) 9 Module 1 1.4.1 Encapsulation Definition: Encapsulation Encapsulation is an OOP property that refers to hiding the code and data in a single unit such as a class. Encapsulation is a process of preventing direct access to attributes from outside classes by using methods to control access to the attributes. The implementation of encapsulation creates a concept called information hiding . The data of the class is now protected against direct access from outside the class. Refer to clsAnimal from Figure 1.7 as we look at how to accomplish this. The steps are explained in Table 1.4. Table 1.4: Steps describing how encapsulation is implemented in Figure 1.7 Step 1 Declare the data members as private . This will ensure that they cannot be accessed directly from another class or its object. Remember ‘-’ means private . For example: - Colour: String - Weight: Decimal Step 2 Use public mutator methods to allow other classes or their objects to change the value of the data member. Remember ‘+’ means public . For example: + setColour(ByVal C_Value: String) + setWeight (ByVal W_Value: Decimal) Step 3 Use public accessor methods to allow other classes or their objects to access the value of the data member. For example: + getColour( ): String + getWeight (): Decimal Encapsulation in use VB.Net uses OOP and every control in the toolbox is a class. To use a control, we have to declare an object of that class. When we drag a control from the toolbox onto the form, VB.Net declares programmatically for us an object of that control. We then use the property window to set the properties for the control, such as name, text and font. We could do this in the code window as well. Some of these controls use encapsulation. Table 1.5 demonstrates encapsulation using VB.Net controls. - eBook - ePub
IT Interview Guide for Freshers
Crack your IT interview with confidence
- Sameer S Paradkar(Author)
- 2019(Publication Date)
- BPB Publications(Publisher)
Encapsulation: Data encapsulation is a key OO concept that facilitates binding code and data in a single translation unit called a class and hiding the implementation details from outside world. This prevents un-warranted access to data members and restricts the enduser to leverage the public methods of the class. Encapsulation implements one of the four fundamental OOP concepts, that is, data hiding. An object binds together the properties and methods that manipulate the data, which is safe from outside manipulations. Encapsulation is usually the process of hiding the elements from the outside world of an object that do not contribute to its key features.- Abstraction: Abstraction is the mechanism of hiding unwanted details of an object from the outside world, and exposing just relevant details. The best example of abstraction is the interface. Encapsulation hides the unimportant details of an object from the outside world and abstraction makes just the relevant information of an object available to the outside world.
The following table compares abstraction and encapsulation:Figure 6.7: Abstraction vs EncapsulationComposition and inheritance
What are the types of inheritance?
Inheritance is an OO feature of a class to leverage the properties and methods of another class while adding its own features. It enables you to add fresh features and functionalities to an existing class without changing the existing class. There are four types of inheritance in OOPs:- Single inheritance: This mechanism consists of one derived class and one base class. The following diagram depicts a single inheritance:
Figure 6.8: Single inheritance
- Hierarchical inheritance: This mechanism consists of one base class and multiple derived classes from this base class.
- Multi-level inheritance: This mechanism consists of a class derived from a derived class.
- Multiple-inheritance:
- No longer available |Learn more
Extreme C
Taking you to the limit in Concurrency, OOP, and the most advanced capabilities of C
- Kamran Amini(Author)
- 2019(Publication Date)
- Packt Publishing(Publisher)
C cannot be object-oriented because it is located on the barrier between object orientation and procedural programming. Object orientation is the human understanding of a problem and procedural execution is what a CPU can do. Therefore, we need something to be in this position and make this barrier. Otherwise high-level programs, which are usually written in an object-oriented way, cannot be translated directly into procedural instructions to be fed into the CPU.If you look at high-level programming languages like Java, JavaScript, Python, Ruby, and so on, they have a component or layer within their architecture which bridges between their environment and the actual C library found inside the operating system (the Standard C Library in Unix-like systems and Win32 API in Windows systems). For instance, Java Virtual Machine (JVM ) does that in a Java platform. While not all these environments are necessarily object-oriented (for example JavaScript or Python can be both procedural and object-oriented), they need this layer to translate their high-level logic to low-level procedural instructions.Encapsulation
In the previous sections, we saw that each object has a set of attributes and a set of functionalities attached to it. Here, we are going to talk about putting those attributes and functionalities into an entity called an object. We do this through a process called encapsulation .Encapsulation simply means putting related things together into a capsule that represents an object. It happens first in your mind, and then it should be transferred to the code. The moment that you feel an object needs to have some attributes and functionalities, you are doing encapsulation in your mind; that encapsulation then needs to be transferred to the code level.It is crucial to be able to encapsulate things in a programming language, otherwise keeping related variables together becomes an untenable struggle (we mentioned using naming conventions to accomplish this).An object is made from a set of attributes and a set of functionalities. Both of these should be encapsulated into the object capsule. Let's first talk about attribute encapsulation - eBook - PDF
- Nikolaos Skarmeas(Author)
- 1999(Publication Date)
- ICP(Publisher)
The public part consists of a set of data and of operations that manipulate these data. The implementation of the operations is hidden in the private part of the module. This intentional concealment of data and operations is the basic principle of in-formation hiding or encapsulation, according to which we only reveal the parts of the module that are necessary and we hide the unnecessary details. This way we minimise the interdependencies between different parts of programs which are the modules in our case. However, modules have some disadvantages. For example, we cannot create more than one instance of a module. The object oriented paradigm is a further improvement, since classes cor-respond to the modules and instances of more than one class can be created. It further enhances the module paradigm with the introduction of inheritance and message passing. Object Oriented Programming 5 1.1.2 Re-usability One of the main advantages of using object oriented languages is the re-usability mechanisms they provide. There are two main mechanisms, the cre-ation of classes and instances and secondly inheritance. Classes and Instances The user is given the ability to create classes that correspond to entities in the domain it examines. It is the main abstraction mechanism that object oriented languages provide. The programmer can abstract common properties from objects and group them under the same class definition. Specific objects correspond to instances of these classes parameterised to reflect the object's characteristics. In Fig. 1.1, a class account has been created that describes all the accounts of a bank. It has a set of attributes and methods that implement the behaviour offered by an account. Specific accounts can be created as instances of this class by giving specific values to the name, balance and pin attributes of the class. - Joyce Farrell(Author)
- 2012(Publication Date)
- Cengage Learning EMEA(Publisher)
Information hiding is the concept that other classes should not alter an object ’ s attributes — only the methods of an object ’ s own class should have that privilege. Outside classes should only be allowed to make a request that an attribute be altered; it is then up to the class ’ s methods to determine whether the request is appropriate. When using a door, you usually are unconcerned with the latch or hinge construction, and you don ’ t have access to the interior workings of the knob. You care only about the functionality and the interface, the user-friendly boundary between the user and internal mechanisms of the device. When you turn a door ’ s knob, you are interacting appropriately with the interface. Banging on the knob would be an inappropriate interaction, so the door would not respond. Similarly, the detailed workings of objects you create within object-oriented programs can be hidden from outside programs and modules if necessary, and the methods you write can control how the objects operate. When the details are hidden, programmers can focus on the functionality and the interface, as people do with real-life objects. Information hiding is also called data hiding . In Chapter 6, you learned that encapsulating a method ’ s instructions is known as implementation hiding . All these terms help emphasize the point that objects should be self-contained entities. In summary, understanding object-oriented programming means that you must consider five of its integral components: classes, objects, polymorphism, inheritance, and encapsulation. Defining Classes and Creating Class Diagrams A class is a category of things; an object is a specific instance of a class. A class definition is a set of program statements that list the characteristics of each object and the methods each object can use. A class definition can contain three parts: l Every class has a name. l Most classes contain data, although this is not required.- Alfried Pollmann(Author)
- 2020(Publication Date)
- De Gruyter(Publisher)
32 Chapter 3. Object-Orientation and Logic Programming details. Data abstraction actually covers two separate but closely related concepts: • modularization and • information hiding. Modularization is concerned with the splitting of complex systems into a number of self-contained entities (or modules). All information relating to a particular entity in the system is held within that module. Thus, a module is a self-contained and complete description of a part of the overall structure. In connection with computing, this means that a module will con-tain all the data structures and algorithms required for the implementation of the respective part of the system. The modularization benefits the pro-grammer as there is an obvious place to go to if changes have to be made or problems occur. More fundamentally, modularization supports a particular design approach whereby the programmer splits the problem domain to be implemented into a number of recognizable conceptual modules and forms the essence of object-oriented computing. Information hiding provides abstraction by hiding the implementation details of a module from the user. Using information hiding, the access of an object must be done through a protected interface. This interface normally consists of a number of operations which collectively define the behaviour of an entity. For the user, these internal details such as local procedures and data structures are unvisible. To handle complexity, information hiding enables the user to abstract over a level of detail in the system. Furthermore, the development of more reliable programs is supported by strictly controlling the entry points to a module. Thus, it is not possible to access data struc-tures directly and hence not possible to perform unexpected actions on the data. The only way of accessing data structures is through the operational interface. Behaviour Sharing A further concept of object-orientation is connected with behaviour sharing.- Karen Kemp(Author)
- 2007(Publication Date)
- SAGE Publications, Inc(Publisher)
Thus, any combination is possible, including combining the fun-damental data types and more complex ones. The concept of encapsulation is central to OO. Encapsulation is the concept that objects combine information and behavior in a single packet and that data within the object can be accessed only through messages to the object. This has three consequences: (1) Encapsulation provides a well-defined and strictly enforced interface to an object; (2) it enhances data integrity by screening requested changes in the object’s attributes; and (3) it is possible to change the internal code for the method without affecting the interface (i.e., the message stays the same, while the method initiated may be different). Another powerful principle of OO is polymor-phism. This is the ability of multiple object classes to understand the same message. For example, consider the following object methods that define how area is determined for several different geometries: _method Circle.area() _return pi* .r* .r _endmethod _method Rectangle.area() _return .b*.h _endmethod _method Ellipse.area() _return pi* .a* .b _endmethod O These methods can then be used by a single set of generic code that defines the general method for determining area for any kind of geometry: _method set.area total << 0.0 _for shape_over_self.elements() _loop total << total + shape.area _endloop _return total _endmethod Thus, polymorphism enables the addition of new object classes with minimal change to existing soft-ware code. If we add a new object triangle, no change in the code is required. All that is needed is to add the object to the code and write the appropriate method for the area of a triangle. Some OO languages do not support multiple inheritances and have come up with a solution that enables multiple classes to have the same set of behaviors in common.- eBook - PDF
Java Programming Fundamentals
Problem Solving Through Object Oriented Analysis and Design
- Premchand S. Nair(Author)
- 2008(Publication Date)
- CRC Press(Publisher)
355 C H A P T E R 7 Object-Oriented Software Design In this chapter you learn Object-oriented concepts Encapsulation, information hiding, interface, service, message passing, responsibil-ity, delegation, late binding, inheritance hierarchy, composition, and abstract class Java concepts Subclass, superclass, reference super, access modifiers, final class, abstract method, abstract class, and interface Programming skills Design, create, execute, and test Java programs having many classes related through superclass/subclass relationship or composition Reliable, error-free, maintainable, and flexible software is very diffi cult to produce. Today’s software systems are quite complex and no level of abstraction can eliminate the complexity completely. However, certain abstractions are more natural to human thinking compared to other forms of abstractions. In the case of object-oriented paradigm, real-world entities are modeled as objects. This form of abstraction enables the software developer to divide the software into a collection of mutually collaborating objects working toward achieving a common goal of solving the problem. In this chapter, you will explore object-oriented para-digm in a more comprehensive fashion. Further, examples and analogies presented in this chapter will help you understand the object-oriented way of developing Java programs. OBJECTS The most fundamental concept of the object-oriented paradigm is that of an object. An object can be perceived in three different ways. In fact, these are not contradictory views; rather, they complement each other to enhance our ability to model the real world. • • • • • • 356 ■ Java Programming Fundamentals Three perspectives of an object are Data-centric view Client–server view Software design view Data-Centric View From a data-centric perspective, an object is a collection of attributes and operations that manipulate the data.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.







