Accessing Base Class Member In Masked Derived Class C# with Example



Accessing Base Class Member In Masked Derived Class C# with Example

 	using System;

	namespace CSharpInheritance{
	class Time
	{
		public byte hour{set; get;}	
		public byte minute{set; get;}
	
		public void SetTime(byte hour, byte minute)
		{
			this.hour = hour;
			this.minute = minute;
		}
		
		public void DisplayTime()
		{
			Console.Write(hour + ":" + minute);
		}
	}
	
	class NewTime : Time
	{
		public byte second{set; get;}
	
		public void SetTime(byte hour, byte minute, byte second)
		{
			SetTime(hour, minute);
			this.second = second;
		}
		
		new public void DisplayTime()
		{
			base.DisplayTime();
			Console.Write(":" + second);
		}
	}
		
		class UsingTime{
			static void Main(String[] arg){
			
				Console.WriteLine("Base Time Class");
				Time hmt = new Time();
				hmt.SetTime(12, 22);
				Console.Write("Time of HMT: ");
				hmt.DisplayTime();
				
				Console.WriteLine("\n\nDerived Time Class");
				NewTime sonata = new NewTime();
				Console.Write("Time of SONATA: ");
				sonata.SetTime(10, 15, 30);
				sonata.DisplayTime();
			}
		}
	}
 

0 Comment's

Comment Form