[go: up one dir, main page]

0% found this document useful (0 votes)
7 views7 pages

DPnew

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

DPnew

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Introduction to

Inheritance in
Object-Oriented
Programming
Inheritance is a fundamental concept in object-oriented
programming (OOP). It allows you to create new classes that
inherit properties and methods from existing classes. This
promotes code reusability and creates a hierarchical relationship
between classes.
Inheritance Types: Single,
Multilevel, and
Hierarchical
1 Single Inheritance 2 Multilevel Inheritance
A derived class inherits from A derived class inherits from
only one base class. This is another derived class,
the simplest and most creating a chain of
common type of inheritance. inheritance.

3 Hierarchical Inheritance4 Hybrid Inheritance


Multiple derived classes This combines multiple
inherit from the same base inheritance types, allowing for
class. This allows for the complex relationships
creation of specialized between classes.
subclasses that share
common properties and
methods.
Inheritance for Laymen:
Analogy of a Family Tree
1 Grandparent
The grandparent class represents the most general characteristics
and behaviors. For example, a "Grandparent" class might have
properties like "Name" and "Age" and a method like "Walk()."

2 Parent
The parent class inherits from the grandparent class. It inherits all
the properties and methods of the grandparent and can add its
own specific traits. For example, a "Parent" class might have a new
method called "Work()."

3 Child
The child class inherits from the parent class. It inherits all the
properties and methods of the parent and can add even more
specialized features. For example, a "Child" class might have a
new method called "Play()."
Technical Example: Inheritance in C#
Base Class Derived Class Inheritance

The base class, "Animal," defines The derived class, “Bird," inherits Inheritance allows you to create a
common properties like "Name" from "Animal." It can access and “Bird" object that can use "Name"
and "Age." It also contains reuse all the properties and and "Age" from the "Animal" class
methods like "Move()." methods of "Animal" and can also and also “Fly()." It promotes code
add its own specific behaviors like reusability and reduces
“Fly()." redundancy.
Code Snippet: Implementing
Single Inheritance in C#
class Animal
{ public:
int age;
string name;
Animal(int a) : age(a)
void eat()
{ std::cout << "This animal is eating." << std::endl; }
virtual void move()
{ std::cout << "This animal is moving." << std::endl;
};

class Bird : public Animal


{
public:
Bird(int a) : Animal(a) {}
void fly()
{ std::cout << "This bird is flying." << std::endl;
void move() override
{ std::cout << "This bird is hopping or flying." << std::endl;
}
};
Advantages of Inheritance
in OOP
Code Reusability Code Organization
Inheritance reduces the need to It helps create a hierarchical
write the same code multiple structure for classes, which
times. By inheriting from a base improves code organization and
class, derived classes can reuse readability.
existing properties and
methods.

Code Maintenance Polymorphism


Changes made to the base class Inheritance enables
are automatically reflected in all polymorphism, a powerful
derived classes, simplifying concept that allows you to write
code maintenance and reducing code that can work with
errors. different types of objects.
Conclusion and Key Takeaways

In conclusion, inheritance is a key concept in object-oriented


programming that allows us to reuse code efficiently. It helps us avoid
writing the same code multiple times by letting child classes inherit
features from a parent class. This makes our code more organized and
easier to manage. Inheritance also enables polymorphism, which
allows us to use different objects in the same way, making the code
more flexible and adaptable.

You might also like