0% found this document useful (0 votes)
39 views7 pages

C# Constructors and Inheritance Examples

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)
39 views7 pages

C# Constructors and Inheritance Examples

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

Constructors in C#

[Link] constructor :

//default constructor using


System;

class demo
{
public demo()
{
[Link]("default Constructor");
}
public void sum_values(int a, int b)
{
[Link](a+b);
}
public static void Main(string[] args)
{
demo d=new demo();
int x=Convert.ToInt32([Link]());
int y=Convert.ToInt32([Link]());
d.sum_values(x,y);
}
}

Output :
default Constructor
5

7
2. Parameterized constructor :

//parameterized constructor using


System;

class demo
{
public demo(int a, int b)
{
[Link]("sum = "+(a+b));
}

public static void Main(string[] args)


{
demo d=new demo(1,2);

}
}

Output :
sum = 3
3. Private Constructor :

In inheritance this private constructor is not used in child class because


it is private

//private constructor using


System;

class demo
{
private demo()
{
[Link]("This is private constructor");
}

public static void Main(string[] args)


{
demo d=new demo();

}
}

Output :
This is private constructor
[Link] Constrcutor :
The constructor is only executed once

//static constructor using


System;

class demo
{
static demo()
{
[Link]("This is static constructor");
}
public void print_hii_method()
{
[Link]("Hii");
}

public static void Main(string[] args)


{
demo d=new demo();
d.print_hii_method();
demo c=new demo();
c.print_hii_method();
}
}
Output :
This is static constructor
Hii
Hii
(Here the “This is static constructor “ is only executed once even two objects is created
because the constructor is static).
EXAMPLES OF ALL TYPES OF INHERITANCES

[Link] inheritance

//single inheritance

using System;

public class parent


{
public int a =5;
public int b = 2;
}
public class child : parent
{
public void sum_(int x, int y)
{
[Link]("Sum is = "+(x+y));
}

public static void Main(string[] args)


{
child c = new child();
c.sum_(c.a, c.b);
}
}

Output :

sum is 7
2. MultiLevel Inheritance

using System;

public class product


{
public void one()
{
[Link]("This is product class");
}
}
public class laptop : product
{
public void two()
{
[Link]("This is laptop class");
}
}
public class hp : laptop
{
public void three()
{
[Link]("This is hp class ");
}

public static void Main(string[] args)


{
hp shop=new hp();
[Link]();
[Link]();
[Link]();
}
}
Output:
This is product class
This is laptop class
This is hp class
3. Hierarchical inheritance
//hierarichal inheritance
using System;
public class product
{
public void one()
{
[Link]("this is product class");
}
}
public class grocery : product
{
public void two()
{
[Link]("this is grocery class");
}
}
public class clothing : product
{
public void three()
{
[Link]("this is clothing class");
}
}
class any
{
public static void Main(string[] args)
{
clothing b = new clothing();
[Link]();
[Link]();
[Link]("********");
grocery a = new grocery();
[Link]();
[Link]();
}
}
Output :
this is product class
this is clothing class
********
this is product class
this is grocery class

You might also like