Base Reference Derived Object C# with Example



Base Reference Derived Object 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("\nDerived Time Class");
				NewTime sonata = new NewTime();
				Console.Write("Time of SONATA: ");
				sonata.SetTime(10, 15, 30);
				sonata.DisplayTime();
			
				Console.WriteLine("\n\nBase Time Class");
				Time hmt = (Time)sonata;
				hmt.SetTime(12, 22);
				Console.Write("Time of HMT: ");
				hmt.DisplayTime();
				
				Console.WriteLine("\n\nAfter modifying Base Part of Derived NewTime");
				Console.Write("Time of SONATA: ");
				sonata.DisplayTime();
				
			}
		}
	}
 

0 Comment's

Comment Form

Submit Comment