cancel
Showing results for 
Search instead for 
Did you mean: 
Patrick_Sweeney
Star Contributor
Star Contributor

Inheritance is a well known pattern in the programming world where a class, called a derived class, can access members and properties that have been defined by the class that it inherits from, called the base class. In other words, inheritance allows a developer to reuse the same base code in multiple classes. 

Previously Unity Scripts and Unity Libraries were limited since inheritance could only be used by classes defined in the same script. For OnBase 16, support has been added to allow scripts and libraries to inherit from classes defined by referenced libraries and imported assemblies. 

Implementation

Support for inheritance does not require any additional implementation on the part of the Unity developer. You can use the same inheritance pattern that you’re already familiar with, the only difference being that you can now use it with classes defined by your referenced files. 

It should be mentioned that the files you wish to inherit from do need to be referenced. It won’t be possible to, for example, inherit a class from an imported assembly if the assembly is not added as a reference to the script/library that you’re working on.

Example

Now to take a look at what that means when we’re programming a script! We’ll first need to define the class that we wish to inherit from, the base class:

As the name implies it’s a very basic class. It’s worth noting that it defines the methods GetMessage() and DoubleNumber(), two methods that we’ll see again shortly. After the library we want to inherit from has been created, saved, and published it’s time to reference it in the script where we want to use it:

The next step is to define a local class that inherits from the class defined in our library.

Lastly we can use our new class to access the methods we defined in our base class, alongside any additional methods and objects we’ve added to our new class!

As can be seen from the code hints, the inheritance of BaseClassLibrary gives us access to the GetMessage() and DoubleNumber() methods defined below. And that’s all there is to it!

Specifics

Inheritance is a basic concept in programming: a base class provides its functionality to a derived class, allowing the base class methods and property definitions to be reused across multiple classes. Another way to word it is that a derived class is a specialized version of a base class, capable of extending the base class and adding functionality on to the basic framework. While basic in concept there are a few nuances that should be kept in mind when using inheritance, including the usage of private members, the abstract and virtual keywords, and base class comments.

For a wonderful breakdown on this design pillar in .NET land, Microsoft has written an article on the matter: Inheritance in C#.

This is not an exhaustive description of Inheritance, but rather a quick overview to help you avoid some possible pitfalls in its implementation. Further research into its usage, best practices, and examples is well recommended for any programmer looking to implement Inheritance into their programming strategy.

In Conclusion

The added support for Inheritance in referenced libraries and imported assemblies should be a welcome feature for programmers desiring the flexibility to reuse their code in more scripts and libraries. Aside from removing the busy work of re-programming logic you’ve already implemented elsewhere, reusing existing code promotes consistency in your solutions, reduces the possible points of failure, and results in less code that needs to be maintained. In other words inheritance has the potential to make your product more stable, modular, and easier to update.

That does it for this new feature, check back next week for some information about code analysis rules for scripting added to OnBase Studio.

If you have any questions or comments, be sure to leave them in the comment area below.