Python Inheritance (OOP) > Python

Entire search within the site

Python

Python Inheritance (OOP)

Page Info

Content

Python Inheritance (OOP)

In Python, inheritance allows a class (child class) to inherit attributes and methods from another class (parent class). This promotes code reuse, modularity, and extensibility in object-oriented programming.

Basic Inheritance Example


# Parent class
class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print(f"{self.name} makes a sound")

# Child class inherits from Animal
class Dog(Animal):
    def speak(self):
        print(f"{self.name} barks")

# Create objects
animal = Animal("GenericAnimal")
animal.speak()  # Output: GenericAnimal makes a sound

dog = Dog("Buddy")
dog.speak()     # Output: Buddy barks
  

Explanation:

  • class Dog(Animal): defines Dog as a subclass of Animal.
  • The child class inherits all attributes and methods from the parent class.
  • Method overriding allows the child class to provide its own implementation of a parent method.

Using super() to Call Parent Methods


class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print(f"{self.name} makes a sound")

class Cat(Animal):
    def __init__(self, name, color):
        super().__init__(name)  # Call parent constructor
        self.color = color

    def speak(self):
        super().speak()  # Call parent method
        print(f"{self.name} meows")

# Create object
cat = Cat("Whiskers", "Gray")
cat.speak()
# Output:
# Whiskers makes a sound
# Whiskers meows
  

Key Points

  • Inheritance: Child class inherits attributes and methods from parent class.
  • Method overriding: Child class can provide a new implementation of a parent method.
  • super(): Used to call parent class methods or constructors from the child class.
  • Promotes code reuse, modularity, and maintainable OOP design.

SEO Keywords

Python inheritance example, Python OOP inheritance, Python subclass, Python super(), Python method overriding, Python parent child class

Inheritance in Python allows you to create a hierarchy of classes, reuse code efficiently, and extend functionality in a clean and organized way.

Good0 Bad0

댓글목록

등록된 댓글이 없습니다.

Python

Latest Posts

Total 31 posts | Page 1
Python List
Title-Author-data
Python Regular Expressions (RegEx) Basic Matching 12
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Timer and Time Measurement (time module) 9
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Iterators (Iterator Usage) 10
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Inheritance (OOP) 6
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Classes and Object Creation (OOP) 8
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Dictionary Sorting (By Key / By Value) 5
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Logging Implementation 5
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Exception Handling (try-except) 8
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python CSV File Reading and Processing 7
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python JSON Data Handling (Read/Write) 6
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python File Reading and Writing (Text Files) 4
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Decorators: Implementation and Usage 11
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Generators: Implementation and Usage 7
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Lambda Functions 9
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Remove Duplicate Elements from a List 11
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New

visit

today
58
yesday
144
maxday
144
allday
202
Copyright © https://goposu.com/g All rights reserved.