SINGLE INHARITANCE
SINGLE INHARITANCE
INHERITANCE
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class (called the child or subclass) to inherit the properties and behaviors of another class (called the parent or superclass). It enables code reusability, hierarchy creation, and easier maintenance.
KEY FEATURES OF INHERITANCE:🔑
1. Code Reusability🏷 – The subclass can reuse the code of the parent class, reducing redundancy.
2. Extensibility🏷 – The subclass can add new features or modify existing ones without altering the parent class.
3. Hierarchy Representation🏷 – It helps model real-world relationships, like “a Car is a type of Vehicle.”
4. Method Overriding🏷 – A subclass can provide a specific implementation of a method already defined in the parent Class.
TYPES OF INHERITANCE:
1. Single Inheritance🖋 – One child class inherits from one parent class.
2. Multiple Inheritance🖋 – A child class inherits from more than one parent class (supported in some languages like Python, but not in Java).
3. Multilevel Inheritance🖋 – A class inherits from another class, which in turn inherits from another class.
4. Hierarchical Inheritance🖋 – Multiple child classes inherit from a single parent class.
5. Hybrid Inheritance🖋 – A combination of multiple inheritance Types.
📍IN THIS ARTICLE WE ARE GOING TO DISCUSS ABOUT SINGLE INHERITANCE:
1.SINGLE INHERITANCE:
Single Inheritance is a type of inheritance in object-oriented programming where a child class (subclass) inherits from a single parent class (superclass). This means the child class gets access to the methods and attributes of the parent class, allowing code reusability and reducing redundancy.
KEY POINTS OF SINGLE INHERITANCE:🔑🗝
☆One child class inherits from one parent class.
☆The child class can use the parent class’s properties and methods.
☆The child class can also define its own additional methods and properties.
BASE CLASS EXPLANATION💥:
●A base class is a parent class from which other classes can inherit properties and behavior. It provides a common interface and implementation that can be shared by its derived classes. The base class is also known as the superclass or parent class.
DERIVED CLASS EXPLANATION💥:
●A derived class is a child class that inherits properties and behavior from a base class. It builds upon the foundation provided by the base class and can add new attributes and methods or override existing ones. The derived class is also known as the subclass or child class.
PROGRAM:😈🖥
#include<iostream>
Using namespace std;
Class personal
{
Public:
Int age;
Float height,weight;
Char name[30];
Void get();
Void print();
};
Void personal::get()
{
Cout<<” Enter your name :- “;
Cin>>name;
Cout<<”\n Enter your age :- “;
Cin>>age;
Cout<<”\n Enter your height :- “;
Cin>>height;
Cout<<”\n Enter your weight :- “;
Cin>>weight;
}
Void personal::print()
{
Cout<<”\n -------------------“;
Cout<<”\nMy name is :- “<<name;
Cout<<”\nMy age is :- “<<age;
Cout<<”\nMy height is :- “<<height;
Cout<<”\nMy weight is :- “<<weight;
}
Class details:public personal //derived class
{
Public:
Char hobby[30];
Void get();
Void print();
};
Void details::get()
{
Personal::get(); //the calling of base class input fn
Cout<<”\n Enter your hobby :- “;
Cin>>hobby;
Cin>>name>>age>>height>>weight;
}
Void details::print()
{
Personal::print();
Cout<<”\nMy hobby is :- “<<hobby;
}
Int main()
{
Cout<<”\n_____PERSONAL DETAILS_____”<<endl;
Details d;
d.get();
d.print();
return 0;
}
OUTPUT😲:
_____PERSONAL DETAILS_____
Enter your name :-
Enter your age :-
Enter your height :-
Enter your weight :-
Enter your hobby :-
-------------------
My name is :- M.KALIESHWARAN
My age is :- 19
My height is :- 150.5
My weight is :- 47.4
My hobby is :- Playing
🫠 ADVANTAGE OF SINGLE INHERITANCE:
1. Simplicity🤗 – Since there is only one parent class, it is easy to understand and implement.
2. Code Reusability🤗– The child class can reuse the methods and properties of the parent class, reducing code duplication.
3. Less Complexity🤗 – The relationship between classes remains straightforward, making debugging and maintenance easier.
4. Avoids Ambiguity🤗– Unlike multiple inheritance, single inheritance prevents confusion due to duplicate methods or attributes from multiple parent classes.
5. Better Performance🤗 – Since only one parent class is involved, method resolution and execution are faster than complex inheritance models.
😔DISADVANTAGES OF SINGLE INHERITANCE:
1. Limited Functionality🤕 –only one parent, it cannot directly use features from multiple Since a child class inherits from classes.
2. Code Duplication🤕 – If different child classes require similar functionalities from multiple sources, it may lead to redundant code.
3. Less Flexibility🤕– If a class needs additional functionalities, developers may have to modify the parent class, which can affect all child classes.
4. Dependency on Parent Class🤕 – Any modification in the parent class can impact all child classes, potentially causing unintended issues.
5. Slower Development in Some Cases🤕 – If functionalities from multiple sources are needed, developers might have to use workarounds like interfaces or helper classes, which can slow down development.
👉CONCLUSION:
Single inheritance enables code reusability and a hierarchical relationship between classes. Derived classes can build upon and modify the base class behavior. It's a fundamental concept in object-oriented programming.
✨️ PREPARED BY,
NAVANESH.R
KARTHIKEYAN.S
1,st B.SC,COMPUTER SCIENCE
DATE:13/03/2025
Good
ReplyDeleteGood
ReplyDeleteGood
ReplyDelete