98-381 Microsoft Introduction to Python – Object-oriented Programming

  1. Introduction to Object-oriented Programming

And reusability. So, in object-oriented programming, we aim to incorporate the advantages of modularity and reusability. Objects are usually instances of classes that are used to interact with one another, to design applications, and to design computer programs.

So the important features of object-oriented programming are that it’s actually a bottom-up approach to programme design. So we have programmes organized around objects grouped into classes. We focus on data with methods that operate upon object data, although we also focus on data with methods that operate upon object data. And the interaction between objects through functions will conclude in the reusability of design through the creation of new classes by adding features to existing classes.

And this is a really, really valuable advantage of an object-oriented approach. So again, Grady Butch is a great researcher. He has defined object-oriented programming as a method of implementation in which programmes are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationships that we will see in the next lessons.

So I think that with an object-oriented cycle, you understand the basic concepts of an object-oriented approach. The most important thing to remember is that everything we will look at in the following lessons—everything related to the object-oriented approach—has the object at its heart. The object is always in the center. So the object and how it is defined, analyzed, and finally implemented are central to the analysis, design, and programming. So I’m looking forward to seeing you in the next lesson to learn more about objects and classes. Thank you very much.

  1. Objects and Classes

Hey guys, welcome back. Now in this lesson we will cover the two most important, fundamental concepts of object-oriented programming. The first one has to do with objects, and the second one with classes. So let’s see some things about objects and classes. Languages that support object-oriented programming actually typically use inheritance for code reuse and extensibility in the form of classes and objects. So classes are actually the definitions for the data format and available procedures for a given type or class of object.

They may also contain data and procedures known as “class methods,” which are classes that contain data, members, and member functions. while objects are the instances of classes. So objects sometimes correspond to things found in the real world. For example, a graphics programme may have objects such as a circle, and a square might have a menu. So an online shopping system might also have objects such as a shopping cart, a customer, and a product. So sometimes objects represent more abstract entities, like an object that represents an open file or an object that provides a service like translating something.

So object-oriented programming is more than just classes and objects. It’s a whole programming paradigm, a whole programming example, and an approach based around objects that contain data fields and methods. So it is essential to understand that using classes to organize a bunch of unrelated methods together is not object orientation. We should first define the classes, then define the objects, and then start analyzing, designing, and coding. So each object is said to be an instance of a particular class. For example, an object with its name field set to “married” might be an instance of a class student. So an object, which is actually a real-world element, is an object-oriented environment that may have either a physical or a conceptual existence. So each object always has an identity that distinguishes it from other objects in the system. a state that determines all of this object’s attributes and characteristics. a behaviour that represents externally visible activities performed by an object in terms of changes in its state.

And actually, you should remember that objects can be bottled according to the needs of the application. So an object may have a physical existence. It could be a customer, a car, or a conceptual existence such as a project or a process. So a class represents a collection of objects having specific characteristics, specific properties, that actually show specific behavior, common behavior. So it gives the blueprint, and it gives the description of the objects that can be created from it. So for the class, for defining what a class is, you should remember that the attributes of the class, the constituents of the class, are a set of attributes for the object that is to be instantiated from the class. So generally speaking, there are different objects in the class that have some differences in the values of the attributes. Attributes are also referred to as class data. So if you are confused about what we mean by classes and objects, We will define classes and objects with specific examples later.

So don’t be frustrated if you don’t understand what an object is or what classes are. Object Oriented Programming in accordance with Junaidle mastery PHP According to Java Object-Oriented Programming, design patterns are more than just classes and objects. It’s a whole programming paradigm based around objects such as data structures that contain data fields and methods. It is essential to understand that using classes to organise a batch of unrelated methods together is not object orientation. We should first define objects, classes, and the links between them. So let’s see some examples. Let us consider a very simple class circle.

So we have a circle. So let’s draw it right here. Okay, it is really bad. I think a five-year-old boy could draw a better circle than this. Okay, let’s assume that this is a circle, actually. So the circle represents the geometrical finger circle in a two-dimensional space. The attributes of this class can be identified as x to denote the x coordinate of the center. So we have the centre here. We have x and y. So x represents the x coordinate of the center, y represents the y coordinate of the center, and a represents the radius of the circle. So I have a right here as well. Okay, let me write it. So some of its operations can be defined as follows: I can have an operation called “find area” and a method called “find scale,” but not “find scale.” With a fine area, I can calculate the area, and with scale, I could increase or decrease the reduction.

  1. Methods

Hey guys, welcome back. Now let’s see some things about methods. So, let’s s start. MethoActually, we should say that its object is said to be an instance of a particular class. For example, consider Mary. Mary might be an instance of a class student.

So procedures in an object-oriented programming language are also known as methods. So a procedure is also a method, and variables are also known in object-oriented programming as fields, members, attributes, or properties. So everything leads to the following terms, to the specific terms. Class variables are the first. Actually, class variables belong to the class as a whole. There is only one copy of each, and they belong to the whole class. We cannot have two class variables that are exactly the same as the variables in the traditional functional languages. If I say that I have one class variable called John, then I cannot have a second variable called John in my program. The second is instance variables or attributes. These are data associated with specific objects. So every object has its own copy of each one, and I can have two instance variables called John. Member variables are the third type. So the first one, the second one, and the third one refer to both the class and instance variables that are defined by a particular class. As defined by a specific class. The fourth is class methods. They are members of the class as a whole and have access to only class variables and procedure Call inputs. The fifth is instance methods.

And instance methods belong to individual objects and have access to instance variables for the specific object for which they are called on inputs and class variables. So the first ones are class variables. They belong to the class as a whole. There is only one copy of each class variable. The second one is an instance variable, an instance attribute. And by instance variable, we mean data that belongs to individual objects. Each object has a duplicate of itself. Member variables are the third type. They refer to both the class and instance variables, and they are defined by a particular class. The fourth type of method is a class method; it belongs to the class as a whole and has access to only class variables and procedure Call inputs. The fifth is instance methods. They belong to individual objects, and they have access to instance variables for the specific object that they are called on. They have specific inputs and specific class variables. So these are the five different methods, five different terms that you should remember for methods, what we mean by saying methods, and we will see in the next lesson how we can use methods. So, I look forward to seeing you in the next lesson.

  1. Encapsulation and Data Hiding

So finally, to sum up, we can say that Encapsulation is the process of binding both attributes and methods together within a class. But through encapsulation, the internal details of the class can be hidden from outside. It permits the elements of the class to be accessed from outside only through the interface provided by the class. When it comes to data hiding, we typically have a class that is designed in such a way that its data can only be accessed by its class methods and is actually insulated from direct outside access. This process of insulating an object is called “data hiding” or “information hiding.” So these are the two very basic object-oriented concepts: encapsulation and data hiding. And I’m looking forward to seeing you in the next lesson, where we will see methods of passing and how this is achieved. So thank you very much, and I look forward to seeing you in the next lesson.

  1. Message Passing

Hey guys, welcome back. Let’s look at what methods passing is. So actually, a method goal is also known as a method pass. It is the responsibility of the object—not an external goal—to select the procedural code to execute in response to a method call. So this feature is known as “dynamic dispatch” as a method of passing and distinguishes an object from actually being an abstract data type or a module that has a fixed static implementation of the operations for all instances. So if the call variability relies on more than the single type of the object on which it is called, for example, at least one other parameter object is involved in the method of choice. One can speak of multiple dispatches. As a result, a method called is also known as a method passing. And actually, it is conceptualised as a method, with the name of the method and its input parameters being passed to the object for the spots. So we have a method call, and we use the method name and the input parameters parameters to call the method. So any application requires a number of objects interacting in a harmonious manner.

So objects in the system may communicate with each other using methods called passing. So suppose we have here Object 1 (a boy) and Object 2 (a girl). And object one sends a message here to object two. If object one wants object two to execute one of its methods, Message passing features are thus message passing between two objects. It’s generally unidirectional. We can either have object one pass a message to object two or object two pass a message to object one. So message passing between two objects is generally unidirectional. Message passing enables all interactions between objects, and message passing essentially involves invoking class methods. Finally, you should remember that objects in different processes can be involved in method passing. So I think that we could now have a clearer view of what the message passing is, how we can call methods, and what this actually means with this photo of a boy and a girl. So I’m looking forward to seeing you in the next lesson, where we will talk more about inheritance.

  1. Inheritance

Hey guys, welcome back. Now we will see perhaps the most important concept in audit-oriented programming that has to do with inheritance. So inheritance objects can contain other objects in their instance variables. And this is known as “object composition.” So, for example, think about an object in the student class that might contain, either directly or indirectly, an object in the school address class. So in addition to its own instance variables like first name and last name, object composition is used to represent something like shared relationships. So every student has a school, and that school has an address. So every student object has access to a place to store a school address object, either directly or at a separate location addressed via pointer. So languages that support classes almost always support inheritance. This allows classes to be arranged in an actual hierarchy that represents a type of relationship. As an example, a classmate may inherit from another classmate. All the data and methods that are available to the parent class also appear in the child class with the same names. For example, a class member could use the method make full name to define variables first and last name.

These will also be available to class students, which might add the variable’s grade and the variable behavior. So this technique all allows easy reuse of the same procedures and data definitions, in addition to potentially mirroring real-world relationships in an intuitive way. So rather than using database tables and programming subroutines, the developer uses objects that he may be more familiar with from their application domain. So in this example right here, we have a parent class called “vehicle,” and then we have two child classes, the wheeled vehicle and a boat. But both are vehicles. And so if I have some methods right here, then the same methods can be used in the child classes. And this is also a parent class for two other child classes: for the car and for a bike. And then I have specific classes, different classes. a car with two doors and a car with four doors, as you can see right here. So the methods defined in a vehicle, a wheeled vehicle, or a car in these three classes—all three classes are parent classes from these two vehicles—can be accessed, called, or used by these two subclasses, as well as the Vase or Vs classes if they are in the vehicles. However, I am unable to use something that was coded in this class from a parent class. So this is not possible. I cannot code something and call it from the parent class to the child class. So inheritance is the mechanism that permits new classes to be created out of existing classes by extending (this is the right keyword) and refining their capabilities. So here you can see another paradigm. Another example is that I have a parent class called Animal. I have several animals for children’s classes here.

And finally, I have specific other child classes, for example, a lion, a hyena, or a man. If it’s an omnivore, it’s exactly the same. So if I would like to extend this program, I can either create some more classes or an apparent class, for example, a class mammal. As a result, a number of classes can be derived, including human, cat, dog, cow, and humans, and all of these cats and dogs share the distinct characteristics of the parent animal, a mammal.

So in addition, it has its own particular characteristics. In Kerbi, it is said that a cow is a mammal; a cow is an animal. So inheritance actually is the mechanism that permitsnew classes to be created out of existingclasses by extending and refining its capabilities.So the existing classes are called the base classes, the base parent classes, or the base super classes, and the new classes are called the derived classes, the child classes, or the subclasses. The subclass can inherit or derive the attributes and methods of the superclass, provided that the superclass allows it.

So besides the fact that the subclass may add its own attributes and methods, it may modify any of the superclass methods. Inheritance defines and is a relationship. So we can say that a carnivore is an animal; a lion is a carnivore; a hyena is a carnivore. So there are specific types of inheritance that we can use. So there are five specific types of inheritance. The first one is a single inheritance, the second one is a multiple inheritance, the third one is a multilevel inheritance, the fourth one is a hierarchical inheritance, and the fifth one is a hybrid inheritance. In single inheritance, a subclass derives from a single superclass.

We have a single superclass, and we have a specific subclass. In the case of multiple inheritance, a subclass derives from more than one superclass. While in multilevel inheritance, a subclass derives from a superclass, which in turn is derived from another class, and so on. So we can have multiple inheritance where a subclass derives from more than one superclass. We have something like this. Okay, I don’t like the arrows. I will do something like this.

So I have a class, another class, and finally, here, a subclass, while here I have a subclass and here I have multilevel inheritance. Finally, I have hierarchical inheritance. Well, a class has a number of subclasses, each of which may have subsequent subclasses, continuing for a number of levels so as to form a tree structure, something like this. This is a hierarchical example, as you can see. And finally, we have the Hebridean inheritance right here, which is a combination of multiple and multilevel inheritances so as to form a lattice structure. So as you can see, I have a lot of different types of inheritance. The most important thing is that I can have inheritance so that I can have a subclass that derives from one or more superclasses. And I can have all of the declared methods and variables coded so that they can be reused in subclasses. This is a very simple concept to grasp and to learn to code in this manner. So I’m looking forward to seeing you in the next lesson.

  1. Polymorphism

Hey guys, welcome back. Now we will see the second-most important concept in object-oriented programming. The second most fundamental concept is polymorphism. So actually, subtyping is a form of polymorphism when the calling code can be agnostic as to whether an object belongs to a parent class or one of its descendants.

So meanwhile, the same operation name among objects in an inheritance hierarchy may behave differently. For example, let’s assume that we have the object of type circle and the object of type square that are derived from a common class called shape. Assume we have a square and a circle, as well as the word “class,” sorry, “shape” here.

So the draw function for each type of shape implements what is necessary to draw itself. While calling code, you can remain indifferent to the particular type of shape that is being drawn. So if I have here a draw function and here a draw function, they will implement something different. The draw function for its type of shape implements what is necessary to draw itself. While calling code, you can remain indifferent to the particular type of shape that is being drawn. So I will have something here that will be called and implemented in a totally different way.

So this is another type of abstraction that simplifies code external to the class hierarchy and enables strong separation of concerns. So polymorphism, which is originally a Greek word, means the ability to make multiple forms in an object-oriented paradigm. In the object-oriented example, polymorphism implies using operations in different ways depending upon the instance they are operating upon. So you should think that polymorphism allows objects with totally different internal structures to have a common external interface. So polymorphism is particularly effective when implementing inheritance.

So if we consider these two classes, Circle and Square, with a method find area, although the name and purpose of the methods in the classes are the same, you should think that the internal implementation will be totally different. The procedure for calculating area is different for each class. When an object of class circle involves its fine terrier method, the operation finds the area of the circle without any conflict with the fine area method of the square class.

Also, as you can see in this diagram, I have a superclass called “animals,” and I also have two subclasses, “subclass cat” and “subclass dog.” And here I have the same name and the same method. But, as you can understand, it will be implemented. It will be coded in a completely different way in the function sound I want the dog to make and the sound of the cat I want now. So I have a method that is defined in a superclass, in the superclass “animal,” because cat is an animal and dog is an animal, but the function “sound,” the method “sound,” is implemented in a totally different way. So I think with this example and this icon, you can understand what a polymorphism is. That means I can have the same method but completely different behavior, code, and implementation. So this is the second-most important factor, the most important term in auditorial programming after inheritance. and I’m looking forward to seeing you in the next lesson.

  1. Generalization and Specification

Hey guys, welcome back. Now we will see two other very basic concepts in optical programming. The first one is the generalization, and the second one is the specification. So what does this mean? What does generalisation mean, and what does specification mean? In the generalisation process, the common characteristics of classes are combined to form a class at a higher level of hierarchy. So think about subclasses that are combined to form a generalised superclass. It represents the truth and is right. Right here is a kind of relationship.

A car, for example, is a type of land vehicle, and a boat is a type of water vehicle. So on the other hand, specification is the reverse process of generalization. Here, the distinguishing features of groups of objects are used to form specialised classes from existing classes. So it can be said that the subclasses are the specialised versions of the super classes.

So, as you can see, okay, I have to erase these lines. As you can see, I have a generalisation where I have some subclasses and am going to create some parent classes, while in the specialisation I create some more subclasses. I repeat, in the generalisation I create more parent classes, while in the specialisation I create more subclasses. So this is a general concept about generalisation and specialisation that is important to know and important to remember when you are creating code as a team in order to define if you’re going to extend your project by generalising some parent classes or by specializing some more subclasses. So I’m looking forward to seeing you in the next lesson, where we will talk about links and association.

  1. Links and Associations

Hey guys, welcome back. Now we will see some things about links and association, and we will see how these two concepts for object-oriented concepts are defined and how they are linked together. So association depicts the relationship between objects of one or more classes. I repeat: association depicts the relationship between objects of one or more classes. While a link can be defined as an instance of an association, So I can have an association that depicts the relationship between concepts and objects of one or more classes. And a link can be defined as an instance of an association. So this means that I can have a specific degree of association.

I am capable of having a unisexual relationship. The first one, as you can see, is whether I can have a binary relationship or a ternary relationship. As a result, this leads to specific cardinality ratios of associations. Let’s understand what we mean right here. The cardinality of a binary association denotes the number of instances that are participating in the association. So there are three types of cardinality ratios: one-to-one, one-to-many, and many-to-many.

So one to one means that we have a single object of class A that is associated with a single object of class B. While I have one too many okay, I will write A and B here. So this means that a single object of class A is associated with a single object of class B. And when I have one too many, this means that a single object of class A is actually associated with many objects of class B. So finally, when I have many too many, I’m sorry. When I have many too many, this means that an object of class A may be associated with many objects of class B and an object of class A may be associated with many objects of class A. So I have what I call “one to one,” “one to many,” or “many to many.”

These kernel durations of associations are actually used in DBMS, databases, and database management systems. DBMS stands for database management system. So it is very important to know if an object is associated with another object in a one-to-one relationship. For example, in one school, one student has one parent, one mother, and one father. It’s one to one. The family has only one kid. But if the family has a lot of kids, this means we have one too many.

So maybe this was not a very good example because a family can have either one or many kids. But for example, one person can have one name. If name is also an object, I have a one-to-one relationship. This is also true if I have one school with many kids, so it’s one too many, and many schools have many classes, so it’s also many too many. However, this is not true because one class can have only one school, so consider examples where I can have many of something or this something can have many of other things. For example, one employee might work at more than one job, so one employee can have two or more employees, and on the other hand, one business can have a lot of employers. So this is something of a good example for many cardinality ratios of associations. So this is, I think, a good example to understand the associations between objects and to start coding and implementing all these theoretical aspects of object-oriented programming. So thank you very much, and I’m looking forward to seeing you in the next lesson.

img