Object Oriented Programming -Single Inheritance.pptx

jospinjj 11 views 6 slides Feb 28, 2025
Slide 1
Slide 1 of 6
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6

About This Presentation

Single Inheritance


Slide Content

Single Inheritance Dr.J.Jospin Jeya

Single Inheritance in Object-Oriented Programming (OOP) Single inheritance refers to a scenario where a class (called the child class or subclass ) inherits properties and behaviors (methods) from one parent class (called the superclass or base class ). This is one of the simplest forms of inheritance and a core concept in many object-oriented programming languages like Java, Python, C++, etc. In single inheritance , the child class can access all non-private attributes and methods of the parent class, and it can also define its own additional properties and methods.

Key Points: Parent Class : The class that provides the base functionality. Child Class : The class that inherits functionality from the parent class. The child class can override methods from the parent class if needed. It allows code reuse and improves the organization of code.

Example # Parent Class (Superclass) class Animal: def __ init __(self, name, species): self.name = name self.species = species def make_sound (self): print("Some generic animal sound") # Child Class (Subclass) class Dog(Animal): # Dog is inheriting from Animal def __ init __(self, name, species, breed): super().__ init __(name, species) # Call the parent class constructor self.breed = breed def make_sound (self): # Overriding the make_sound method print("Bark!")

Cont.. # Create an object of the Dog class dog1 = Dog("Buddy", "Dog", "Golden Retriever") dog1.make_sound() # Output: Bark! print(dog1.name) # Output: Buddy print(dog1.species) # Output: Dog print(dog1.breed) # Output: Golden Retriever

Cont.. Explanation: Parent Class (Animal) : Contains the name, species, and a make_sound method. Child Class (Dog) : Inherits from Animal. It has its own breed attribute and overrides the make_sound method to provide a more specific sound ("Bark!"). The super() function is used to call the constructor (__ init __) of the parent class so that the child class can initialize the inherited properties.
Tags