Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by GutoThomas · Mar 30, 2012 at 05:51 PM · interfaceabstract

What's the use of abstract classes and interfaces in a game?

Hey guys, what's up? I was studying about Abstract Classes and Interfaces recently. Ok, I know how to use and the sintax in C#, but I really don't know in what it could be used, talking about games. To understand something, I guess we need to know his applications, and that's a point that I couldn't reach with this two things. Someone could give some examples? I don't need any type of code or something, just some kind of example of use.

Thanks from now! Hope this kind of question is permitted here in UnityAnswers, and sorry if it is not.

Cheers!

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Mar 30, 2012 at 07:55 PM 3
Share

Yeah, sounds like a forum question to me. There is no clear answer to this. You could mark it as community wiki, but unlike Stack Overflow, we have a companion forum, so I would just recommend the forum.

avatar image GutoThomas · Mar 31, 2012 at 11:21 PM 0
Share

Thanks for the 2 replies. I really liked your explanation kromenak. Turned the things more clear now!

avatar image GutoThomas · Mar 31, 2012 at 11:38 PM 0
Share

Sorry for the newbie question, but, if I want to use something from a abstract class in my game I need at least 3 classes? One for the abstract, other for the implementation of the abstract methods and another in order to be able to instantiate this new script with the implemented methods or there's a easier way? Sorry if the question's a bit confusing. I'll explain better if you guys don't understand it.

Thanks from now!

avatar image kromenak · Apr 01, 2012 at 05:17 PM 1
Share

Yeah, you will probably need at least three classes, as you said. In that third script where you instantiate, you'll just need to either "new" the subclass or do AddComponent if it is a monobehavior.

avatar image CHPedersen · Apr 01, 2012 at 06:48 PM 2
Share

Unless the abstract class inherits from $$anonymous$$onoBehavior. ;) Then, when you inherit from it, the child class can become the script.

I also like your description, kromenak. You've pointed out a lot of the classical ideas behind polymorphism and inheritance. The only thing I have to add is that another difference between interfaces and abstract classes in C# is that, unlike C++ for example, C# doesn't support multiple inheritance. This means that if you want to bind your class to multiple contracts, you need to use interfaces ins$$anonymous$$d of abstract classes, because you can inherit from just one class at a time, but you can implement an arbitrary amount of interfaces. In the case of a weapon, you might split method declarations for methods that reload the weapon and methods that fire the weapon into two different interfaces. Then you can implement a child class that fires and reloads (implements both the IFirable and IReloadable interfaces), and another child class that fires, but doesn't reload. That could be a machine gun and a grenade, for example. (It doesn't really make sense to "reload" a grenade).

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
28
Best Answer

Answer by kromenak · Mar 30, 2012 at 10:08 PM

Using abstract classes and interfaces in games is much the same as using them in other applications. They allow for convenience and simplification when used correctly, but they can also silently add unneeded complexity.

Abstract classes and interfaces can be pretty similar, but they also have differences. An abstract class can actually have functionality in it, which child classes take advantage of. An interface only acts as a contract for the functions in a class, so no functionality in interfaces at all.

In our game, we use abstract classes when we have multiple items that share some base functionality, but that base functionality is not enough to be an object itself. For example, a Weapon class can contain functionality that is common to a crossbow and shotgun, but an instance of the Weapon class is pretty useless by itself - so making it abstract might be a good idea.

Interfaces are useful when you want to refer to several disparate objects by a common attribute - this makes it possible to put things into lists and iterate through them, even if they are very different classes. For example, a Robot and a Crate are very different objects in a game, but if both implement a Breakable interface, they can be considered equal objects, at least in the respect that they are both breakable. You can then define a

  List Breakable

for example, and keep track of a lot of breakable things.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mahdiii · Jul 27, 2016 at 01:07 PM 1
Share

I say, as far as possible,split your codes to bunch of components. For example suppose you need the reload function. You know that all of weapons need to reload so you write an abstract class "weaponsystem" and an abstract method reload,then you write several classes(S$$anonymous$$G,A$$anonymous$$47,$$anonymous$$16,G3) that implement the reload function.it is cool but suppose that G3 and A$$anonymous$$47 have the same reload function but $$anonymous$$16 and S$$anonymous$$G have the same one. what to do. You need to copy and paste reload codes for G3 and A$$anonymous$$47 (and $$anonymous$$16 and S$$anonymous$$G). It is not bad? surely it is bad and causes problems. now suppose you write an interface IReloadable and implement it in Reload1 and Reload2 class. So you can use Reload1 for S$$anonymous$$G and $$anonymous$$16 class and Reload2 for A$$anonymous$$47 and G3 easily. Component based procedures give you a power you can easily extend your codes and programs. Use abstract class or common class for inheritence only when you are sure you need them.

avatar image
1

Answer by hameed-ullah-jan · Oct 24, 2018 at 11:43 AM

an abstract class is the best way to remove duplication of extra code and restrict the user from creating its instance. I think @kromenak explained it very well.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

problem with interface and abstract class in unity 3 Answers

How to use of interfaces and inheritance if you have seperated scripts? 0 Answers

Is the inclusion of abstract classes and interfaces really needed in Unity Game development? 2 Answers

How to make a class selection system? 1 Answer

manipulate fields of class 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges