picha

Python somo la 28: inheritance kwenye OOP

Katika somo hili utakwenda kujifunz amaana ya inheritance na sheria za kuiandika inheritance.

Inheritance in Python

Inheritance ni dhana inayoruhusu class moja (child class) kurithi sifa (properties) na tabia (methods) kutoka kwa class nyingine (parent class). Hii hurahisisha matumizi tena ya code na kuwezesha kuongeza vipengele bila kuharibu class asilia. Class inayorithiwa huitwa parent class yaani mzazi na class inayorithi huitwa child class yaani mtoto.

 


 

Vipengele Muhimu vya Inheritance:

  1. Parent Class: Class inayorithisha properties na methods zake.

  2. Child Class: Class inayorithi kutoka kwa parent class. Inaweza kuongeza au kurekebisha functionalities za parent class.

 


 

Aina za Inheritance:

  1. Single Inheritance: Child class inarithi kutoka kwa parent class moja.

  2. Multilevel Inheritance: Child class inarithi kutoka kwa parent class, ambayo nayo inarithi kutoka kwa parent mwingine.

  3. Hierarchical Inheritance: Parent class moja inarithiwa na child classes nyingi.

  4. Multiple Inheritance: Child class inarithi kutoka kwa parent classes zaidi ya moja.

 


 

 

Mfano wa Single Inheritance:

Ili kuweza kurithi kutoka kwenye parent class, tutaweka jins la class inayorithiwa  wwakati kunapotengeneza child class. Mfano kama parent class ni Animal na tukataka kuandika child class tutaandika hivi Dog(Animal)

Mfano:

class Animal:

    def speak(self):

        print("This animal speaks.")

 

class Dog(Animal):

    def bark(self):

        print("Woof! Woof!")

 

# Object creation

dog = Dog()

dog.speak()  # Method kutoka parent class

dog.bark()   # Method ya child class

 

Hapa, Dog ni child class inayorithi method speak() kutoka Animal.

 


 

Mfano wa Multilevel Inheritance:

class Animal:

    def speak(self):

        print("This animal speaks.")

 

class Mammal(Animal):

    def walk(self):

        print("This mammal walks.")

 

class Dog(Mammal):

    def bark(self):

        print("Woof! Woof!")

 

# Object creation

dog = Dog()

dog.speak()  # Method kutoka Animal

dog.walk()   # Method kutoka Mammal

dog.bark()   # Method ya Dog

 

 

 


 

Multiple Inheritance:

class Animal:

    def speak(self):

        print("This animal speaks.")

 

class Pet:

    def is_pet(self):

        print("This is a pet.")

 

class Dog(Animal, Pet):

    def bark(self):

        print("Woof! Woof!")

 

# Object creation

dog = Dog()

dog.speak()  # Method kutoka Animal

dog.is_pet() # Method kutoka Pet

dog.bark()   # Method ya Dog

 

 

 


 

Faida za Inheritance:

  1. Matumizi Tena ya Code: Hakuna haja ya kuandika code sawa mara kwa mara.

  2. Upanuzi wa Tabia: Unaweza kuongeza functionalities mpya kwenye child class bila kuharibu parent class.

  3. Kuonyesha Mahusiano Halisi: Inaonyesha urithi wa kihierarkia (Hierarchical Inheritance) wa tabia za vitu halisi.

 

Matumizi ya "super()" Keyword:

Inatumika kwenye child class kuita methods za parent class, hasa constructor __init__.

Mfano:

 

class Animal:

    def __init__(self, name):

        self.name = name

 

class Dog(Animal):

    def __init__(self, name, breed):

        super().__init__(name)  # Kuita constructor ya parent

        self.breed = breed

 

dog = Dog("Buddy", "Golden Retriever")

print(f"{dog.name} is a {dog.breed}.")

 

 


 

Inheritance hutoa nguvu kubwa ya kuunda code inayoweza kutumiwa tena na inayoweza kupanuliwa kwa urahisi.

 

Jiunge nasi WhatsApp kupata update zetu
Zoezi la Maswali

Nyuma Endelea


Umeionaje Makala hii.. ?

       
Author: Rajabu image Tarehe: 2024-12-03 14:41:46 Topic: Python Main: Masomo File: Download PDF Views 745

Share On:

Share follows: 0 | Unique share links followed: 0
Sponsored links
👉1 Bongolite - Game zone - Play free game     👉2 Kitabu cha Afya     👉3 Simulizi za Hadithi Audio     👉4 web hosting     👉5 Madrasa kiganjani     👉6 kitabu cha Simulizi    

Post zinazofanana:

PYTHON somo la 11: Matumizi ya comperison eperator katika python

Katika somo hili utakwenda kujifunza kuhusu operator. Hasa hapa tutakwend akujifunz akuhusu comparison operaor.

Soma Zaidi...
PYTHON somo la 12: assignment operator, logical operator, identity oeprator na membership operator

Katika somo hili utakwenda kujfunza operator nyingine kama assignment operator logical, identity operator na membership operator.

Soma Zaidi...
Python somo la 35: Jinsi ya kutumia MYSQL kwenye python

Katika somo hili utajifunz akutumia database kwenye python

Soma Zaidi...
Python somo la 26: Sheria za uandishi wa object

Katika somo hili utakwend akujifunza maana ya object, na sheria za kuandika object.

Soma Zaidi...
Python somo la 22: Package kwenye Python

Hapa utakwenda kujifunz amaana ya package, aina zake na jinsi ya kuandika package.

Soma Zaidi...
Python somo la 46: Kutengeneza Fomu na Kuituma kwa Django Template

Katika somo hili utakwenda kujifunza kutengeneza htmk form pamoja na kuituma.

Soma Zaidi...