[Access Modifier] class DerivedClassName : BaseClassName
{
// Your code goes here.
}
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class Employee
{
public Employee()
{
this.ID = 0;
this.Name = "N/A";
}
public Employee(int id, string name)
{
this.ID = id;
this.Name = name;
}
public int ID { get;set; }
public string Name { get;set; }
public void getData()
{
Console.WriteLine("Enter the Employee ID");
ID = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the Employee Name");
Name =Console.ReadLine();
}
public void showData()
{
Console.WriteLine("\n Employee ID = "+ ID);
Console.WriteLine(" Employee Name = " + Name);
}
}
class Manager : Employee
{
public Manager()
{
Role="N/A";
}
public string Role { get; set; }
public void getRole()
{
Console.WriteLine("Enter the Employee Role");
Role = Console.ReadLine();
}
public void showRole()
{
Console.WriteLine(" Employee Role is " +Role);
}
}
class Program
{
static void Main(string[] args)
{
Manager obj = new Manager();
obj.getData();
obj.getRole();
obj.showData();
obj.showRole();
Console.ReadLine();
}
}
}
class BaseClase
{
protected int intVar;
…….
…….
}
Class DerivedClass : BaseClase
{
// intVar is protected here.
…….
…….
}
Class MostDerivedClass : DerivedClass
{
// intVar is protected here.
…….
…….
}
derivedClass-constructor (parameter-list) : base(arg-list)
{
// body of constructor
}
using System;
namespace ConsoleApplication1
{
class BaseClase
{
public BaseClase()
{
Console.WriteLine("Base Class constructor is called");
}
}
class DerivedClass : BaseClase
{
public DerivedClass()
{
Console.WriteLine("Derived Class constructor is called");
}
}
class MostDerivedClass : DerivedClass
{
public MostDerivedClass()
{
Console.WriteLine("MostDerived Class constructor is called");
}
}
class Program
{
static void Main(string[] args)
{
MostDerivedClass obj = new MostDerivedClass();
Console.ReadLine();
}
}
}
class MostDerivedClass : DerivedClass
{
public MostDerivedClass():base(int a)
{
// Constructor code
}
}
using System;
namespace ConsoleApplication1
{
class BaseClase
{
public int intVar=100;
}
class DerivedClass : BaseClase
{
new int intVar; //this intVar hides the intVar in BaseClass
public DerivedClass(int a)
{
intVar = a;
}
public void show()
{
Console.WriteLine("intVar from derived class and value =" + intVar);
// base.intVar will access the value of base class variable
Console.WriteLine("intVar from base class and value =" + base.intVar);
}
}
class Program
{
static void Main(string[] args)
{
DerivedClass obj = new DerivedClass(10);
obj.show();
Console.ReadLine();
}
}
}
sealed class BaseClase
{
// ………
}
//ERROR! Can't derive from class BaseClase.
// BaseClass is sealed class
class DerivedClass : BaseClase
{
// ………..
}
using System;
namespace ConsoleApplication1
{
class BaseClase
{
public virtual void show()
{
// Your implementation.
// ……….
}
}
class DerivedClass : BaseClase
{
sealed public override void show()
{
// Your implementation.
// ……….
}
}
class MostDerivedClass : DerivedClass
{
//Attempting to override show causes compiler error
sealed public override void show()
{
// Your implementation.
// ……….
}
}
class Program
{
static void Main(string[] args)
{
MostDerivedClass obj = new MostDerivedClass();
Console.ReadLine();
}
}
}