200 likes | 209 Views
Learn about the principles of inheritance, abstract classes, and interfaces in game design using Unity. Understand how inheritance allows for code reusability and creating specialized groups of objects. Explore base classes, derived classes, and the concept of polymorphism.
E N D
Fundaments of Game Design Unity and Inheritance Richard Gesick
Objectives • Inheritance • Abstract classes • Interfaces • Inheritance in Unity
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.
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-arelationship 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.
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.
6 Base Classes and Derived Classes The virtual, overrideand 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.
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. Base Classes and Derived Classes
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 protectedinternal members can be accessed by members of a base class, the derived classes and by any class in the same assembly.
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 thefields as well. This would propagate access to what should be private fields, and the benefits of informationhiding would be lost.
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.
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.
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.
Abstract Classes and Methods abstract property declarations have the form: public abstract ReturnTypeMyProperty { 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.
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.
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
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.
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.
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.
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.
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.