Inheritance Basics C# with Example



Inheritance Basics 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)
		{
			//this.hour = hour;		Error: Cannot Access Private Property  Directly
			//this.minute = minute; Error: Cannot Access Private Property Directly
			SetTime(hour, minute);
			this.second = second;
		}
		
		public void DisplayNewTime()
		{
			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.DisplayNewTime();
			}
		}
	}
 

0 Comment's

Comment Form

Submit Comment