What is the difference between Abstraction and Implementation? [duplicate]

1

I would like to know the difference between Abstração and Implementação , and how this applies in practice!

    
asked by anonymous 03.05.2017 / 18:32

1 answer

2

Abstraction is the ability to focus on the essentials of any context, ignoring less important or accidental features. In object-oriented modeling, a class is an abstraction of entities in the domain of the software system.

For example, we imagine the abstraction referring to the Animals class. There are several entities in the Animals class, such as Amphibians, Reptiles and Mammals, which are also subclasses of the Animals class, where there are objects that contain each sub-class as Being-human, Alligator, and others.

An abstract class is designed to represent abstract entities and concepts. The abstract class is always a superclass that has no instances. It defines a template for a feature and provides an incomplete implementation - the generic part of that functionality - that is shared by a group of derived classes. Each of the derived classes completes the functionality of the abstract class by adding a specific behavior. (WIKIPÉDIA)

The implementation, is the work itself, write the code, anyway. What the people want to talk about is you need to plan, how the system will behave in different situations using the objects, rather than writing the code without thinking about the abstraction of the problems.

Example grotesque, taking advantage of the hook of the animals demonstrated in the wikipédia:

It was not thought of the abstraction of the problems, and finally ... just implemented:

        public class Boi
        {
            public Boi()
            {
                Familia = "mamiferos";
                SubFamilia = "bovinos";
            }
            public string Familia { get; set; }
            public string SubFamilia { get; set; }
            public void Som()
            {
                //Mugir
            }
        }
        public class Cachorro
        {
            public Cachorro()
            {
                Familia = "mamiferos";
                SubFamilia = "caninos";
            }
            public string Familia { get; set; }
            public string SubFamilia { get; set; }
            public void Som()
            {
                //Latir
            }
        }

        private void Programa()
        {
            string animal = "boi";

            if (animal == "boi")
            {
                Boi boi = new Boi();
                boi.Som();
            }
            else
            {
                Cachorro dog = new Cachorro();
                dog.Som();
            }
        }

Now with the minimum planning of the objects, it would look like this:

   public interface IAnimal
   {
       public void Som();
       public string Familia;
       public string SubFamilia;
   }

    public class Mamifero : IAnimal
    {
        public Mamifero()
        {
            Familia = "mamifero";
        }

        public string Familia { get; set; }
        public string SubFamilia { get; set; }
        public virtual void Som()
        {
            throw new NotImplementedException();
        }
    }

    public class Boi : Mamifero
    {
        public Boi() : base()
        {
            SubFamilia = "bovinos";
        }

        public override void Som()
        {
            //mugir
        }
    }

    public class Cachorro : Mamifero
    {
        public Cachorro() : base()
        {
            SubFamilia = "caninos";

        }

        public override void Som()
        {
            //Latir
        }
    }

    public void Programa()
    {

        IAnimal animal;
        string animalEscolhido = "cachorro";

        if (animalEscolhido == "cachorro")
        {
            animal = new Cachorro();
        }
        else
        {
            animal = new Boi();
        }

        animal.Som();

    }

So, when it comes to implementing other animals, the whole mammalian and animal structure would already be implemented, you would not have to write all these properties again. That part will have already been abstracted. In a small example, it seems to compensate for the first form, but try to apply these concepts to something great ...

Excuse the example, I hope it helps you, but I'm not a teacher and the teaching is terrible.

    
03.05.2017 / 20:15