Book Image

Learning Object-Oriented Programming

By : Gaston C. Hillar
Book Image

Learning Object-Oriented Programming

By: Gaston C. Hillar

Overview of this book

Table of Contents (16 chapters)
Learning Object-Oriented Programming
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Working with simple inheritance in C#


First, we will create a base class in C#. Then we will use simple inheritance to create subclasses and specialize behavior. We will override methods and overload comparison operators to be able to compare different instances of a specific class and its subclasses. We will take advantage of this polymorphism.

Creating classes that specialize behavior in C#

Now it is time to code all the classes in C#. The following lines show the code for the Animal abstract class in C#. The class declaration doesn't specify a base class; therefore, this class inherits from Object, specifically System.Object. System.Object is the base class for all the classes included in .NET Framework. The usage of the abstract keyword before class makes this class an abstract class that we cannot use to create instances:

public abstract class Animal
{
  protected virtual int NumberOfLegs { get { return 0; } }
  protected virtual int PairsOfEyes { get { return 0; } }

  public int Age...