Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Calling Methods of the Child Class to the parent Abstract Class!

Discussion in 'Scripting' started by hikariakio, Sep 19, 2019.

  1. hikariakio

    hikariakio

    Joined:
    Dec 19, 2016
    Posts:
    25
    Hello, I cannot even quite explain what I want. Well, but.....


    IMG_1067.JPG

    For example ,I have an abstract class called Enemy and another two Enemy classes (EnemyA and EnemyB) that inherits from Base Enemy class. Each A and B class has its different algorithm method to find a Direction to move and Moving method is in the Base Enemy Class. I want to use Children Classes's functions in the Base Abstract Class. What I am doing now is the normal way; using an Abstract Class Method in the Child Classes as usual.But I want to know if it is possible or not :3 (Or if I am doing wrong)

    Cheers xD
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    First of all, it's not possible to use a child funtion in a parent. I mean, how would the compiler know what to do? If an Enemy does not have a FindDir() method, then how is the compiler supposed to know what to do in that situation? ;)

    That said, you can just implement a FindDir() method in the abstract Enemy class without adding any body, forcing each child to implement their own version. To do that, just make the method abstract as well. That way Enemy has a FindDir() method, but Enemy A and Enemy B each implement their own version of that. If you had 3 Enemy types, and 2 of them shared the same FindDir() method (thus it beind the "default") you could also just implement that into Enemy directly, and make the one that needs a different version of FindDir() override it.

    Hope this clears all your questions. If not or i missed something, feel free to ask again.
     
    hikariakio likes this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Yeah, that's just basic inheritance, assuming you define FindDir in Enemy:
    Code (csharp):
    1. public abstract class Enemy : MonoBehaviour {
    2. public abstract Vector3 FindDir();
    3. }
    4.  
    5. public class EnemyA : Enemy {
    6. public override Vector3 FindDir() {
    7. return whatever;
    8. }
    9. }
    10.  
    11. .....
    12. Enemy en1 = GetComponent<EnemyA>();
    13. Debug.Log("Moving in direction " + en1.FindDir() );
     
    hikariakio likes this.
  4. hikariakio

    hikariakio

    Joined:
    Dec 19, 2016
    Posts:
    25
    Oh yeahhh!!! I got it... It's my first time using Abstract or whatsoever in C# real projects and it makes me confused. Now, I understand some how though I cannot still explain well it verbally. Thanks you two. :p
     
  5. SebasSBM

    SebasSBM

    Joined:
    Jul 30, 2016
    Posts:
    14
    I have similar problem. I am trying to use polymorfism in my code's structure, so I made (similarly) an abstract Base class extended by two classes (lets call them ChildA and ChildB). So, I forced this childs to follow IWork interface (names for example perhaps not too elegant, but please bear with me)...

    The first two childs work fine with two of the methods of the interface:

    * getStep (it's supposed to return an array of objects)
    * getBranchSteps (uses getStep recursively to find a target)

    The code works fine. The problem is that the code in getBranchSteps is the same in both child classes. So i tried shifting up getBranchSteps to the superclass (the abstract Base class)... it compiles, but it doesn't behave as expected anymore... perhaps it is because `getStep` is just abstract in parent class? I don't know what to do about it...

    ...do I have any hope to fix this refactoring attempt?

    *EDIT:* Nevermind :). For some reason, removing the prefix `this.` from `getStep` calls fixed the issue. (Well, I like to use `this` a lot, specially with wild named properties and methods, because I liked Python's better explicit than implicit philosofy, so I use it in C# as well... have a nice day блин
     
    Last edited: Apr 8, 2020