IsA and HasA Relationship in C#

IsA and HasA Relationship in C# with Examples

In this article, I am going to discuss IsA and HasA Relationship in C# with Examples. It is very important to understand the concept of IS-A and HAS-A relationship between classes. Please read our Inheritance in C# article before proceeding to this article.

IsA and HasA Relationship in C# with Examples:

Let us understand IS-A and HAS-A Relationship in C# with Examples. Please have a look at the below example.

class Rectangle{
          Some Data Members
          Some Member Functions
}

This is a class called Rectangle. Let us assume data we have some data members and member functions are there inside this class. Next, we have a class called Cuboid which is inheriting from the above Rectangle class as follows.

class Cuboid : Rectangle{
          Some Data Members
          Some Member Functions
}

This is the Cuboid class which is inheriting from the Rectangle class. Also, assume that this class also has some data members and member functions inside it. Now let us write one more class as follows.

class Table{
       Rectangle top;
       int legs;
}

This is the Table class which is not inherited from any class. This class has two data members that are of class Rectangle and integer type variable i.e. top and legs.

So first we have created a class called Rectangle. Then we created another class called Cuboid which is inherited from the Rectangle class and then we created another class called Table. Inside the Table class, we have created one variable of the type Rectangle as well as an integer type variable.

The class Cuboid is inherited from the Rectangle class. So, can we say that a Cuboid is a Rectangle? Yes. So, the relationship between the Rectangle class and Cuboid class is the ‘Is A’ relationship.

Next, our Table class is having a Table Top that is of the type Rectangular. The Table class is having an object of the Rectangle class. So, can we say that the Table class has a Rectangle? Yes, the Table class Has a Rectangle. So, the relationship between the Table class and the Rectangle class is the ‘Has A’ relationship.

So, we can use our class in two ways that are ‘Is A’ and ‘Has A’. This is common in object-oriented programming just not for C#, it is available in C++, Java, and other Object-Oriented Programming Language. 

So, a class in C# can be used in two ways. One is that a class can be derived means we can write child classes inheriting from that one. The second one is that object of that class can be used. So, there are two ways of using one class. Either you can create the object and use it or you can inherit it from that class.

Real-time Example to Understand Is-A and Has-A Relationship:

Let us understand Is-A and Has-A Relationship with one Real-time Example. Let us take an example. Please have a look at the below image.

Real-time Example to Understand Is A and Has A Relationship

Let us assume that the above image shows the design of my car. Now I can manufacture the car from the above design and I can sell it on the market. Let us assume we have manufactured the following cars from the above design.

Real-time Example to Understand Is A and Has A Relationship in C#

Manufacturing the Cars means, these are objects. Now I can sell my design (not the manufactured cars or objects, only the design) to your company and you can manufacture your car with some changes in the above design which I sell to you.

IsA and HasA Relationship in C# with Examples

So, what are the uses of design class?

Either we share it with the derived classes or create an object. If a class is inheriting from some other class, then it is having an ‘Is A’ relationship with that class or if a class is having an object of some other class, then it is having a ‘Has A’ relationship with that class. So, Table has a Rectangle, and a Cuboid is a Rectangle.

Next important thing is that a class can have different types of members, let us assume we have three types of members such as privateprotected, and public. So, these three types of members can be there inside a class.

Now when you have those three types of members then which members are accessible inside the class, which members are accessible in the class which is having ‘Is A’ relationship and the class which is having ‘Has A’ relationship? So, it means what is accessible inside the class and what is accessible in derived classes, and what is accessible upon an object. We already discussed this in our Access Specifiers article.

Example to Understand IS-A Relationship in C#

Please have a look at the below example. In the below example, the relation between Rectangle and Cuboid is IS-A relationship so we can say that Cuboid IS-A Rectangle.

using System;
namespace IsAHasADemo
{
    public class Program
    {
        static void Main()
        {
            Cuboid cuboid = new Cuboid(3, 5, 7);
            Console.WriteLine($"Volume is : {cuboid.Volume()}");
            Console.WriteLine($"Area is : {cuboid.Area()}");
            Console.WriteLine($"Perimeter is : {cuboid.Perimeter()}");
            
            Console.ReadKey();
        }
    }
    class Rectangle
    {
        public int Length;
        public int Breadth;
        public int Area()
        {
            return Length * Breadth;
        }
        public int Perimeter()
        {
            return 2 * (Length + Breadth);
        }
    }
    class Cuboid : Rectangle
    {

        public int Height;
        public Cuboid(int l, int b, int h)
        {
            Length = l;
            Breadth = b;
            Height = h;
        }
        public int Volume()
        {
            return Length * Breadth * Height;
        }
    }
}
Output:

Example to Understand IS-A Relationship in C#

Example to Understand HAS-A Relationship in C#

Please have a look at the below example. In the below example, within the Employee class, we have created one variable of Address type which is nothing but a HAS-A relationship between Employee and Address. So, we can say that Employee HAS-A Address.

using System;
namespace IsAHasADemo
{
    public class Program
    {
        static void Main()
        {
            Address address =new Address("B1-3029", "BBSR", "Odisha");
            Employee employee = new Employee(1001, "Ramesh", address);
            employee.Display();
            Console.ReadKey();
        }
    }
    class Address
    {
        public string AddressLine, City, State;
        public Address(string addressLine, string city, string state)
        {
            AddressLine = addressLine;
            City = city;
            State = state;
        }
    }
    class Employee
    {
        public Address address; //Employee HAS-A Address   
        public int Id;
        public string Name;
        public Employee(int id, string name, Address adrs)
        {
            Id = id;
            Name = name;
            address = adrs;
        }
        public void Display()
        {
            Console.WriteLine($"Employee Id: {Id}");
            Console.WriteLine($"Employee Name: {Name}");
            Console.WriteLine($"AddressLine: {address.AddressLine}");
            Console.WriteLine($"City: {address.City}");
            Console.WriteLine($"State: {address.State}");
        }
    }
}
Output:

Example to Understand HAS-A Relationship in C#

In the above example, if we inherit the Employee Class from the Address class, then it does not make any sense. Because if we inherit then it will establish an IS-A relationship between Employee and Address classes and it does not make any sense to say Employee IS-A Address rather if we implement HAS-A relationship then it makes sense to say Employee HAS-A Address.

How to Decide What to Implement between IS-A and HAS-A?

Ask the question yourself. For example, If I ask you the questions which statement gives you more sense from the below two statements.
Employee IS-A Address
Employee HAS-A Address

Then definitely, you will tell Employee HAS-A Address gives more sense than Employee IS-A Address. Now, again If I ask you, which statement gives you a better sense of the below two statements.
BMW IS-A Car
BMA HAS-A Car

Then definitely your answer will be BMW IS-A Car.

Note: In C#, the IS-A relationship is implemented using Inheritance and the HAS-A relationship is implemented using Composition i.e. declaring a variable.

In the next article, I am going to discuss Generalization and Specialization in C# with Examples. Here, in this article, I try to explain IsA and HasA Relationship in C# with Examples and I hope you enjoy this IS-A and HAS-A Relationship in C# article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.