Ir al contenido principal

Inheritance

WHAT IS?


Is the procedure in which one class inherits the attributes and methods of another class. The class whose properties and methods are inherited is known as the Parent class. 


                                     

There are 5 types of inheritance: 

    1. Single Inheritance: This is a form of inheritance in which a class inherits only one parent class. This is the simple form of inheritance and hence also referred to as simple inheritance.



      Here the class Child is inheriting only one class Parent, hence this is an example of Single inheritance.

    2. Multiple Inheritance: An inheritance becomes multiple inheritances when a class inherits more than one parent class. The child class after inheriting properties from various parent classes has access to all of their objects.



      Here we have one Child class which is inheriting properties of three-parent classes Parent_1, Parent_2, and Parent_3. All the classes have different functions and all of the functions are called using the object of the Child class.

    3. Multi-level Inheritance: For example, a class_1 is inherited by a class_2 and this class_2 also gets inherited by class_3 and this process goes on. This is known as multi-level inheritance. Let’s understand with an example: 



    4. Hierarchical Inheritance: In this, various Child classes inherit a single Parent class. The example given in the introduction of the inheritance is an example of Hierarchical inheritance since classes BMW and Audi inherit class Car.

      For simplicity let’s look at another example:



      Here two child classes are inheriting the same Parent class. The class Child_1 has access to functions f1() of Parent class and function f2() of itself. Whereas the class Child_2 has access to functions f1() of Parent class and function f3() of itself.

    5. Hybrid Inheritance: When there is a combination of more than one form of inheritance, it is known as hybrid inheritance. It will be more clear after this example:



      In this example, two classes ‘Child_1′ and ‘Child_2’ are derived from base class ‘Parent’ using hierarchical inheritance. Another class ‘Child_3’ is derived from classes ‘Child_1’ and ‘Child_2’ using multiple inheritances. The class ‘Child_3’ is now derived using hybrid inheritance.

Comentarios

Entradas más populares de este blog

Encapsulation

 WHAT IS? Encapsulation is one of the fundamentals of OOP (object-oriented programming). It refers to the bundling of data with the methods that operate on that data. Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties’ direct access to them. Publicly accessible methods are generally provided in the class (so-called getters and setters) to access the values, and other client classes call these methods to retrieve and modify the values within the object. The most important principle of object orientation is encapsulation: the idea that data inside the object should only be accessed through a public interface – that is, the object’s methods. If we want to use the data stored in an object to perform an action or calculate a derived value, we define a method associated with the object which does this. Then whenever we want to perform this action we call the method on the object. We consider it bad practice to retriev

Object-Oriented Programming

WHAT IS OOP? Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior. OOP focuses on the objects that developers want to manipulate rather than the logic required to manipulate them. This approach to programming is well-suited for programs that are large, complex and actively updated or maintained. This includes programs for manufacturing and design, as well as mobile applications; for example, OOP can be used for manufacturing system simulation software. And these are 3 fundamental concepts of OOP: Inheritance Polymorphism Encapsulation

Polymorphism

WHAT IS? Polymorphism in OOPs is inseparable and an essential concept of every object-oriented programming language. An object or reference basically can take multiple forms in different instances. As the word suggests, ‘poly’ means ‘many’ and ‘morph’ points at ‘forms’; thus, polymorphism as a whole would mean ‘a property of having many forms.’ The object-oriented programming language processes classes and objects by a single interface. It implements the concepts of function overloading, overriding, and virtual functions. Also, it is typically used for instrumenting inheritance in programming.   Types of Polymorphism in Oops In Object-Oriented Programming (OOPS) language, there are two types of polymorphism as below: Compile Time or Static Polymorphis With Method Overloading, static polymorphism is achieved in Object-Oriented Programming languages that allow the programmer to implement various methods. The names they use can be the same, but their parameters are different. Certain cond