Chapter 2. Classes and Generics

Classes form the building blocks of software development and are essential in building good code. In this chapter, we will be looking at classes and generics and why we need to use them. The recipes we will be covering are going to be as follows:

  • Creating and implementing an abstract class
  • Creating and implementing an interface
  • Creating and using a generic class or method
  • Creating and using a generic interface

Introduction

As you probably know, classes are simply containers for related methods and properties to describe some object in your software. An object is an instance of a specific class and, sometimes, mimics real-world things. When thinking of a car, you might create a vehicle class that contains certain attributes (properties) that all vehicles contain, such as automatic or manual transmission, wheel count (not all vehicles have only four wheels), or fuel type.

When we create an instance of the vehicle class, we can create a car object, an SUV object, and so on. Here lies the power of classes, which is to describe the world around us and translate it into a programming language that a compiler can understand.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.15.43