Fundaments of Game Design

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Advertisements

CS 211 Inheritance AAA.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Object-Oriented Programming: Inheritance
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Object Oriented Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Object Oriented Concepts Classes II. Object Oriented concepts Encapsulation Composition Inheritance Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Object Oriented Programming
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Object-Oriented Programming: Inheritance and Polymorphism.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Object-Oriented Programming Concepts
Classes (Part 1) Lecture 3
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Classes and Objects
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Object-Oriented Programming: Inheritance
Chapter 3: Using Methods, Classes, and Objects
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Lecture 23 Polymorphism Richard Gesick.
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Abstract Classes AKEEL AHMED.
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Object-Oriented Programming: Inheritance
Lecture 14- Abstract Classes
Java – Inheritance.
Object-Oriented Programming: Inheritance
Java Inheritance.
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Lecture 6 Inheritance CSE /26/2018.
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Object-Oriented Programming: Inheritance
Chapter 8 Inheritance Part 2.
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Object-Oriented Programming: Inheritance
Presentation transcript:

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. 3

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

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

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

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.

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

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

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

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 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.

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.