From 3d68dcccc97861c9419bdf63b0a225fd02db9246 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Tue, 22 Feb 2022 22:36:32 +0530 Subject: [PATCH 01/26] Update config.json --- concepts/class-inheritance/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/class-inheritance/.meta/config.json b/concepts/class-inheritance/.meta/config.json index 9b9e8da5a9..cbd72f6a05 100644 --- a/concepts/class-inheritance/.meta/config.json +++ b/concepts/class-inheritance/.meta/config.json @@ -1,5 +1,5 @@ { - "blurb": "TODO: add blurb for this concept", + "blurb": "Inheritance is the capability of one class to derive or inherit the properties from another class.", "authors": ["bethanyg", "cmccandless"], "contributors": [] } From c73856a0888404fe9630e65a967e93dc050ba472 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Wed, 23 Feb 2022 11:36:13 +0530 Subject: [PATCH 02/26] Update config.json --- concepts/class-inheritance/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/class-inheritance/.meta/config.json b/concepts/class-inheritance/.meta/config.json index cbd72f6a05..447917fd94 100644 --- a/concepts/class-inheritance/.meta/config.json +++ b/concepts/class-inheritance/.meta/config.json @@ -1,5 +1,5 @@ { "blurb": "Inheritance is the capability of one class to derive or inherit the properties from another class.", "authors": ["bethanyg", "cmccandless"], - "contributors": [] + "contributors": ["girijakar"] } From 2d65f174acb751646444ef090be3229b43bc4d68 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Wed, 23 Feb 2022 12:06:42 +0530 Subject: [PATCH 03/26] Update config.json --- concepts/class-inheritance/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/class-inheritance/.meta/config.json b/concepts/class-inheritance/.meta/config.json index 447917fd94..e42a8c57c6 100644 --- a/concepts/class-inheritance/.meta/config.json +++ b/concepts/class-inheritance/.meta/config.json @@ -1,5 +1,5 @@ { - "blurb": "Inheritance is the capability of one class to derive or inherit the properties from another class.", + "blurb": "Inheritance is the capability of one class to derive or inherit the properties from another class. The existing class is called base class and the new class is called the derived class.", "authors": ["bethanyg", "cmccandless"], "contributors": ["girijakar"] } From 38b2631b6cfbce5bbd0138ed0e516333f22b09c2 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Wed, 23 Feb 2022 12:12:33 +0530 Subject: [PATCH 04/26] Update about.md --- concepts/class-inheritance/about.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index c628150d56..4f808a9543 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -1,2 +1,2 @@ -#TODO: Add about for this concept. +# About From 4c11958408b3d784a11f71575ed31bf5a014db24 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Wed, 23 Feb 2022 20:33:26 +0530 Subject: [PATCH 05/26] Update config.json --- concepts/class-inheritance/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/class-inheritance/.meta/config.json b/concepts/class-inheritance/.meta/config.json index e42a8c57c6..6638de48b3 100644 --- a/concepts/class-inheritance/.meta/config.json +++ b/concepts/class-inheritance/.meta/config.json @@ -1,5 +1,5 @@ { - "blurb": "Inheritance is the capability of one class to derive or inherit the properties from another class. The existing class is called base class and the new class is called the derived class.", + "blurb": "Inheritance refers to a class's capacity to derive or inherit properties from another class. The existing class is known as the base class, and the new class is known as the derived class.", "authors": ["bethanyg", "cmccandless"], "contributors": ["girijakar"] } From f3c8bee7cb4d7ff4ae04acec89feb165c286d285 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Wed, 23 Feb 2022 21:10:11 +0530 Subject: [PATCH 06/26] Update about.md --- concepts/class-inheritance/about.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index 4f808a9543..31e8f6c693 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -1,2 +1,8 @@ # About +A `Class` is a template/blueprint representing a group of objects that share common properties and relationships. +Inheritance is one of the Object Oriented Programming(OOP) concept. Understanding inheritance is important to +understanding the whole point behind object oriented programming. + +## Single Inheritance + From c272ced28ccde35ece24894eb148edd2f6f598c1 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Sat, 26 Feb 2022 21:39:35 +0530 Subject: [PATCH 07/26] Update config.json --- concepts/class-inheritance/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/class-inheritance/.meta/config.json b/concepts/class-inheritance/.meta/config.json index 6638de48b3..3012b85ab9 100644 --- a/concepts/class-inheritance/.meta/config.json +++ b/concepts/class-inheritance/.meta/config.json @@ -1,5 +1,5 @@ { - "blurb": "Inheritance refers to a class's capacity to derive or inherit properties from another class. The existing class is known as the base class, and the new class is known as the derived class.", + "blurb": "Inheritance refers to a class's capacity to derive or inherit properties from another class. The existing class is known as the base class, and the new class is known as the derived class. The object oriented languages express this inheritance relationship by allowing one class to inherit from another.", "authors": ["bethanyg", "cmccandless"], "contributors": ["girijakar"] } From 54701e4b4b75f56d2eace948b7f990b409484d9f Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Sat, 26 Feb 2022 22:31:14 +0530 Subject: [PATCH 08/26] Update about.md --- concepts/class-inheritance/about.md | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index 31e8f6c693..686926c891 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -4,5 +4,37 @@ A `Class` is a template/blueprint representing a group of objects that share com Inheritance is one of the Object Oriented Programming(OOP) concept. Understanding inheritance is important to understanding the whole point behind object oriented programming. +## More on Classes + +### Access Modifiers + +### Class Functions + + ## Single Inheritance +When a subclass inherits only from one base class, it is known as single inheritance. + +```python +#Base class +class Odin: + def func1(self): + print("Odin: father of thor") + +#Derived/Subclass class +class Thor(Odin): + def func2(self): + print("Thor: son of Odin") + + +object = Thor() +object.func1() +object.func2() +``` +The derived class will print both the statements of `func1` and `func2`. + +## Multiple Inheritance + + + + From 6fd43eeac4e48d99ec43526417c2bb15e48e72e5 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Sun, 27 Feb 2022 20:45:50 +0530 Subject: [PATCH 09/26] Update about.md --- concepts/class-inheritance/about.md | 168 ++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index 686926c891..0e11f71ffb 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -7,9 +7,168 @@ understanding the whole point behind object oriented programming. ## More on Classes ### Access Modifiers +Access modifications are used to restrict access to the variables and methods of the class in various object-oriented languages such as C++, Java, and Python. In most programming languages, there are three types of access modifiers in a class: `Public`, `Protected`, and `Private`. +The access control for a specific data member or a member function of a class is determined by the `_` symbol in Python. In Python, access specifiers serve a crucial role in protecting data from unwanted access and preventing it from being misused. + +Python has three types of access modifiers: +1. Public access modifier +1. Protected access modifier +1. Private access modifier + +#### 1. Public Access Modifier: +The members of a class that are declared public are easily accessible from any part of the program. All data members and member functions of a class are public by default. +```python +class School: + #constructor + def __init__(self, name, location): + #public data members + self.school_name = name + self.school_loc = location + + # public member function + def school_details(self): + print("Location:", self.school_loc) + +obj = School("Harvard", "USA") +#Accessing public data members outside the class +print("Name:", obj.school_name) +obj.school_details() +``` +In the above program, the `school_name`, `school location` and `school_details()` are public data members. These data members can be accessed from anywhere in the program. They also can be inherited. + +#### 2. Protected Access Modifier: +Protected members of a class can only be accessed by a class that is derived from it. A single underscore `_` symbol is added before the data member of a class to declare it protected. +```python +# parent class +class Student: + + # protected data members + _name = None + _roll = None + _branch = None + + # constructor + def __init__(self, name, roll, branch): + self._name = name + self._roll = roll + self._branch = branch + + # protected member function + def _displayRollAndBranch(self): + + # accessing protected data members + print("Roll: ", self._roll) + print("Branch: ", self._branch) + + +# derived class +class About(Student): + + # constructor + def __init__(self, name, roll, branch): + Student.__init__(self, name, roll, branch) + + # public member function + def displayDetails(self): + + # accessing protected data members of super class + print("Name: ", self._name) + + # accessing protected member functions of super class + self._displayRollAndBranch() + +# creating objects of the derived class +obj = Student("ABC", 122333, "Physics") + +# calling public member functions of the class +obj.displayDetails() +``` +`_name`, `_roll`, and `_branch` are protected data members in the preceding application, while the `_displayRollAndBranch()` function is a protected method of the super class `Student`. The `displayDetails()` method is a public member function of the `About` class, which is derived from the Student class. The `displayDetails()` method in the `About` class accesses the `Student` class's protected data members. + +#### 3. Private Access Modifier: +The members of a class that are declared private are accessible within the class only. Private data members cannot be inherited by the derive class. A double underscore `__` sign is added before the data member of a class to make it private. + +```python +class Student: + + # private members + __name = None + __roll = None + __division = None + + # constructor + def __init__(self, name, roll, division): + self.__name = name + self.__roll = roll + self.__division = division + + # private member function + def __displayDetails(self): + + # accessing private data members + print("Name: ", self.__name) + print("Roll: ", self.__roll) + print("Branch: ", self.__division) + + # public member function + def accessPrivateFunction(self): + + # accessing private member function + self.__displayDetails() + +# creating object +obj = Student("ABC", 122333, "Physics") + +# accessing public member function of the class +obj.accessPrivateFunction() +``` +`__name`, `__roll`, and `__division` are private members of the class `Student`, while the `__displayDetails()` method is a private member function (accessible only within the class), and the `accessPrivateFunction()` method is a public member function of the class `Student` that can be accessed from anywhere within the programme. The `accessPrivateFunction()` method gives you access to the `Student` class's private members. ### Class Functions +The member functions of a class can be categorized into following two categories: + +__1. Accessor Functions__: These are those number functions that allow us to access data fields of the object. However, these functions cannot/do not change the value of the data members. + +```python +class Student: + # private members + __name = None + __roll = None + __marks = None + __div = None + + # constructor + def __init__(self, name, roll, marks, div): + self.__name = name + self.__roll = roll + self.__marks = marks + self.__div = div + + # public member function + def displayDetails(self): + # accessing private data members + print("Name: ", self.__name) + print("Roll: ", self.__roll) + print("Marks:", self.__marks) + print("Division: ", self.__div) + + # Accessor Function + def getRollNo(self): + return self.__roll + + # Mutator Function + def setDiv(self): + if self.__marks>=60: + self.__div = 'A' + elif self.__marks<60: + self.__div = 'B' + return self.__div +``` +__2. Mutator Function__: This method is used to change the state of an object, i.e. it changes the data variable's hidden value. It has the ability to change the value of a variable on the fly. The update method is another name for this function. We can also use the word _set_ to name these procedures. +As per the above example, `getRollNo()` is an accessor function and `setDiv` is a mutator function. + +## Inheritance ## Single Inheritance @@ -35,6 +194,15 @@ The derived class will print both the statements of `func1` and `func2`. ## Multiple Inheritance +### Diamond Inheritance + +### Method resolution order(MRO) + +### Mixins + +#### Pros and Cons of Mixins + +## __super()__ From fe2ccb73a1624ad41d8bdf0ed157c3970c0f6541 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Sun, 27 Feb 2022 21:01:57 +0530 Subject: [PATCH 10/26] Update about.md --- concepts/class-inheritance/about.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index 0e11f71ffb..ffd520a414 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -193,7 +193,11 @@ object.func2() The derived class will print both the statements of `func1` and `func2`. ## Multiple Inheritance - +As we've seen, a class inherits from another class in this scenario. On the other side, multiple inheritance is a feature that allows a class to inherit characteristics and methods from many parent classes. +```python +class SubclassName(BaseClass1, BaseClass2, BaseClass3, ...): + pass +``` ### Diamond Inheritance ### Method resolution order(MRO) From 1244a82aaeee5b83b9a7b7a156d65153add2c5ef Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Sun, 27 Feb 2022 22:28:14 +0530 Subject: [PATCH 11/26] Update about.md --- concepts/class-inheritance/about.md | 87 +++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 4 deletions(-) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index ffd520a414..968d7d1707 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -123,7 +123,7 @@ obj = Student("ABC", 122333, "Physics") # accessing public member function of the class obj.accessPrivateFunction() ``` -`__name`, `__roll`, and `__division` are private members of the class `Student`, while the `__displayDetails()` method is a private member function (accessible only within the class), and the `accessPrivateFunction()` method is a public member function of the class `Student` that can be accessed from anywhere within the programme. The `accessPrivateFunction()` method gives you access to the `Student` class's private members. +`__name`, `__roll`, and `__division` are private members of the class `Student`, while the `__displayDetails()` method is a private member function (accessible only within the class), and the `accessPrivateFunction()` method is a public member function of the class `Student` that can be accessed from anywhere within the programme. The `accessPrivateFunction()` method gives us access to the `Student` class's private members. ### Class Functions The member functions of a class can be categorized into following two categories: @@ -195,18 +195,97 @@ The derived class will print both the statements of `func1` and `func2`. ## Multiple Inheritance As we've seen, a class inherits from another class in this scenario. On the other side, multiple inheritance is a feature that allows a class to inherit characteristics and methods from many parent classes. ```python -class SubclassName(BaseClass1, BaseClass2, BaseClass3, ...): +class SubclassName(BaseClass1, BaseClass2, ...): pass ``` -### Diamond Inheritance +### Diamond Problem +The "diamond problem" (also known as the "deadly diamond of death") refers to an ambiguity that occurs when two classes B and C inherit from a superclass A, while another class D inherits from both B and C. If A has a method "m" that B or C (or even both of them) has overridden, and if it does not override this method, the question becomes which version of the method D inherits. It's possible that it's from A, B, or C. +Let's have a look at the problem using an example: +```python +class A: + def m(self): + print("m of A called") +class B(A): + def m(self): + print("m of B called") +class C(A): + def m(self): + print("m of C called") +class D(B,C): + pass +``` +If we call an instance x of class D, we will get the output as `m of B called`. But if we interchange the order of inheritance in class D i.e. `Class D(C, D)`. We will get the output as `m of C called`. +To solve the diamond problem in python, we will look into a new method `mro()`. ### Method resolution order(MRO) +To get the method resolution order of a class we can use either `__mro__` attribute or `mro()` method. By using these methods we can display the order in which methods are resolved. For Example + +```python +class A: + def m(self): + print(" m of A called") +class B: + def m(self): + print(" m of B called") + +# classes ordering +class C(A, B): + def __init__(self): + print("Constructor C") + +r = C() + +# it prints the lookup order +print(C.__mro__) +print(C.mro()) +``` +The output +```cmd +Constructor C +(, , , ) +[, , , ] +``` ### Mixins +A mixin is a type of multiple inheritance that is unique. Mixins are typically employed in one of two scenarios: -#### Pros and Cons of Mixins +1. We wish to give a class a number of optional features. +1. We want to use a specific feature in a variety of classes. +For example +```python +class A1(object): + def method(self): + return 1 + +class A2(object): + def method(self): + return 2 + +class B1(object): + def usesMethod(self): + return self.method() + 10 + +class B2(object): + def usesMethod(self): + return self.method() + 20 + +class C1_10(A1, B1): pass +class C1_20(A1, B2): pass +class C2_10(A2, B1): pass +class C2_20(A2, B2): pass +``` +Mixins helps us to recombine functionalities with different choices of base classes. +#### Pros and Cons of Mixins +| Advantages | Disadvantages | +|:-- | :-- | +|Mixin classes tend to be simple because they represent simple orthogonal concepts. | Execution of statements at run time tends to jump around in different mixins, making it hard to follow and debug| +|Helps us to recombine functionalities with different choices | Potential for long compile times| ## __super()__ +In a nutshell, `super()` gives us access to methods in a superclass from the subclass that inherits from it. +`super()` by itself returns a temporary object of the superclass, which may subsequently be used to call the methods of that superclass. +But why we want to use `super()`? +Using `super()` to call already created methods avoids having to rebuild those methods in our subclass and allows us to swap out superclasses with little code modifications. From a493c91f6ee3e329a6ede0d46dce341d5f0c3a12 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Sun, 27 Feb 2022 23:26:29 +0530 Subject: [PATCH 12/26] Update introduction.md --- concepts/class-inheritance/introduction.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/concepts/class-inheritance/introduction.md b/concepts/class-inheritance/introduction.md index fcde74642c..6ad4d23dae 100644 --- a/concepts/class-inheritance/introduction.md +++ b/concepts/class-inheritance/introduction.md @@ -1,2 +1,6 @@ -#TODO: Add introduction for this concept. +# Introduction +[`Inheritance`](inherit) represents what is known as a relationship. When a Derived class inherits from a Base class, you've established a relationship in which Derived is a specialised version of Base. +Either by using single or multiple inheritance, we can inherit the properties from the base class. Inheritance is used because it helps in code reusability. + +[inherit]:https://realpython.com/inheritance-composition-python/#whats-inheritance From a05b2b91b45ccaac24aaefbf521ecfcb35cbf1b3 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Sun, 27 Feb 2022 23:27:52 +0530 Subject: [PATCH 13/26] Update introduction.md --- concepts/class-inheritance/introduction.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/concepts/class-inheritance/introduction.md b/concepts/class-inheritance/introduction.md index 6ad4d23dae..6c8d2762fc 100644 --- a/concepts/class-inheritance/introduction.md +++ b/concepts/class-inheritance/introduction.md @@ -1,6 +1,7 @@ # Introduction -[`Inheritance`](inherit) represents what is known as a relationship. When a Derived class inherits from a Base class, you've established a relationship in which Derived is a specialised version of Base. +[Inheritance](inherit) represents what is known as a relationship. When a Derived class inherits from a Base class, you've established a relationship in which Derived is a specialised version of Base. Either by using single or multiple inheritance, we can inherit the properties from the base class. Inheritance is used because it helps in code reusability. -[inherit]:https://realpython.com/inheritance-composition-python/#whats-inheritance + +[inherit]: https://realpython.com/inheritance-composition-python/#whats-inheritance From 5e88b46b22e6924f5aab0a2a7fba35b0a4032f03 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Sun, 27 Feb 2022 23:28:21 +0530 Subject: [PATCH 14/26] Update introduction.md --- concepts/class-inheritance/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/class-inheritance/introduction.md b/concepts/class-inheritance/introduction.md index 6c8d2762fc..fb1cfff6e4 100644 --- a/concepts/class-inheritance/introduction.md +++ b/concepts/class-inheritance/introduction.md @@ -4,4 +4,4 @@ Either by using single or multiple inheritance, we can inherit the properties from the base class. Inheritance is used because it helps in code reusability. -[inherit]: https://realpython.com/inheritance-composition-python/#whats-inheritance +[inherit]:https://realpython.com/inheritance-composition-python/#whats-inheritance From 0339372f7a9dd97280b71502aab843b7d895e84d Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Sun, 27 Feb 2022 23:36:19 +0530 Subject: [PATCH 15/26] Update links.json --- concepts/class-inheritance/links.json | 40 +++++++++++++++++++++------ 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/concepts/class-inheritance/links.json b/concepts/class-inheritance/links.json index eb5fb7c38a..034b9b3935 100644 --- a/concepts/class-inheritance/links.json +++ b/concepts/class-inheritance/links.json @@ -1,18 +1,42 @@ [ { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/tutorial/classes.html", + "description": "Classes in python" }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://www.geeksforgeeks.org/access-modifiers-in-python-public-private-and-protected/", + "description": "Access Modifiers in python" }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://www.geeksforgeeks.org/accessor-and-mutator-methods-in-python/", + "description": "Functions in classes" }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://realpython.com/inheritance-composition-python/#whats-inheritance", + "description": "About inheritance" + }, + { + "url": "https://python-course.eu/oop/multiple-inheritance.php", + "description": "Multiple inheritance and Diamond inheritance problem" + }, + { + "url": "https://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful", + "description": "Python mixins" + }, + { + "url": "https://raganwald.com/2016/07/16/why-are-mixins-considered-harmful.html", + "description": "Mixins cons" + }, + { + "url": "https://gaopinghuang0.github.io/2018/12/29/dig-into-python-super-and-mro", + "description": "Super and mixins functions" + }, + { + "url": "https://www.python.org/download/releases/2.3/mro/", + "description": "MRO" + }, + { + "url": "https://nedbatchelder.com/blog/201210/multiple_inheritance_is_hard.html", + "description": "Multiple inheritancs and mixins" } ] From e0190c791fe338491e84dd52ebdea283ef1f76f6 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Mon, 14 Mar 2022 22:48:02 +0530 Subject: [PATCH 16/26] Update concepts/class-inheritance/.meta/config.json Co-authored-by: BethanyG --- concepts/class-inheritance/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/class-inheritance/.meta/config.json b/concepts/class-inheritance/.meta/config.json index 3012b85ab9..6a6e86f3eb 100644 --- a/concepts/class-inheritance/.meta/config.json +++ b/concepts/class-inheritance/.meta/config.json @@ -1,5 +1,5 @@ { - "blurb": "Inheritance refers to a class's capacity to derive or inherit properties from another class. The existing class is known as the base class, and the new class is known as the derived class. The object oriented languages express this inheritance relationship by allowing one class to inherit from another.", + "blurb": "In Object Oriented Programming ('OOP'), inheritance refers to a class's capacity to copy or 'inherit' properties and methods from another class. Python allows inheritance from a single class or multiple classes. Existing classes are known as base (or parent) classes, and new classes are known as derived (or child) classes.", "authors": ["bethanyg", "cmccandless"], "contributors": ["girijakar"] } From 87cbb76f337f09b8caf1745b03f6862528ffaa3c Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Mon, 14 Mar 2022 22:48:17 +0530 Subject: [PATCH 17/26] Update concepts/class-inheritance/.meta/config.json Co-authored-by: BethanyG --- concepts/class-inheritance/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/class-inheritance/.meta/config.json b/concepts/class-inheritance/.meta/config.json index 6a6e86f3eb..f1414d2978 100644 --- a/concepts/class-inheritance/.meta/config.json +++ b/concepts/class-inheritance/.meta/config.json @@ -1,5 +1,5 @@ { "blurb": "In Object Oriented Programming ('OOP'), inheritance refers to a class's capacity to copy or 'inherit' properties and methods from another class. Python allows inheritance from a single class or multiple classes. Existing classes are known as base (or parent) classes, and new classes are known as derived (or child) classes.", - "authors": ["bethanyg", "cmccandless"], + "authors": ["girijakar", "bethanyg"], "contributors": ["girijakar"] } From d9071685cfe92750486619aea3dce61df954c4e9 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Mon, 14 Mar 2022 22:48:32 +0530 Subject: [PATCH 18/26] Update concepts/class-inheritance/.meta/config.json Co-authored-by: BethanyG --- concepts/class-inheritance/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/class-inheritance/.meta/config.json b/concepts/class-inheritance/.meta/config.json index f1414d2978..f6bf6d7d87 100644 --- a/concepts/class-inheritance/.meta/config.json +++ b/concepts/class-inheritance/.meta/config.json @@ -1,5 +1,5 @@ { "blurb": "In Object Oriented Programming ('OOP'), inheritance refers to a class's capacity to copy or 'inherit' properties and methods from another class. Python allows inheritance from a single class or multiple classes. Existing classes are known as base (or parent) classes, and new classes are known as derived (or child) classes.", "authors": ["girijakar", "bethanyg"], - "contributors": ["girijakar"] + "contributors": [ ] } From c3c2edc5ef38eb7fc914931391025851cac0efe3 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Mon, 14 Mar 2022 22:49:05 +0530 Subject: [PATCH 19/26] Update concepts/class-inheritance/about.md Co-authored-by: BethanyG --- concepts/class-inheritance/about.md | 1 - 1 file changed, 1 deletion(-) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index 968d7d1707..300df22c37 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -1,6 +1,5 @@ # About -A `Class` is a template/blueprint representing a group of objects that share common properties and relationships. Inheritance is one of the Object Oriented Programming(OOP) concept. Understanding inheritance is important to understanding the whole point behind object oriented programming. From 55a31638b18d1fceda2c3f2b3a6282b36ca51e91 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Mon, 14 Mar 2022 22:49:45 +0530 Subject: [PATCH 20/26] Update concepts/class-inheritance/about.md Co-authored-by: BethanyG --- concepts/class-inheritance/about.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index 300df22c37..71c8a29631 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -1,7 +1,7 @@ # About -Inheritance is one of the Object Oriented Programming(OOP) concept. Understanding inheritance is important to -understanding the whole point behind object oriented programming. +Inheritance is one of the ['four pillars'][four-pillars] of Object Oriented Programming (`OOP`). +In situations where only a small amount of functionality needs to be customized for a new class, `inheritance` allows code re-use from one or more parent classes, and can help make programs cleaner and more maintainable. ## More on Classes From 0bb7ca69d97feb521d81c960cb584c569cab112e Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Mon, 14 Mar 2022 22:50:13 +0530 Subject: [PATCH 21/26] Update concepts/class-inheritance/about.md Co-authored-by: BethanyG --- concepts/class-inheritance/about.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index 71c8a29631..a717c2238d 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -171,7 +171,7 @@ As per the above example, `getRollNo()` is an accessor function and `setDiv` is ## Single Inheritance -When a subclass inherits only from one base class, it is known as single inheritance. +When a derived (or child) class inherits only from one base (or parent) class, it is known as single inheritance. ```python #Base class From 18321f54031fbb71ad8f586e8c98db86916659c6 Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Mon, 14 Mar 2022 22:51:05 +0530 Subject: [PATCH 22/26] Update concepts/class-inheritance/about.md Co-authored-by: BethanyG --- concepts/class-inheritance/about.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index a717c2238d..a094e7bf27 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -288,3 +288,5 @@ But why we want to use `super()`? Using `super()` to call already created methods avoids having to rebuild those methods in our subclass and allows us to swap out superclasses with little code modifications. +[four-pillars]: https://www.educative.io/edpresso/what-are-the-four-pillars-of-oops-in-python + From 7fd20ab6277fdcb5d0d57f1b536cdf0bd08af74b Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Mon, 14 Mar 2022 22:51:25 +0530 Subject: [PATCH 23/26] Update concepts/class-inheritance/about.md Co-authored-by: BethanyG --- concepts/class-inheritance/about.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index a094e7bf27..27a6b3c681 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -290,3 +290,5 @@ Using `super()` to call already created methods avoids having to rebuild those m [four-pillars]: https://www.educative.io/edpresso/what-are-the-four-pillars-of-oops-in-python +[four-pillars]: https://www.educative.io/edpresso/what-are-the-four-pillars-of-oops-in-python + From 8dfc81b49133248415f59340f5400b91cf28c7fe Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Tue, 22 Mar 2022 20:24:51 +0530 Subject: [PATCH 24/26] Remove getters, setters and name mangling --- concepts/class-inheritance/about.md | 164 ---------------------------- 1 file changed, 164 deletions(-) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index 27a6b3c681..206b714aa6 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -3,170 +3,6 @@ Inheritance is one of the ['four pillars'][four-pillars] of Object Oriented Programming (`OOP`). In situations where only a small amount of functionality needs to be customized for a new class, `inheritance` allows code re-use from one or more parent classes, and can help make programs cleaner and more maintainable. -## More on Classes - -### Access Modifiers -Access modifications are used to restrict access to the variables and methods of the class in various object-oriented languages such as C++, Java, and Python. In most programming languages, there are three types of access modifiers in a class: `Public`, `Protected`, and `Private`. - -The access control for a specific data member or a member function of a class is determined by the `_` symbol in Python. In Python, access specifiers serve a crucial role in protecting data from unwanted access and preventing it from being misused. - -Python has three types of access modifiers: -1. Public access modifier -1. Protected access modifier -1. Private access modifier - -#### 1. Public Access Modifier: -The members of a class that are declared public are easily accessible from any part of the program. All data members and member functions of a class are public by default. -```python -class School: - #constructor - def __init__(self, name, location): - #public data members - self.school_name = name - self.school_loc = location - - # public member function - def school_details(self): - print("Location:", self.school_loc) - -obj = School("Harvard", "USA") -#Accessing public data members outside the class -print("Name:", obj.school_name) -obj.school_details() -``` -In the above program, the `school_name`, `school location` and `school_details()` are public data members. These data members can be accessed from anywhere in the program. They also can be inherited. - -#### 2. Protected Access Modifier: -Protected members of a class can only be accessed by a class that is derived from it. A single underscore `_` symbol is added before the data member of a class to declare it protected. -```python -# parent class -class Student: - - # protected data members - _name = None - _roll = None - _branch = None - - # constructor - def __init__(self, name, roll, branch): - self._name = name - self._roll = roll - self._branch = branch - - # protected member function - def _displayRollAndBranch(self): - - # accessing protected data members - print("Roll: ", self._roll) - print("Branch: ", self._branch) - - -# derived class -class About(Student): - - # constructor - def __init__(self, name, roll, branch): - Student.__init__(self, name, roll, branch) - - # public member function - def displayDetails(self): - - # accessing protected data members of super class - print("Name: ", self._name) - - # accessing protected member functions of super class - self._displayRollAndBranch() - -# creating objects of the derived class -obj = Student("ABC", 122333, "Physics") - -# calling public member functions of the class -obj.displayDetails() -``` -`_name`, `_roll`, and `_branch` are protected data members in the preceding application, while the `_displayRollAndBranch()` function is a protected method of the super class `Student`. The `displayDetails()` method is a public member function of the `About` class, which is derived from the Student class. The `displayDetails()` method in the `About` class accesses the `Student` class's protected data members. - -#### 3. Private Access Modifier: -The members of a class that are declared private are accessible within the class only. Private data members cannot be inherited by the derive class. A double underscore `__` sign is added before the data member of a class to make it private. - -```python -class Student: - - # private members - __name = None - __roll = None - __division = None - - # constructor - def __init__(self, name, roll, division): - self.__name = name - self.__roll = roll - self.__division = division - - # private member function - def __displayDetails(self): - - # accessing private data members - print("Name: ", self.__name) - print("Roll: ", self.__roll) - print("Branch: ", self.__division) - - # public member function - def accessPrivateFunction(self): - - # accessing private member function - self.__displayDetails() - -# creating object -obj = Student("ABC", 122333, "Physics") - -# accessing public member function of the class -obj.accessPrivateFunction() -``` -`__name`, `__roll`, and `__division` are private members of the class `Student`, while the `__displayDetails()` method is a private member function (accessible only within the class), and the `accessPrivateFunction()` method is a public member function of the class `Student` that can be accessed from anywhere within the programme. The `accessPrivateFunction()` method gives us access to the `Student` class's private members. -### Class Functions -The member functions of a class can be categorized into following two categories: - -__1. Accessor Functions__: These are those number functions that allow us to access data fields of the object. However, these functions cannot/do not change the value of the data members. - -```python -class Student: - # private members - __name = None - __roll = None - __marks = None - __div = None - - # constructor - def __init__(self, name, roll, marks, div): - self.__name = name - self.__roll = roll - self.__marks = marks - self.__div = div - - # public member function - def displayDetails(self): - # accessing private data members - print("Name: ", self.__name) - print("Roll: ", self.__roll) - print("Marks:", self.__marks) - print("Division: ", self.__div) - - # Accessor Function - def getRollNo(self): - return self.__roll - - # Mutator Function - def setDiv(self): - if self.__marks>=60: - self.__div = 'A' - elif self.__marks<60: - self.__div = 'B' - return self.__div -``` -__2. Mutator Function__: This method is used to change the state of an object, i.e. it changes the data variable's hidden value. It has the ability to change the value of a variable on the fly. The update method is another name for this function. We can also use the word _set_ to name these procedures. - -As per the above example, `getRollNo()` is an accessor function and `setDiv` is a mutator function. - ## Inheritance ## Single Inheritance From 3ca94b701635dc2b2eec6249c02a9fb8c0acd61a Mon Sep 17 00:00:00 2001 From: Girija Nandan Kar Date: Wed, 23 Mar 2022 16:48:38 +0530 Subject: [PATCH 25/26] Update about.md --- concepts/class-inheritance/about.md | 52 ++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index 206b714aa6..ee34d972e7 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -5,28 +5,48 @@ In situations where only a small amount of functionality needs to be customized ## Inheritance +`Inheritance` describes `is a kind of` relationship between two or more classes, abstracting common details into super class and storing specific ones in the subclass. +To create a child class, specify the parent class name inside the pair of parenthesis, followed by it's name. +Example +```python +class Child(Parent): + pass +``` +Every child class inherits all the behaviours exhibited by their parent class. + ## Single Inheritance When a derived (or child) class inherits only from one base (or parent) class, it is known as single inheritance. ```python -#Base class -class Odin: - def func1(self): - print("Odin: father of thor") - -#Derived/Subclass class -class Thor(Odin): - def func2(self): - print("Thor: son of Odin") - - -object = Thor() -object.func1() -object.func2() +class Person: + def __init__(self, fname, lname): + self.fname = fname + self.lname = lname +class Employee(Person): + all_employees = [] + def __init__(self, fname, lname, empid): + Person.__init__(self, fname, lname) + self.empid = empid + Employee.all_employees.append(self) +``` +`Employee` class is derived from `Person`. +Now, we can create an `Employee` object. +```python +... +p1 = Person('George', 'smith') +print(p1.fname, '-', p1.lname) +e1 = Employee('Jack', 'simmons', 456342) +e2 = Employee('John', 'williams', 123656) +print(e1.fname, '-', e1.empid) +print(e2.fname, '-', e2.empid) +``` +After running the program we will get the following output +```cmd +George - smith +Jack - 456342 +John - 123656 ``` -The derived class will print both the statements of `func1` and `func2`. - ## Multiple Inheritance As we've seen, a class inherits from another class in this scenario. On the other side, multiple inheritance is a feature that allows a class to inherit characteristics and methods from many parent classes. ```python From 5ea681a37ff5a697adfc92966cdb64c5c3901e36 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sat, 11 Jun 2022 10:28:09 -0700 Subject: [PATCH 26/26] Apply suggestions from code review --- concepts/class-inheritance/about.md | 30 +++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/concepts/class-inheritance/about.md b/concepts/class-inheritance/about.md index ee34d972e7..5db7909e2c 100644 --- a/concepts/class-inheritance/about.md +++ b/concepts/class-inheritance/about.md @@ -5,33 +5,47 @@ In situations where only a small amount of functionality needs to be customized ## Inheritance -`Inheritance` describes `is a kind of` relationship between two or more classes, abstracting common details into super class and storing specific ones in the subclass. +`Inheritance` describes `is a kind of` relationship between two or more classes, abstracting common details into super (_base_ or _parent_) class and storing specific ones in the subclass (_derived class_ or _child class_). + To create a child class, specify the parent class name inside the pair of parenthesis, followed by it's name. Example ```python class Child(Parent): pass ``` -Every child class inherits all the behaviours exhibited by their parent class. +Every child class inherits all the behaviors (_attributes, constructors, methods_) exhibited by their parent class. + ## Single Inheritance -When a derived (or child) class inherits only from one base (or parent) class, it is known as single inheritance. +When a derived (or child) class inherits only from one base (or parent) class, it is known as _single inheritance_. + ```python +# The parent or base class. class Person: + def __init__(self, fname, lname): self.fname = fname self.lname = lname + +# The child or derived class, inheriting from Person. class Employee(Person): + all_employees = [] def __init__(self, fname, lname, empid): + # Using the Parent constructor to create the base object. Person.__init__(self, fname, lname) + + # Adding an attribute specific to the Child class. self.empid = empid + Employee.all_employees.append(self) ``` `Employee` class is derived from `Person`. Now, we can create an `Employee` object. + + ```python ... p1 = Person('George', 'smith') @@ -42,18 +56,22 @@ print(e1.fname, '-', e1.empid) print(e2.fname, '-', e2.empid) ``` After running the program we will get the following output -```cmd +```bash + George - smith Jack - 456342 John - 123656 ``` ## Multiple Inheritance -As we've seen, a class inherits from another class in this scenario. On the other side, multiple inheritance is a feature that allows a class to inherit characteristics and methods from many parent classes. +As we've seen, `single inheritance` is where a class inherits directly from another class. +On the other side, `multiple inheritance` is a Python feature that allows a child class to inherit characteristics and methods from more than one parent class. + ```python class SubclassName(BaseClass1, BaseClass2, ...): pass ``` -### Diamond Problem +### Multiple Inheritance and the Diamond Problem + The "diamond problem" (also known as the "deadly diamond of death") refers to an ambiguity that occurs when two classes B and C inherit from a superclass A, while another class D inherits from both B and C. If A has a method "m" that B or C (or even both of them) has overridden, and if it does not override this method, the question becomes which version of the method D inherits. It's possible that it's from A, B, or C. Let's have a look at the problem using an example: