0% found this document useful (0 votes)
11 views6 pages

C# Inheritance Examples: Single to Multiple

The document contains code examples demonstrating various types of inheritance in C#, including single, multilevel, hybrid, and multiple inheritance. Each section provides a class structure and methods to illustrate how inheritance allows for code reuse and organization. The examples include classes for rectangles, students, and family members, showcasing their properties and methods.

Uploaded by

devtvpurpose
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

C# Inheritance Examples: Single to Multiple

The document contains code examples demonstrating various types of inheritance in C#, including single, multilevel, hybrid, and multiple inheritance. Each section provides a class structure and methods to illustrate how inheritance allows for code reuse and organization. The examples include classes for rectangles, students, and family members, showcasing their properties and methods.

Uploaded by

devtvpurpose
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Single Inheritance

using System;
using [Link];
using [Link];
using [Link];
namespace single_inheritance
{
class Rectangle
{
protected double length, width;
public void getdata()
{
[Link]("Enter the length and width of the rectangle");
length = [Link]([Link]());
width = [Link]([Link]());
}
public double GetArea()
{
return length * width;
}
public void Display()
{
[Link]("Length:{0}",length);
[Link]("Width:{0}",width);
[Link]("Area:{0}", GetArea());
}
}// end class Rectangle
class Childclass : Rectangle
{
private double cost;
public double GetCost()
{
double cost;
cost = GetArea()*70;
return cost;
}
public void Display()
{
[Link]();
[Link]("Cost:{0}",GetCost());
}
}
class ExecuteSingleInheritance
{
static void Main(string[] args)
{
Childclass t =new Childclass();
[Link]();
[Link]();
[Link]();}}}
Multilevel Inheritance
using System;
using [Link];
using [Link];
using [Link];
namespace multilevelinheritanace
{
public class Person
{
protected string regno, name;
public void get()
{
[Link]("Multilevel Inheritance!");
[Link]("Enter the register number and name of a student :-");
regno = [Link]();
name = [Link]();
}
public virtual void display()
{
[Link]("Student register number - {0} and name is {1}", regno, name);
[Link]();
}
}
class Student : Person
{
public string Classincharge = "PSV";
public override void display()
{
[Link]();
[Link]("Class incharge of the Student is: {0}", Classincharge);
}
}
class Details : Student
{
private string StudentAddress = "BCA, PUCC";
public void display()
{
[Link]("Student Address: {0}", StudentAddress);
}
}
class TestClass
{
public static void Main()
{
Student s = new Student();
[Link](); [Link]();
Details d = new Details();
[Link]();
[Link]();
}
}
}
Hybrid Inheritance
using System;
using [Link];
using [Link];
using [Link];
namespace hybridinheritanace
{
class Father
{
public void home()
{
[Link]("Father's home");
}
public void Car()
{
[Link]("Father's Car");
}
}
class Son : Father
{
public void mobile()
{
[Link]("Son's mobile");
}
}
class Daughter : Father
{
public void purse()
{
[Link]("Daughter's purse");
}
}
public class TestHybridInheritance
{
public static void Main(String[] args)
{
Son s = new Son();
[Link]();
[Link]();
[Link]();
Daughter d = new Daughter();
[Link]();
[Link]();
[Link]();
[Link]();
} }}
Multiple Inheritance
using System;
using [Link];
using [Link];
using [Link];
namespace multipleinheritance
{
class Shape
{
protected int width,height;
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
}
public interface PaintCost //interface declarations
{
int getCost(int area);
}
class Rectangle : Shape, PaintCost
{
public int getArea()
{
return (width * height);
}
public int getCost(int area)
{
return area * 70;
}
}
class Test
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
int area;
[Link](5);
[Link](7);
area = [Link]();
[Link]("\t \t IMPLEMENTATION OF MULTIPLE INHERITANCE \n");
[Link]("Painting Details:- \n");
[Link]("Total area: {0}", [Link]());
[Link]("Total paint cost: ${0}", [Link](area));
[Link]();
}
}
}

Common questions

Powered by AI

In the provided code, single inheritance is demonstrated by the 'Childclass' inheriting from the 'Rectangle' class. The 'Childclass' is able to access the protected members 'length' and 'width' of the 'Rectangle' class, as well as its public methods. Additionally, 'Childclass' extends the functionality of 'Rectangle' by adding the 'GetCost' method which calculates cost based on the area, illustrating the typical way single inheritance is used to build upon base class functionality .

The 'PaintCost' interface in the 'Rectangle' class defines a contract for method 'getCost', enforcing any implementing class to provide an implementation. This enables 'Rectangle' to separate the calculation of area (a structural property) from the economic aspect of calculating cost, adhering to interface segregation principles. This role ensures that the class is focused on its geometric responsibilities while being flexibly extended to include financial computation functionalities without direct dependency or base class constraints .

The code demonstrates multiple inheritance in C# by having 'Rectangle' implement both a class 'Shape' and an interface 'PaintCost'. While C# does not support direct multiple inheritance, interfaces allow classes to inherit from multiple sources. 'Rectangle' inherits properties from 'Shape' and defines 'getArea', while also implementing 'getCost' as specified by 'PaintCost'. This allows 'Rectangle' to inherit structural data and behavior while providing interface-based flexibility, illustrating a solution to inheritance limitations in C# .

The code demonstrates encapsulation by using protected and private access modifiers for class members, such as length and width in 'Rectangle' and cost in 'Childclass'. These access modifiers limit direct access from outside, ensuring that only derived classes or members of the same class can interact with these fields. Coupled with inheritance, such as in the 'Childclass', this encapsulation can ensure data integrity while allowing access to necessary class behavior, but could be improved by providing validation in setters instead of direct field access .

Hybrid inheritance as shown in the code involves a combination of different types of inheritance: in this case, hierarchical inheritance as two classes 'Son' and 'Daughter' inherit from 'Father'. This setup enables both 'Son' and 'Daughter' to access methods of 'Father', while allowing individual behaviors like 'mobile' and 'purse'. This differs from single or multilevel inheritance by allowing shared base class functionality combined with specific functionalities for each child class .

The multilevel inheritance in the example allows for a hierarchical relationship between classes: 'Person', 'Student', and 'Details'. The main benefit is the reuse of code across different levels of hierarchy, allowing 'Student' to inherit from 'Person' and then 'Details' from 'Student'. This enables sharing of attributes and behaviors. However, potential limitations include increased complexity and harder debugging due to deep inheritance chains; changes to 'Person' might inadvertently affect 'Student' and 'Details', leading to maintenance challenges .

The 'Childclass' uses a fixed multiplication rate of 70 to determine cost based on area, which may lack scalability and flexibility as needs or rates change. Improvements could include parameterizing the rate value or using configuration files to store rate values, enhancing flexibility and scalability. Additionally, precision can be increased by ensuring that cost calculations account for currency representation accurately, using data types like decimal instead of double to avoid floating-point inaccuracies .

Potential issues with the 'Details' class include overshadowing the 'display' method without invoking 'base.display()', which could lead to incomplete data presentation unless explicitly called. Overshadowing might lead to confusion regarding which 'display' method is invoked. To mitigate this, always call 'base.display()' when overriding methods to ensure all superclass information is displayed, or alternatively, rename or clarify method intentions to prevent accidental overrides .

The use of protected members in the 'Rectangle' class ensures that 'length' and 'width' are only accessible within the class itself and its derived classes, which is 'Childclass' in this case. This access control allows extending the functionality by derived classes directly accessing these members while preventing access directly from outside. This control helps maintain integrity and abstraction in the class hierarchy, as external interference is minimized while allowing derived classes to build upon base class functionalities .

Overriding the 'display' method in 'Student' means providing a new implementation for the 'display' method that exists in 'Person', using the 'override' keyword, allowing both the base and derived implementations to be used. This allows 'Student' to extend 'Person's capabilities without losing its base functionality. On the other hand, overshadowing, as seen in 'Details', could replace or hide the 'display' method without the benefit of dynamic method invocation, possibly leading to loss of data cohesion if not managed carefully .

You might also like