Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts

Saturday, March 1, 2014

Inheritance

Next Page

Inheritance

Def:  The  process  of implementing  the  "Parent-Child" relationship  between  two  or  more classes. As  a  part  of  this  "Inheritance"  implementation,  parent  and  child  classes  are  to  be implemented.
Inheritance

Advantage:  All  the  data  members  and  methods  of  the  parent class are accessible by child class. This is called as "Re-usability". That means the parent class members are re-usable in the child class.

  • The parent class can also be called as "Base" / "Super" class.
  • The child class can also be called as "Derived" / ―Sub" class.

Classification of Inheritance supported by C#:

1) Implementation Inheritance:
  • This is commonly used inheritance.
  • Whenever  a  class  is  derived  from  another  class,  it  can  be  called  as  "Implementation Inheritance"
  • As said above, all of the members of the super class are adopted by sub class.
2) Interface Inheritance:
  • This type of inheritance is taken from "Java".
  • Whenever  a  class  is  derived  from  an  interface,  it  can  be  called  as  "Interface Inheritance".
  • The  interface  is  similar  to  the  class,  but  doesn‘t  contain  the  method  definitions;  it contains the method declarations only. We discuss about the interfaces in-depth later

Types of Inheritance:

1)  Single Inheritance

Multi level inheritance
  • This is the simplest type of inheritance.
  • An  inheritance  relationship,  with  only  one  super  class and one sub class



                                               2) Hierarchical Inheritance

Single Inheritance

An  inheritance  relationship, with  only  one  super  class  and multiple sub classes.






3)  Multi-Level Inheritance
  • An inheritance relationship, with only one super class  and  multiple  sub  classes,  and  also extended  with  another  sub  class  from  the  first sub class.
  • In  other  words,  one  class  acts  as  super  class and sub class simultaneously.





                                            4) Multiple Inheritance

multiple inheritance
  • An  inheritance  relationship, with only multiple super classes and only one sub class.
  • "Multiple  Implementation Inheritance"  is  not  supported by  C#,  but "Multiple  Interface Inheritance" is supported by C#.


5)  Hybrid Inheritance
hybrid inheritance
  • An inheritance relationship that  contains  a combination  of  any  other two types of inheritances.
  • Ex:  Multiple + Hierarchical Inheritance
  •        Multiple + Multi Level Inheritance etc.
Note:  Other OOP languages like C++ support multiple inheritance. But C# and  VB.NET doesn‘t 
support  multiple  inheritance  to  avoid  some  practical  problems  while  developing  future  GUI 
applications like windows forms applications, web sites etc.
But instead, C# and VB.NET supports ―multiple interface inheritance‖, because it doesn‘t cause any practical problems.

Principles of OOPS

Principles of Object Oriented Programming:

  • Data Encapsulation
  • Data Abstraction
  • Polymorphism
  • Inheritance

Data Encapsulation 

This is the default nature of a class. A  class  encapsulates  (combines)  some  data  values  (fields  or properties)  and some operations (methods) related to the data as a pack. This nature can be called as Data Encapsulation.

Data Abstraction:

The nature of "instance" creation is called as "Data Abstraction". This feature is taken from "structures" in C  languages. (In ‗C‘ language you can create  any  no.  of  structure  variables  based  on  one  structure.  The  structure variables act as instances for the structure). In the same way, you can create any no. of instances (objects) for a class.

Polymorphism:

The decision making capacity of the compiler is called "Polymorphism".
It is of two types:
  • Compile-time Polymorphism:
          The decision can be made at the time of compilation. This can be implemented with two concepts:
           i)  Method  Overloading:  You  can  write  multiple methods with same name within the same class.
           ii)  Operator  Overloading:  You  can  write  multiple definitions for the same operator.
  • Run-time Polymorphism:
         The decision can be made at run time. This can be implemented with two concepts:
          i)  Templates / Generics:  You  can  pass  any  type  of value as an argument to a method.
          ii)  Interfaces:  Contains  method  declarations  only, doesn‘t contain method definitions.

Inheritance:

The  process  of  creating  a  new  class  that  derives  all  the  features  of  another existing class is called as "Inheritance". In other words, we can extend the features of a class.
Inheritance
Flag Counter