What is the difference between Abstraction and Implementation?

Asked

Viewed 347 times

2

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

  • 1

    Is there a context? The question arose because? In the current form it seems that you are asking what is the difference between an orange and a chicken. So if you tell me you want to eat one of them and you want to know what’s healthier, it makes it easier to answer. Are you talking about an abstract method or an implemented method?

  • @Bigown thanks for the question, is that I’m starting in OOP and I see several videos people talking, program for Abstraction and not for Implementation. And that’s it, it doesn’t say anything else. If you can edit the question to stay within the context of programming and answer it, I appreciate it. And I learned a lot from reading your answers here at SOPT.

  • 2

    I think you’re wondering about this: https://answall.com/q/86484/101.

  • @Thanks for the link and thanks!!!

1 answer

2


Abstraction is the ability to concentrate on the essential aspects of any context, ignoring less important or accidental characteristics. In object-oriented modeling, a class is an abstraction of existing entities in the domain of the software system.

For example, we imagine the abstraction concerning the class Animals. There are several entities in the Animals class such as Amphibians, Reptiles and Mammals that are also sub-classes of the Animals class, where there are objects that contain each sub-class such as Human, Alligator and others.

An abstract class is developed to represent abstract entities and concepts. The abstract class is always a superclass that has no instances. It defines a template (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. (WIKIPEDIA)

The implementation, it’s really the job, writing the code, anyway. What people want to talk about is that you need to plan, how the system will behave in different situations using the objects, instead of writing the code without thinking about the abstraction of problems.

Grotesque example, taking advantage of the hook of the Animals demonstrated in the Wikipédia:

It was not thought in the abstraction of problems, and anyway... only 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 minimal object planning, 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();

    }

That way, when implementing other animals, the entire previous mammal and animal structure, would already be implemented, you wouldn’t need to write all these properties again. That part will have already been abstracted. In a small example, it seems compensatory the first way, but try to apply these concepts to something big...

Sorry for the example, I hope it helps you, but I’m not a teacher and the teaching is terrible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.