What is the difference between Abstraction and Inheritance?

瘦欲@ 提交于 2020-01-04 05:36:07

问题


I could not find this question anywhere. Based upon my understanding, Inheritance should/might be the subset of Abstraction.


回答1:


First of you should be aware, that here is always some leeway in interpreting such terms and concepts. Below is my take on it.

Abstraction is the concept while inheritance is a technical realization.


Abstraction in general refers to the leaving out of (unnecessary) details. The opposite direction is concretization. Generalization is also often used in this context and basically means the same as abstraction.

In the context of computer science this can be used to describe several ideas:

  • One is the modelling of domain concepts. As in the class Car is an abstraction of real world automobiles. It uses an engine and four wheels to transport 1-5 people. Obviously this is nowhere near the informational density to describe a real car, but it may very well be all that is needed for the context of this application.

  • An other is to use it to describe the conceptual relation between multiple domain entities: A Car is a Motorvehicle. A Bus is also a Motorvehicle. Both are used to transport people. The Motorvehicle is the abstraction of both Car and Bus. It describes the idea of transporting people, while leaving out the detail of how many.

  • A third is the difference between an interface and an implementation. The interface abstracts the implementation by hiding the implementation details and only representing the surface area with which one may interact.


Inheritance is one method of realizing abstractions in code. It describes the process of taking a base class (this is the more general or abstract thing), inheriting all of its features/properties/behavior/meaning and adding some more details (or overriding some of the existing) to create the derived class (this is the more concrete thing).




回答2:


First, When you mark a class as abstract you cannot create an instance. Abstract classes are used to be inherited only.

But when you inherited from a non abstract class you can create instance from both of derived and base classes. So you can say abstract classes are used to generate new types, to be inherited not to create instance !

This is also somehow related polymorphism.

For example you have a base Employee class like below: public class Employee

{
    public string Name { get; set; }
    public double Salary { get; set; }

    public void IncreaseSalary(double rate)
    {
        this.Salary += (this.Salary * rate / 100);
    }
}

Now when we create SalesRepresentative class like below we should inherit it from Employee because SalesRepresentative is an Employee. public class SalesRepresentative : Employee

{
    public double AnnualSalesAmount { get; set; }
}

Now SalesRepresentative object has IncreaseSalary method because it is inherited from Employee. But in generally Sales Representatives' and Employee's Salaries are increased by different ways for example according to their AnnualSalesAmount.

In this case you should be able to change method code of IncreaseSalary from SalesRepresentative but you can't. Actually now you are across with Polymorphism

Now Let's come to abstraction. If you want to change the default code of IncreaseSalary from inherited class there are 2 choices. First marking the method as Virtual. And the second one is marking it as abstract.

The difference is If you mark it as virtual. You don't have to implement it in SalesRepresentative but If you mark it as abstract you have to implement it and you should forget an abstract member can only be in abstract classes. Examine the example below:

public abstract class Employee

{
   public string Name { get; set; }
   public double Salary { get; set; }

   public abstract void IncreaseSalary(double rate);

}

public class SalesRepresentative : Employee

{
    public double AnnualSalesAmount { get; set; }

    public override void IncreaseSalary(double rate)
    {
        if (this.AnnualSalesAmount > 100000)
        {
            this.Salary += (this.Salary * (rate + 5) / 100);

        }
        else
        {
            this.Salary += (this.Salary * rate / 100);
        }
    }
}


来源:https://stackoverflow.com/questions/57336414/what-is-the-difference-between-abstraction-and-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!