Advertisement
//C# Example of no-argument constructor
using System;
class A
{
//A no-argument constructor of class A
//With public access modifier
public A()
{
Console.WriteLine("Constructor of class A is called");
}
public void Message()
{
Console.WriteLine("A");
}
}
class B
{
public static void Main(String[] ar)
{
//Calling a no-argument constructor of class A
A ob= new A();
//Calling the method Message() of class A
//by using its object
ob.Message();
}
}
Constructor of class A is called
A
//C# Although we can create a constructor with private access modifier
//But, a private constructor cannot be called outside the scope its class.
using System;
class A
{
private A()
{
Console.WriteLine("Constructor of A is called");
}
}
class B
{
public static void Main(String[] ar)
{
//The private constructor of class A is called outside its class, hence, compile error.
A ob = new A();
}
}
A.cs(19,13): error CS0122: 'A.A()' is inaccessible due to its protection level
Advertisement
//C# Although we can create a constructor with protected access modifier
//But, a protected constructor can only be called from a subclass of its class, by inheritance.
using System;
class A
{
protected A()
{
Console.WriteLine("Constructor of A is called");
}
}
//class B is inheriting from class A
class B:A
{
public static void Main(String[] ar)
{
//We cannot directly call the protected constructor of a class with its subclass.
//A ob = new A(); --> This, if uncommented will issue a compile error
//The protected constructor of class A is called outside its class, by inheritance
//Calling a subclass no-argument constructor, automatically calls the superclass no-argument constructor
B ob = new B();
}
}
Constructor of A is called
//C# Calling the default no-argument constructor
using System;
class A
{
//Defining a method Message() of class A
public void Message()
{
Console.WriteLine("A");
}
}
class B
{
public static void Main(String[] ar)
{
//Calling the default no-argument constructor of class A, outside its class A
//Which is fine, because its access modifier is by default, public
A ob = new A();
//Calling the method Message()
//By using the object of class A
ob.Message();
}
}
A
//C# Example of a parameterized constructor
using System;
class Names
{
//Instance variables
String firstName;
String secondName;
//A parameterized constructor
//with public access modifier
public Names(String firstN, String secondN)
{
firstName=firstN;
secondName=secondN;
}
//A method of class Names
public void Comedian()
{
Console.WriteLine(firstName + " " + secondName);
}
}
class D
{
public static void Main(String[] ar)
{
//Calling the parameterized constructor of class Names
//with two String values to create an object of class Nmaes
Names ob= new Names("Charlie","Chaplin");
//Calling the method Comedian() of class Names
ob.Comedian();
}
}
Charlie Chaplin
//C# Example of a class with overloaded constructors i.e. different versions of a constructor
using System;
class MathOP
{
//Constructor to add two int values
public MathOP(int a, int b)
{
Console.WriteLine(a+b);
}
//Constructor to add two String objects
public MathOP(String a, String b)
{
Console.WriteLine(a+b);
}
//Constructor to add two float values
public MathOP(float a, float b)
{
Console.WriteLine(a+b);
}
//Constructor to add two char values
public MathOP(char a, char b)
{
Console.WriteLine((char)(a+b));
}
}
class Cons
{
public static void Main(String[] ar)
{
//Calling the constructor version which takes two int values
MathOP ob1= new MathOP(1,2);
//Calling the constructor version which takes two String values
MathOP ob2= new MathOP("John","Denver");
//Calling the constructor version which takes two float values
MathOP ob3= new MathOP(2.2f, 2.5f);
//Calling the constructor version which takes two char values
MathOP ob4= new MathOP('a', 'x');
}
}
3
JohnDenver
4.7
U
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement