1 / 10

Data Encapsulation and Relationships in Object-Oriented Programming

Learn about the concept of encapsulation, inheritance, association, aggregation, composition, and the const keyword in object-oriented programming.

enidd
Download Presentation

Data Encapsulation and Relationships in Object-Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LECTURE 22 Aggregation and Const Keyword

  2. Encapsulation • Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.

  3. Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface. • The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

  4. Inheritance If a class B is derived by inheritance from a class A, we can say that “B is a kind of A.” This is because B has all the characteristics of A, and in addition some of its own. It’s like saying that a starling is a kind of bird: A starling has the characteristics shared by all birds (wings, feathers,and so on) but has some distinctive characteristics of its own (such as dark iridescent plumage). For this reason inheritance is often called a “kind of” relationship.

  5. Association • Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

  6. Aggregation • Aggregation is a specialize form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let’s take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.

  7. Aggregation is called a “has a” relationship. We say a library has a book or an invoice has an item line.Aggregation is also called a “part-whole” relationship: the book is part of the library. In object-oriented programming, aggregation may occur when one object is an attribute of another. Here’s a case where an object of class A is an attribute of class B:

  8. class A { }; class B { A objA; // define objA as an object of class A };

  9. Composition • Composition is again specialize form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.

  10. Const Keyword • it is simple in concept: variables declared with ‘const’ added become constants and cannot be altered by the program.

More Related