diff --git a/content/python/concepts/built-in-functions/terms/super/super.md b/content/python/concepts/built-in-functions/terms/super/super.md new file mode 100644 index 00000000000..028ffa2d6d1 --- /dev/null +++ b/content/python/concepts/built-in-functions/terms/super/super.md @@ -0,0 +1,92 @@ +--- +Title: 'super()' +Description: 'Returns a temporary object that allows a given class to inherit the methods and properties of a parent or sibling class.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Functions' + - 'Objects' + - 'Inheritance' + - 'Classes' + - 'Functions' + - 'Inheritance' + - 'Methods' + - 'Objects' + - 'OOP' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +The **`super()`** function returns a temporary object that allows a given [class](https://www.codecademy.com/resources/docs/python/classes) to [inherit](https://www.codecademy.com/resources/docs/python/inheritance) the methods and properties of a parent or sibling class. + +## Syntax + +```pseudo +super(type, obj) +``` + +The `type` parameter specifies the type object of the parent class. The `obj` is optional and is an instance, or subtype, of the class `type`. + +### Inside Class Definition + +When used inside a class definition, the `super()` function can be used with zero arguments since the given class inherits from a parent class: + +```pseudo +class A(): + method(self, arg): + #Method code starts here + +class B(A): + method(self, arg): + super().method(arg) + #Method code starts here +``` + +The `super()` function allows the child class `B` to access the `.method()` of parent class `A`. + +### Outside Class Definition + +The following syntax can be applied inside and outside of a class definition: + +```pseudo +class B(A): + method(self, arg): + super().method(arg) + #Method code starts here + +instance_of_b = B() +super(B, instance_of_b).method() +``` + +The `super()` function accepts the class type `B` along with an `instance_of_b` variable to direct the lookup search for a `.method()` in the nearest parent class. + +## Codebyte Example + +In the following example, both syntaxes of `super()` are implemented in the classes `Python` and `Java` to access the `intro()` method of the parent `ProgramLanguage` class: + +```codebyte/python +# Base class +class ProgramLanguage: + def intro(self): + print("Hi! I am a Programming Language.") + +# Derived class +class Python(ProgramLanguage): + def intro(self): + super().intro() + print("Python here!") + +# Derived class +class Java(ProgramLanguage): + def intro(self): + super(Java, self).intro() + print("Java here!") + +python_lang = Python() +python_lang.intro() + +java_lang = Java() +java_lang.intro() +``` diff --git a/content/python/concepts/inheritance/inheritance.md b/content/python/concepts/inheritance/inheritance.md index 5303591232c..dfeaeaf6ad1 100644 --- a/content/python/concepts/inheritance/inheritance.md +++ b/content/python/concepts/inheritance/inheritance.md @@ -8,12 +8,13 @@ Subjects: Tags: - 'Inheritance' - 'OOP' + - 'Classes' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -Inheritance is an object-oriented programming concept where a class (often referred to as the child class) derives attributes and behaviors from another class (often referred to as the parent class). +**Inheritance** is a concept in [object-oriented programming](https://www.codecademy.com/resources/docs/general/object-oriented-programming) where a child [class](https://www.codecademy.com/resources/docs/python/classes) (or subclass) derives attributes and behaviors from a parent or sibling class. This eliminates the need to implement the methods inherited by a subclass, or child class, again. In other words, it enables a child class to inherit/reuse the attributes and methods of a parent class. In terms of real-world objects, it represents an IS-A relationship. @@ -66,30 +67,7 @@ Hi! I am a Programming Language. Python here! ``` -**Note**: Inside the child class, the same number and type of arguments need to be passed into the parent class' method. - -### `super()` - -`super()` returns a temporary object of the superclass, allowing the superclass’s methods to be called. This comes in handy when using [multiple inheritance](#multiple-inheritance). - -In this case, the `.say_hi()` method is implemented utilizing `super()` inside `.intro()`: - -```py -class Python(ProgramLanguage): - def intro(self): - super().say_hi() - print("Python here!") - -doc = Python() -doc.intro() -``` - -The output would be: - -```shell -Hi! I am a Programming Language. -Python here! -``` +> **Note**: Inside the child class, the same number and type of arguments need to be passed into the parent class' method. ## Overriding Methods