Click here to Skip to main content
13,248,087 members (36,236 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Can somebody tell me what is the exact difference between Inheritance and Abstraction. i.e. In which scenario I have to use only inheritance and in which scenario i have to use only Abstract classes.
Posted 4-May-12 18:47pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

Of course Abstraction is interested with Inheritance. But there are some important points.
First of all when you mark a class as abstract you cannot create an instance. This is very important. 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 we can say abstract classes are used to generate new types,to be inherited not to create instance !

And of course abstraction is interested with Polymorphism. Abstraction forces us to 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'nt 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);
            }
        }
    }
  Permalink  
Comments
Nima.naqipoor 5-May-12 1:54am
   
good explain! :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Abstraction and Inheritance are the important features having wide implications in Object Oriented Programming. The full features may not be covered exhaustively in a short answer, and these are dealt elaborately in various books and articles. However, to get a quick idea they can described as below:

Abstraction from Object Oriented Programming perspective is extracting the core features of an object to deal without being specific about the implementation details. For example if we take a vehicle then it can be abstracted like a an object having Wheels, Seats, Gears among other things, which can be treated as the members of a Vehicle class describing its composition. To add behaviour to the Vehicle there can be methods like GiveHorn, Drive, ChangeGear, ApplyBrake etc.

Then specific classes can be derived from this base class using Inheritance. Say a Car, a Bus. So a Car is a vehicle with 4 wheels, 4 gears and say 4 seats. The Drive, Change Gear, Apply brake can be implemented within this derived class either afresh or they can used as provided in the base class or with a combination as per the requirement. Similarly a Bus is derived from the Vehicle class using inheritance and it has 6 wheels, 20 seats, 5 gears. The methods can be implemented as explained above.

In this connection the following articles may be helpful.
Abstraction (computer science)[^]
Inheritance (object-oriented programming)[^]

The abstraction and inheritance can be implemented either with Abstract classes or Interfaces and the following references may be helpful in this regard.

Abstract Class versus Interface[^]
Abstract Class Vs Interface[^]

The answers given to this question may also be helpful.
Purpose of Interfaces[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171114.1 | Last Updated 5 May 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100