1

Hey I have being looking a lot and watching a lot of videos about inheritance. There is just this concept of abstract classes which means that it cant be implemented.

I get that you cant use a abstract class on a gameobject but what is the force of using a abstract class? Also can abstract method only be implemented in a abstract class, and why cant you implement the method in a abstract class but only override it?

I also read something about a abstract method taking a component<T> and I havent found out what the purpase of that was hope some of you clever minds can help a confused programmer :D

Jerry
  • 43
  • 6
  • The thing using `` is called a "generic". Google that and there are lots of examples and explenations – derHugo Jan 08 '19 at 17:44
  • You also **can** implement methods in an abstract class! Only those using the `abstract` keyword e.g. `abstract void DoSomething();` you can not implement directly. – derHugo Jan 08 '19 at 17:45
  • abstract classes **can** or better said shall be implemented .. that's the whole purpose of them. What you can not do is Instantiate them – derHugo Jan 08 '19 at 17:47
  • Possible duplicate of [When to use abstract classes?](https://stackoverflow.com/questions/2570814/when-to-use-abstract-classes) – Draco18s no longer trusts SE Jan 08 '19 at 18:32

2 Answers2

2

Abstract classes are used to model a shared "master class" which will never be used itself but derived classes of it will use that data/functions.

For example say your inheritance is

     Animal

Bird Mammal Reptile

In your game, you will only ever use a Bird/Mammal/Reptile since those are concrete models of animals, But they all will share at least some common amount of code. Health, Hunger, and may provide some abstract functions such as Move() which will be implemented on a per subclass level.

From an engineering standpoint, this let's you do fancy things such as create an array/vector of type Animal which can contain birds, mammals, and reptiles.

So say we have a vector of type Animal with 1 of each subclass.

We can then do

foreach (Animal a in animals)
   a.Move(100)

Birds/Mammals/Reptiles all move differently, but because we have that abstract function with no implementation at the base level, our code can guarantee that at some point in the inheritance tree it is implemented.

Other popular examples are an abstract class of Item and the abstract function item.Use(); Item could be a Potion, a Scroll, an apple (class Food). It doesn't matter because they all share that same base level interface.

Prodigle
  • 1,757
  • 12
  • 23
  • 1
    thanks I tried to make a simple inheritance script using these concepts. It was a eyeopener I understand more the some of the power of OOP now :D – Jerry Jan 08 '19 at 18:38
  • :) And your syntax is another concept called Generics, functions that can take in any type . You've already used them if you've used a vector since you write it as Vector / Vector . The "<>" brackets indicate a generic class/function and you fill in the space with the type you want. – Prodigle Jan 09 '19 at 09:02
0

I think reading Microsoft's article on Abstract Classes and Methods would be extremely helpful. You utilize abstract classes by providing base functionality to a class and inheriting from it.

You're also confusing some wording with implement and override: you implement an abstract method by overriding it.

Component<T> means that the class Component with the type parameter as something you define. This is defined in C# as Generics. You utilize these with base Unity3D classes like Component<GameObject> or GetComponent<GameObject>.

Eliasar
  • 1,067
  • 1
  • 7
  • 18