base class field
using System; public class Animal { public string color = "white"; } public class Dog:Animal{ string color="black"; public void showColor() { Console.WriteLine(base.color); Console.WriteLine(color); } } namespace baseKeyword { public class TestBase { static void Main(string[] args) { Dog d=new Dog(); d.showColor(); Console.ReadKey(); } } }
//base class method
using System; namespace BaseClassMethod { public class Animal { public virtual void eat() { Console.WriteLine("eat"); } } public class Dog : Animal { public override void eat() { base.eat(); Console.WriteLine("eating bread!"); } } class Program { static void Main(string[] args) { Dog d = new Dog(); d.eat(); Console.ReadKey(); } } }
//calling base class constructor internally
using System; namespace base_class_constructor_internally { public class Animal { public Animal() { Console.WriteLine("Animal"); } } public class Dog : Animal { public Dog() { Console.WriteLine("Dog..."); } } class Program { static void Main(string[] args) { Dog d=new Dog(); Console.ReadLine(); } } }
//sealed class using System; namespace Sealed { sealed public class Animal { public void eat() { Console.WriteLine("Eating..."); } } public class Dog : Animal { public void bark() { Console.WriteLine("Barking...."); } } public class TestSealed { static void Main(string[] args) { Dog d=new Dog(); d.bark(); d.eat(); Console.ReadLine(); } } }
using System; namespace SealedMethod { public class Animal { public virtual void eat() { Console.WriteLine("eating.."); } public virtual void run() { Console.WriteLine("running..."); } } public class Dog : Animal { public void eat() { Console.WriteLine("Eating bread..."); } public sealed void run() { Console.WriteLine("Running very fast"); } } public class BabyDog : Dog { public override void eat() { Console.WriteLine("Eating biscuits"); } public override void run() { Console.WriteLine("Running slowly"); } } class Program { static void Main(string[] args) { BabyDog d=new BabyDog(); d.eat(); d.run(); Console.ReadKey(); } } }
Sealed variable not possible:
using System; namespace SealedMethod { public class Animal { public virtual void eat() { Console.WriteLine("eating.."); } public virtual void run() { Console.WriteLine("running..."); } } public class Dog : Animal { public void eat() { Console.WriteLine("Eating bread..."); } public void run() { Console.WriteLine("Running very fast"); } } public class BabyDog : Dog { public override void eat() { Console.WriteLine("Eating biscuits"); } public override void run() { Console.WriteLine("Running slowly"); } } class Program { static void Main(string[] args) { BabyDog d=new BabyDog(); d.eat(); d.run(); sealed int x=10; Console.WriteLine(x); Console.ReadKey(); } } }