R
R
r3xetdeus2019-07-15 15:58:11
OOP
r3xetdeus, 2019-07-15 15:58:11

How to inherit from 2 abstract C# classes?

Good afternoon. I'm just learning C #, I got an interesting problem, please tell me how to solve it.
There are 2 interfaces that are inherited in abstract classes (cars and planes). The task is to describe the 3rd abstract class that inherits 2 classes (Ilona Mask's machine). Let's say there is multiple inheritance in C++, but how to do it in c#? The code is described below, thanks in advance.

interface ICar
    {
        void Move();
    }
    interface IPlane
    {
 
        void Fly();
    }

    abstract class StandartCar: ICar
    {   
        public int wheel { get; set; }

        public int doors  { get; set; }        

        public  void Move()
        {
            Console.WriteLine("60 km/h");
        }
    }

    abstract class StandartPlane: IPlane
    {
        public int wings { get; set;  }
        public void Fly()
        {
            Console.WriteLine("500 km/h");
        }

    }

    abstract class MaskCar: ICar, IPlane
    {

    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Romashkan, 2019-07-15
@EvgeniiR

The task is to describe 3 abstract classes that inherit 2 classes

The task was originally set incorrectly. In itself, the inheritance of two abstract classes will not bring any practical benefit, and does not provide any unique functionality that cannot be implemented through inheritance.
If someone gives you such puzzles under the guise of "tasks for OOP" - you need to send it to the forest. All these inheritances of Customer from User, User from Human are not needed in practice in 99% of cases, because if the module conditionally wants a Buyer (Customer), it doesn’t matter if a Human is there, or a robot, etc., but any inheritance is rigid class hierarchies in the code, extra cohesion (cheat. Coupling) and extra dependencies between components, which makes the code more difficult to understand and change.
Why, in this case, should the ground-air MaskCar be inherited from StandardCar and StandardPlane? Will she always behave like her parents, will there be violations of the substitution principle (see Liscov Substitution Principle)?
If MaskCar is going to change independently of its parents, even if for some reason it currently implements the same methods, there is no problem to define the same methods in the new class again, and if necessary, implement the Car and Plane interfaces (ICar and IPlane in your case).
If you need the behavior of parents, in most cases inheritance can be replaced with composition without any problems, if we are talking about C #, you can also very conveniently solve such problems through delegation ( https://metanit.com/sharp/tutorial/3.13.php)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question