Tuesday, January 25, 2022

Multi-Level Inheritance in C#

 It is 2nd Part of  Inheritance in C# for Multi-Level

Generally, c# supports only single inheritance that means a class can only inherit from one base class. However, in c# the inheritance is transitive, and it allows you to define a hierarchical inheritance for a set of types, and it is called a multi-level inheritance.

 

For example, if class C is derived from class B, and class B is derived from class A, then class C inherits the members declared in both class B and class A.


public class A
{
// Implementation
}
public class B: A
{
// Implementation
}
public class C: B
{
// Implementation
}


If you observe the above code snippet, class C is derived from class B, and class B is derived from class A, then class C inherits the members declared in both class B and class A. This is how we can implement multi-level inheritance in our applications.


Multi-Level Inheritance Example

Following is the example of implementing multi-level inheritance in the c# programming language.

using System;

namespace ArjunInheritance
{
    public class A
    {
       public string Name;
       public void GetName()
       {
          Console.WriteLine("Name: {0}", Name);
       }
    }
    public class B: A
    {
       public string Location;
       public void GetLocation()
       {
          Console.WriteLine("Location: {0}", Location);
       }
    }
    public class C: B
    {
       public int Age;
       public void GetAge()
       {
          Console.WriteLine("Age: {0}", Age);
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
          C c = new C();
          c.Name = "Arjun Walmiki";
          c.Location = "Thane";
          c.Age = 35;
          c.GetName();
          c.GetLocation();
          c.GetAge();
          Console.WriteLine("\nPress Any Key to Exit..");
          Console.ReadLine();
       }
    }
}

If you observe the above example, we implemented three classes (ABC), and class C is derived from class B, and class B is derived from class A.

 

By implementing a multi-level inheritance, class C can inherit the members declared in class B and class A.

 

When you execute the above c# program, you will get the result as shown below.


Name : Arjun Walmiki

Location : Thane

Age : 35

Press Any Key to Exit..


Multiple Inheritance

As discussed, c# supports only single inheritance that means a class can only inherit from one base class. If we try to inherit a class from multiple base classes, then we will get compile-time errors.

 

For example, if class C is trying to inherit from Class A and B simultaneously, we will get a compile-time error because multiple inheritance is not allowed in c#.

public class A
{
// Implementation
}
public class B
{
// Implementation
}
public class C: A, B
{
// Implementation

}

If you observe the above code snippet, class C is trying to inherit properties from both classes A and B simultaneously, which will lead to compile-time errors like “Class C cannot have multiple classes: A and B”.

 

As discussed, multi-level inheritance is supported in c#, but multiple inheritance is not supported. If you want to implement multiple inheritance in c#, we can achieve this by using interfaces. In the next chapters, we will learn how to use interfaces to achieve multiple inheritance in a detailed manner. 

Inheritance in C#

 In c#, Inheritance is one of the primary concepts of object-oriented programming (OOP), and it is used to inherit the properties from one class (base) to another (child) class.

In c# inheritance, the class whose members are inherited is called a base (parentclass, and the class that inherits the members of the base (parent) class is called a derived (childclass.


Following is the syntax of implementing an inheritance

<access_modifier> class <base_class_name>
{
// Base class Implementation
}

<access_modifier> class <derived_class_name> : <base_class_name>
{
// Derived class implementation
}

Example:-

public class x
{
    public void GetDetails()
    {
       // Method implementation
    }
}
public class Y: X
{
    // your class implementation
}
class Program
{
   static void Main(string[] args)
   {
       Y y = new Y();
       y.GetDetails();
   }
}

If you observe the above example, we defined a class “X” with the method called “GetDetails” and the class “Y” is inheriting from class “X”. After that, we call a “GetDetails” method by using an instance of derived class “Y”.

Example 2:-

using System;

namespace ArjunInheritanceExample
{
    public class User
    {
       public string Name;
       private string Location;
       public User()
       {
          Console.WriteLine("Base Class Constructor");
       }
       public void GetUserInfo(string loc)
       {
          Location = loc;
          Console.WriteLine("Name: {0}", Name);
          Console.WriteLine("Location: {0}", Location);
       }
    }
    public class Details: User
    {
       public int Age;
       public Details()
       {
          Console.WriteLine("Child Class Constructor");
       }
       public void GetAge()
       {
          Console.WriteLine("Age: {0}", Age);
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
          Details d = new Details();
          d.Name = "Arjun Walmiki";
          // Compile Time Error
          //d.Location = "Thane";
          d.Age = 35;
          d.GetUserInfo("Thane");
          d.GetAge();
          Console.WriteLine("\nPress Any Key to Exit..");
          Console.ReadLine();
       }
    }
}

If you observe the above example, we defined a base class called “User” and inheriting all the user class properties into a derived class called “Details” and we are accessing all the members of the User class with an instance of the Details class.

 

If we uncomment the commented code, we will get a compile-time error because the Location property in the User class is defined with a private access modifier. The private members can be accessed only within the class.

 

When you execute the above c# program, you will get the result as shown below.


Base Class Constructor

Child Class Constructor

Name : Arjun Walmiki

Location : Thane

Age : 35

Press Any Key to Exit..


If you observe the above result, we are able to access all the properties of base class into child class based on our requirements.

How many data type are supporting to JSON ?

This article is dedicate to Atos employee During the interview he asked me this question.

"How many data type are  supporting  to JSON ?"

In JSON, values must be one of the following data types:

  • a string
  • a number
  • an object (JSON object)
  • an array
  • a boolean
  • null

JSON values cannot be one of the following data types:

  • a function
  • a date
  • undefined

JSON String, Number

Strings in JSON must be written in double quotes.

Example:- {"name":"John", "age":12}

JSON Objects

Values in JSON can be objects.

Example:-

 {
"employee":{"name":"John""age":30"city":"New York"}
}

Objects as values in JSON must follow the JSON syntax.

JSON Arrays

Values in JSON can be arrays.

Example:-

{
"employees":["John""Anna""Peter"]
}

JSON Blooleans

Values in JSON can be true/false. 

Example:-

{"sale":true}

JSON null

Values in JSON can be null.

Example:-

{"middlename":null}

Thursday, January 13, 2022

ng is not recognized as an internal or external command

 If you are new in angular and your facing below issue during the creation of project then your on right place.  

ng is not recognized as an internal or external command 

Let's follow my instruction and get out from issue.

Step 1) Create one folder anywhere on your system eg :- my folder (C:\~\Desktop\AngularApps).

Step 2) open  folder in CMD as below screen


Step 3) Run Command in cmd(npm install -g @angular/cli)




Now Create new project using => ng new [Your project name] e.g :- ng new myFirstProject.

Thank you!


Thursday, December 23, 2021

If you did not select routing option on the project make time so how can add.

 This solution to experience and fresher "If you did not select routing option on the project make time so how can add. "

The solution are as below :- Run below command.


ng generate module app-routing --flat --module=app


Thank you ! 



Wednesday, October 27, 2021

npm WARN npm npm does not support Node.js v14.15.4

 First you need to open your command line, and use " npm install -g npm@latest  or npm install " you'll get the error like this

C:\Users\yourname>npm install -g npm@latest
npm WARN npm npm does not support Node.js v14.7.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8, 9.
npm WARN npm You can find the latest version at https://nodejs.org/
npm ERR! cb.apply is not a function npm ERR! A complete log of this
run can be found in: npm ERR! 
C:\Users\BOMWALMIKIA\AppData\Roaming\npm-cache\_logs\2020-08 10T09_36_56_388Z-debug.log

Solution 

  • Go to the path where you can find the debug log (this file is found in your npm-cache folder) C:\Users\yourname\AppData\Roaming
  • Delete the NPM and NPM-Cache folder, but DO NOT reinstall node. Once deleted go back to your command line and re-use the command " npm install -g npm@latest "