Message relationship in oop

Has-a - Wikipedia

message relationship in oop

This is the main relation in object-oriented programming. . Testing for equality between instances of these classes produces a long error message containing. The OO concept of the Is-a relationship is called specialization. In C++ specialization is implemented via inheritance and messages are implemented via. B Level DOEACC Module 3 OO Data Model- OO Relationships. run-time dispatch of messages to objects related in an inheritance hierarchy.

Then whenever we want to perform this action we call the method on the object.

We consider it bad practice to retrieve the information from inside the object and write separate code to perform the action outside of the object. Encapsulation is a good idea for several reasons: We could switch to using another object which is completely different on the inside, and not have to change any code because both objects have the same interface. Some languages have features which allow us to enforce encapsulation strictly. In Java it is also considered good practice to write setters and getters for all attributes, even if the getter simply retrieves the attribute and the setter just assigns it the value of the parameter which you pass in.

It is also customary to set and get simple attribute values directly, and only write setter and getter methods for values which require some kind of calculation. In Python, there are two main types of relationships between classes: We saw in the previous chapter how we can make a datetime.

We can say that a person has a birthdate — if we can express a relationship between two classes using the phrase has-a, it is a composition relationship.

Relationships like this can be one-to-one, one-to-many or many-to-many, and they can be unidirectional or bidirectional, depending on the specifics of the the roles which the objects fulfil.

008 Is A Relationship in OOP

According to some formal definitions the term composition implies that the two objects are quite strongly linked — one object can be thought of as belonging exclusively to the other object. If the owner object ceases to exist, the owned object will probably cease to exist as well. If the link between two objects is weaker, and neither object has exclusive ownership of the other, it can also be called aggregation. Here are four classes which show several examples of aggregation and composition: This is an example of the way that translation of real-life concepts into objects in your code may not always be as straightforward as it appears.

Would it have made sense to have a single course object which has both description, code and department attributes and a list of students? We have represented these two concepts by two separate classes which are linked to each other. Course is the theoretical description of a course, and CourseRunning is the concrete instance of a course.

We have defined several relationships between these classes: A student can be enrolled in several courses CourseRunning objectsand a course CourseRunning can have multiple students enrolled in it in a particular year, so this is a many-to-many relationship. A student knows about all his or her courses, and a course has a record of all enrolled students, so this is a bidirectional relationship.

A department offers multiple courses Course objectsbut in our implementation a course can only have a single department — this is a one-to-many relationship.

It is also bidirectional. Furthermore, these objects are more strongly coupled — you can say that a department owns a course. The course cannot exist without the department. You should include classes for songs, artists, albums and playlists. You can also assume that each song is associated with a single album, but that multiple copies of the same song which are included in different albums can exist.

Write a simple implementation of this model which clearly shows how the different classes are composed. Write some example code to show how you would use your classes to create an album and add all its songs to a playlist. An object which inherits from another object is considered to be a subtype of that object.

Object-Oriented Terminology

As we saw in the previous chapter, all objects in Python inherit from object. We can say that a string, an integer or a Person instance is an object instance. When we can describe the relationship between two objects using the phrase is-a, that relationship is inheritance.

We also often say that a class is a subclass or child class of a class from which it inherits, or that the other class is its superclass or parent class.

We can refer to the most generic class at the base of a hierarchy as a base class. Inheritance can help us to represent objects which have some differences and some similarities in the way they work.

Object-Oriented Terminology

We can put all the functionality that the objects have in common in a base class, and then define one or more subclasses with their own custom functionality. Inheritance is also a way of reusing existing code easily. If we already have a class which does almost what we want, we can create a subclass in which we partially override some of its behaviour, or perhaps add some new functionality.

Here is a simple example of inheritance: We create a subclass to represent students and one to represent staff members, and then a subclass of StaffMember for people who teach courses as opposed to staff members who have administrative positions. We represent both student numbers and staff numbers by a single attribute, number, which we define in the base class, because it makes sense for us to treat them as a unified form of identification for any person.

We use different attributes for the kind of student undergraduate or postgraduate that someone is and whether a staff member is a permanent or a temporary employee, because these are different sets of options.

We have also added a method to Student for enrolling a student in a course, and a method to Lecturer for assigning a course to be taught by a lecturer.

Because we use the class of an exception to determine whether it should be caught by a particular except block, it is useful for us to define custom classes for exceptions which we want to raise in our code. Using inheritance in our classes is useful because if an except block catches a particular exception class, it will also catch its child classes because a child class is its parent class.

That means that we can efficiently write except blocks which handle groups of related exceptions, just by arranging them in a logical hierarchy.

message relationship in oop

Write a simple program which loops over a list of user data tuples containing a username, email and age and adds each user to a directory if the user is at least 16 years old. You do not need to store the age. Write a simple exception hierarchy which defines a different exception for each of these error conditions: Whenever an exception occurs, your program should move onto the next set of data in the list. Print a different error message for each different kind of exception.

Think about where else it would be a good idea to use a custom class, and what kind of collection type would be most appropriate for your directory. You can assume that the age is already an integer value. At a real university, the divisions between staff and students and administrative and teaching staff are not always clear-cut.

Lecture Notes on Object-Oriented Programming

A student who tutors a course is also a kind of temporary staff member. A staff member can enrol in a course.

message relationship in oop

One benefit of this approach is that there can be many methods named "add", with different objects implementing them in different ways. This allows programmers to reuse names of methods, allowing the same name to have different meanings in different contexts.

Polymorphism and Overloading Polymorphism refers to the capability of having methods with the same names and parameter types exhibit different behavior depending on the receiver. In other words, you can send the same message to two different objects and they can respond in different ways. More generally, the capability of using names to mean different things in different contexts is called overloading.

This also includes allowing two methods to have the same name but different parameters types, with different behavior depending on the parameter types. Note that a language could support some kinds of overloading without supporting polymorphism.

In that case, most people in the object-oriented community would not consider it to be an object-oriented language. Polymorphism and overloading can lead to confusion if used excessively. However, the capability of using words or names to mean different things in different contexts is an important part of the power of natural languages.

message relationship in oop

People begin developing the skills for using it in early childhood. Members Objects can have their own data, including variables and constants, and their own methods. The variables, constants, and methods associated with an object are collectively refered to as its members or features.

Classes Many object-oriented languages use an important construction called a class. A class is a category of objects, classified according to the members that they have. Like objects, classes can also be implemented in classical languages, using separate compilation and structs for encapsulation.

See Implementing a Class in C for a brief explanation and example.

message relationship in oop

The object-oriented language Java uses the following syntax for class definitions: Class Members and Instance Members In many object-oriented languages, classes are objects in their own right to a greater or lesser extent, depending on the language. Their primary function is as factories for objects in the category.

A class can also hold data variable and constants that are shared by all of its objects and can handle methods that deal with an entire class rather than an individual object. The members that are associated with objects are called instance members. Inheritance One important characteristic of object-oriented languages is inheritance. Inheritance refers to the capability of defining a new class of objects that inherits from a parent class. New data elements and methods can be added to the new class, but the data elements and methods of the parent class are available for objects in the new class without rewriting their declarations.

For example, Java uses the following syntax for inheritance: