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

Pointers

Pointers: A pointer is a variable whose value is a memory address. A pointer contains the memory address of a variable that, in turn, contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value. Syntax: type * variable ; Interpretation: The value of the pointer variable ptr is a memory address. A data item whose address is stored in this variable must be of the specified type. Dynamic Memory Management: C/C++ enables programmers to control the allocation and deallocation of memory in a program for any built in or user defined type. The ability for a program to obtain more memory space at execution time to hold new nodes, and to release space no longer needed is known as dynamic memory management. Syntax: int *num = (int *)malloc(sizeof (int)*numCount); or int *ptr = (int *)calloc(numCount, sizeof (int)); /* returns a pointer to a section of memory just large enough to hold the integers, whose q...

Single LinkList in Java

Linked List: Linked List contains a sequence nodes which are linked together. Each node contains a connection to another link and data. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List. Link − Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. LinkedList − A Linked List contains the connection link to the first link called First. Types of Linked List: Following are the various types of linked list. Simple Linked List − Item navigation is forward only. Doubly Linked List − Items can be navigated forward and backward. Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous. Basic Operations: Insert:  Inserts at tail,  specific index. Delete: Deletes from the tail.  specific index. ...

Object Oriented Programming (OOP)

Object Oriented Programming: Object Oriented Programming (OOP) is a programming concept which used in the modern programming world. Languages like Java, C++,  and Python support Object Oriented Programming (OOP). It works on the principle that objects are the most important part of a program. In OOP we think in terms of objects and every object has its attributes (properties) and a state (behavior/functions). Object Oriented Programming (OOP) is a technique of system modeling and its main purpose is to understand the product before developing it and manipulating these objects to achieve a specific task.   Pillars of Object Oriented Language (OOP): There are four basic principles of Object Oriented Language (OOP). Inheritance Polymorphism Data Encapsulation Abstraction WHY is Object Oriented Language (OOP) NEEDED? Problems with Procedural Languages: Functions have unrestricted access to global data Unrelated Functions and data. Before Object Oriente...