WHAT IS INHERITANCE?
Inheritance is where Object-Oriented Programming really comes into its own. Understanding this most important concept is the key to effective C# Programming and the goal of this Tutorial. There's no going back now. after you've completed this part, you can call yourself an Object-Oriented-Programmer. so to understand the concept of Inheritance, let's take a real-life example in our this tutorial.
Inheritance is why we look similar to our Mom or Dad. People's bodies have characteristics, like the shape of their nose or the color of their eyes, and parents pass on these characteristics to their children, this is inheritance.
We are made of cells like a building is made of bricks. Our cells are told what to do by DNA. DNA is the boss and tells the cells how to make a human body. All of us are made up of DNA from both our Mom and dads so we inherit orders on how to build a body from both of them that is why we have characteristics from both our parents.
People look like People. Every Person is Unique but they have far more in common than they are different. This is Because we all are related and we have all inherited characteristics from our ancestors. This is all Inheritance. and Now I can hope that you Understand the concept of Inheritance, So Let's move to our Programming World.
inheritance in C#:
Inheritance is one of the most Important concepts in OOP. Inheritance Allows us to define a Class in terms of Another Class, which makes it easier to create and Maintain an application. This Also Provides an Opportunity to reuse the code functionality and speeds up implementation time.
When We create a Class, Instead of Writing completely New Data Members and Member Methods, we can Designate that the New class should inherit the Members of an Existing Class. This Existing Class is called the Base Class, and the New class is referred to as the Derived Class.
Syntax:
A Class can be derived from More than One Class. Which Means that it can Inherit data and Methods from Multiple base classes. We can create an Derived Class using the Following Syntax:
Access-Specifier class Base_Class
{
// Base Class Body:
}
class Derived_Class : Base_class
{
// Derived Class Body:
}
Let's have a Simple example to Know, How this Inheritance Work. We will create two classes, and the second class will use some Methods and Members of first class sense of Inheritance.
/*Example: Inheritance : InfoBrother: */
using System;
namespace Inheritance
{
class base_class //base Class:
{
public double width;
public double height;
public void show()
{
Console.WriteLine("Width = {0} | Height = {1}", width, height);
}
}
class derived_class : base_class //Derived Class:
{
public string style;
public double area()
{
return width * height / 2; //using Base Class Members:
}
public void showStyle()
{
Console.WriteLine("Triangle is: " + style);
}
}
class Program //main class.
{
static void Main(string[] args)
{
//creating object of type Derived_class:
derived_class tringle1 = new derived_class();
derived_class tringle2 = new derived_class();
//setting Values for Triangle 1.
tringle1.width = 4.0;
tringle1.height = 4.0;
tringle1.style = "Isosceles";
//show Triangle 1.
Console.WriteLine("Triangle 1: ");
tringle1.show();
tringle1.showStyle();
//setting Values for Triangle 2.
tringle2.width = 8.0;
tringle2.height = 12.0;
tringle2.style = "Right";
//Show Triangle 2.
Console.WriteLine("\nTriangle 2: ");
tringle2.show();
tringle2.showStyle();
Console.ReadKey();
}
}
}
Inheritance:
Triangle 1: Width = 4 | Height = 4 Triangle is: Isosceles Triangle 2: Width = 8 | Height = 12 Triangle is: Right
In Above Example, We create Two Classes: a Base Class and Derived Class. The Base Class define the Attributes of two-dimensional shape such as Square, Rectangle, and Triangle, and so on. the derived class inherit the Properties of Base class and create a two-dimensional shape that is Triangle. this is very simple and conceptual example to understand the concept of Inheritance.
Member Access and Inheritance:
A Derived Class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member function of derived classes should be declared private in the base class. We can summarize the different access types according to who can access them, in the following way:
Access | Public | Protected | Private |
---|---|---|---|
Same Class | Yes | Yes | Yes |
Derived Class | Yes | Yes | No |
Outside Class | Yes | No | No |
Base Keyword:
The Base Keyword is Used when we want to access members and methods of the base class, from within a derived class. This keyword serves two essential purposes.
» This keyword call a method on the base class that has been overridden by another method.
» Specify which base class constructor should be called when creating instance of the derived class.
Let's have an Example to understand the concept of base keyword. In this Example, we will create two classes and the both classes will use the same method website() . and by using the keyword base it is possible to call the website() method in the base class from within the derived class.
/*Example: Inheritance - base keyword- : InfoBrother: */
using System;
namespace Inheritance
{
class base_class //base Class:
{
protected string web = "www.";
protected string name = "InfoBrother";
protected string domain = ".com";
public virtual void website() //virtual Method:
{
Console.WriteLine(web + name + domain);
}
}
class derived_class : base_class //Derived Class:
{
public override void website() //overridden method:
{
Console.WriteLine("for More Information visit-");
base.website(); //calling method Using "base" Keyword:
}
}
class Program //main class.
{
static void Main(string[] args)
{
derived_class obj = new derived_class(); //object:
obj.website(); //method calling:
Console.ReadKey();
}
}
}
"Base" Keyword:
for More Information visit-
www.InfoBrother.com