Skip to main content

Inheritance (OOP)

Inheritance:

In object-oriented programming, inheritance means an object or class has inherited behaviors from another object or class. A child class is inherited from parent class.A child inherits characteristics of its parents, besides inherited characteristics, a child may have
its own unique characteristic.

.


In inheritance, every object of a child class is also an object of parent class, but not vice-versa.

Base class: It is the class from which features are to be inherited into another class.

Derived class: It is the class in which the base class features are inherited. A derived class can have additional properties and methods not present in the parent class that distinguishes it and provides additional functionality.

REAL WORLD EXAMPLE:

Let’s consider the Windows operating system. Windows 98 had certain properties and methods that were used in Windows XP. Windows XP was derived from Windows 98, but it was still different from it. Windows 7 was derived from Windows XP, but they were both different. They both followed a certain basic template and shared properties.


If we have Flower as a base class and Rose as a derived class, then all Roses are Flowers, but not all Flowers are Roses (is a relationship).

Advantages of Inheritance:


  • Reuse
  • Less redundancy
  • Increased maintainability


SYNTAX (INHERITANCE):

class derived-class-name : access base-class-name {
// body of class
};



TYPES OF INHERITANCE WITH RESPECT TO BASE CLASS ACCESS CONTROL:


  •  Public
  •  Private
  •  Protected







Types Of Inheritance: 

  • Single Inheritance
  • Hierarchical Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance 

Single Inheritance:

Single Inheritance is the type of inheritance in which there is one base class and one derived class.




Hierarchical Inheritance:

This is the type of inheritance in which there are multiple classes derived from one base class. This type of inheritance is used when there is a requirement of one class feature that is needed in multiple classes.





Multilevel Inheritance:

When one class is derived from another derived class then this type of inheritance is called multilevel inheritance.




Multiple Inheritance:

In multiple inheritances, one class inherits the features of multiple classes simultaneously, hence this type of inheritance is called Multiple Inheritance.





Hybrid Inheritance:

The method of combining any two or more forms of inheritance in single form is called hybrid inheritance.





Comments

Popular posts from this blog

Data Types

DATA TYPES in C/C++: Since C and C++ are strongly type languages, therefore every thing must be defined. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Data Types Description C-Keyword Format Specifier integer Integers are whole numbers that can have both positive and negative values but no decimal values.   int   %d or %i Float Floating type variables can hold real numbers precision of 6 digits. float %f Double can hold real numbers with the precision of 14 digits. double %f Character data type allows a variable to store only one character. char %c Implementation in C: #include<stdio.h> int main() { printf(“%d n”,18); //this will print integer printf(“%fn”,32.56); //this will print decimal number printf(“%d %d %dn”,32,56,89); // %d is a place holder for integer and it provide place for one integer printf(“%ld...

Tic-Tac-Toe Implementation in Cpp (C++) Step by Step

Conditional Statement

Conditional Statement: A conditional statement is an if-then statement which means that if some statement is true then it follows the if-block of code and if the condition is false then it follows the else-block of code. In the programming world, there are two types of conditional statement namely if-else and switch. if-else Statement: Syntax: if (condition 1) { } else if (condition 2) { } else { }   Implementation of If-else statement in C: Q: Write a program to find the greatest of three numbers. #include<stdio.h> int main() { int a,b,c; printf(“Please Enter First Number “); scanf(“%d”,&a); printf(“Please Enter Second Number “); scanf(“%d”,&b); printf(“Please Enter Third Number “); scanf(“%d”,&c); if (a>b && a>c) { printf(“The Greatest Number is %d”,a); } else if (b>c) { printf(“The Greatest Number is %d”,b); } else { pr...