Free mag vol1 | Page 248

CHAPTER 5  UNDERSTANDING ENCAPSULATION // Static classes can only // contain static members! static class TimeUtilClass { public static void PrintTime() { Console.WriteLine(DateTime.Now.ToShortTimeString()); } } public static void PrintDate() { Console.WriteLine(DateTime.Today.ToShortDateString()); } Given that this class has been defined with the static keyword, you cannot create an instance of TimeUtilClass using the new keyword. Rather, all functionality is exposed from the class level: static void Main(string[] args) { Console.WriteLine("***** Fun with Static Classes *****\n"); // This is just fine. TimeUtilClass.PrintDate(); TimeUtilClass.PrintTime(); // Compiler error! Can't create static classes! TimeUtilClass u = new TimeUtilClass (); } Console.ReadLine(); At this point in the chapter, you hopefully feel comfortable defining simple class types containing constructors, fields, and various static (and nonstatic) members. Now that you have the basics of class construction under your belt, you can formally investigate the three pillars of object-oriented programming.  Source Code The SimpleUtilityClass project is located under the Chapter 5 subdirectory. Defining the Pillars of OOP All object-oriented languages (C#, Java, C++, Visual Basic, etc.) must contend with three core principals, often called the pillars of object-oriented programming (OOP): 184 • Encapsulation: How does this language hide an object’s internal implementation details and preserve data integrity? • Inheritance: How does this language promote code reuse? • Polymorphism: How does this language let you treat related objects in a similar way?