Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundaments of Game Design

Similar presentations


Presentation on theme: "Fundaments of Game Design"— Presentation transcript:

1 Fundaments of Game Design
Unity and Inheritance Richard Gesick

2 Objectives Inheritance Abstract classes Interfaces
Inheritance in Unity

3 3 Introduction Inheritance allows a new class to absorb an existing class’s members. A derived class normally adds its own fields and methods to represent a more specialized group of objects. Inheritance saves time by reusing proven and debugged high-quality software. 3

4 4 Introduction The direct base class is the base class which the derived class explicitly inherits. An indirect base class is any class above the direct base class in the class hierarchy. The class hierarchy begins with class object. The is-a relationship represents inheritance. For example, a car is a vehicle, and a truck is a vehicle. New classes can inherit from thousands of pre-built classes in class libraries. 4

5 Base Classes and Derived Classes
5 Base Classes and Derived Classes Objects of all classes that extend a common base class can be treated as objects of that base class. However, base-class objects cannot be treated as objects of their derived classes. When a derived class needs a customized version of an inherited method, the derived class can override the base-class method as long as that method is marked virtual, abstract or override. 5

6 Base Classes and Derived Classes
6 Base Classes and Derived Classes The virtual, override and abstract keywords indicate that a base-class method can be overridden in derived classes. The override modifier declares that a derived-class method overrides a virtual or abstract base-class method. This modifier also implicitly declares the derived-class method virtual. 6

7 Base Classes and Derived Classes
To override a base-class method, a derived class must declare a method with keyword override. The method must have the same signature (method name, number of parameters and parameter types) and return type as the base-class method It is a compilation error to override a method with a different access modifier. If a public method could be overridden as a protected or private method, the derived-class objects would not respond to the same method calls as base-class objects.

8 private and protected Members
8  private and protected Members A base class’s private members are inherited by derived classes, but are not directly accessible by derived-class methods and properties. A base class’s protected members can be accessed by members of that base class and by members of its derived classes. A base class’s protected internal members can be accessed by members of a base class, the derived classes and by any class in the same assembly. 8

9 private and protected Members
9 private and protected Members A derived class can change the state of private base-class fields only through non-private methods and properties provided in the base class. If a derived class can access its base class’s private fields, classes that inherit from that base class could access the fields as well. This would propagate access to what should be private fields, and the benefits of information hiding would be lost. 9

10 Base Classes and Derived Classes
10 Base Classes and Derived Classes Using protected instance variables creates several potential problems. The derived-class object can set an inherited variable’s value directly without validity checking. Derived-class methods would need to be written to depend on the base class’s data implementation. You should be able to change the base-class implementation while still providing the same services to the derived classes. Declaring base-class instance variables private enables the base-class implementation of these instance variables to change without affecting derived-class implementations. 10

11 Abstract Classes and Methods
Abstract classes, or abstract base classes cannot be used to instantiate objects. Abstract base classes are too general to create real objects—they specify only what is common among derived classes. Classes that can be used to instantiate objects are called concrete classes. Concrete classes provide the specifics that make it reasonable to instantiate objects.

12 Abstract Classes and Methods
An abstract class normally contains one or more abstract methods, which have the keyword abstract in their declaration. A class that contains abstract methods must be declared as an abstract class even if it contains concrete (non-abstract) methods. Abstract methods do not provide implementations. Constructors and static methods cannot be declared abstract.

13 Abstract Classes and Methods
abstract property declarations have the form: public abstract ReturnType MyProperty { get; set; } An abstract property may omit implementations for the get accessor, the set accessor or both. Concrete derived classes must provide implementations for every accessor declared in the abstract property.

14 Abstract Classes and Methods
We can use abstract base classes to declare variables that can hold references to objects of any concrete classes derived from those abstract classes. You can use such variables to manipulate derived-class objects polymorphically and to invoke static methods declared in those abstract base classes.

15 Common Errors with Abstract Classes
Attempting to instantiate an object of an abstract class is a compilation error. Failure to implement a base class’s abstract methods and properties in a derived class is a compilation error unless the derived class is also declared abstract

16 Creating and Using Interfaces
Interfaces define and standardize the ways in systems can interact with one another. A C# interface describes a set of methods that can be called on an object—to tell it, for example, to perform some task or return some piece of information. An interface declaration begins with the keyword interface and can contain only abstract methods, properties, indexers and events. All interface members are implicitly declared both public and abstract. An interface can extend one or more other interfaces to create a more elaborate interface that other classes can implement.

17 Creating and Using Interfaces
An interface is typically used when unrelated classes need to share common methods so that they can be processed polymorphically You can create an interface that describes the desired functionality, then implement this interface in any classes requiring that functionality. Like abstract classes, interfaces are typically public types, so they are normally declared in files by themselves with the same name as the interface and the .cs file-name extension.

18 Creating and Using Interfaces
C# does not allow derived classes to inherit from more than one base class, but it does allow a class to implement any number of interfaces. To implement more than one interface, use a comma-separated list of interface names after the colon (:) in the class declaration. When a class inherits from a base class and implements one or more interfaces, the class declaration must list the base-class name before any interface names.

19 Using Inheritance in Unity
As we’ve seen before, Unity children inherit the transform of the parent The default when creating a script has the script inheriting from monobehavior In scripts that inherit from monobehavior, the classical constructors cause problems. Start and Awake methods that the place of the constructor for initializing variables and objects.

20 Using Inheritance in Unity
Unity doesn’t let an abstract class be assigned to a game object. So, how do we use an abstract class? A couple of approaches could be used. Have the base class inherit from monobehavior and then the subclasses will inherit it indirectly and still be able to use update, etc. Or don’t inherit monobehavior at all and create a manager to create, move and otherwise control the subclasses. In this case the constructors will need to be used to initialize variables.


Download ppt "Fundaments of Game Design"

Similar presentations


Ads by Google