-2

I read many article about Abstract Class and Interface but every time i not able to explain to interview i don't know why plz help me better understanding in both topics.

What my understanding is about Abstract Class :- Abstract class those class where you want to use some common functionality in your project. Like there are 2 class 1) "Contract Employee" 2) Permanent Employee

So in both class having same Field Like First name , Last Name , Employee ID , Salary , Total Hours

So Here Salary Calculation is different based on your contract and permanent Employee but rest thing is same so here need Abstract class for same functionality.

Interface: - Interface is like Contract where all the methods in a Contract should be implement in Inherited Class. it mandatory.

Confusion: but in Abstract class also says if you declare some methods Abstract in Abstract class it also mandatory to Implement in Inherited class ?

Please explain me so i can get better understanding in both

thanks in Advance

5 Answers5

3

Interface is just a contract. It defines a public API through which any implementing type can be used. One type can implement multiple interfaces. In addition, interface can be implemented not only by classes but also by structs. This supports the polymorphism aspect of the OOP. They cannot contain fields or any type of executable code.

Abstract classes, they are both more and less than interfaces. The also can define a public API, but their main purpose is to provide base functionality for any derived type. They can contain full-blown methods and properties, fields. Many times they delegate actual implementation of some aspect of their functionality to their inheritors. Another differences are:

  • any class can derive from a single abstract (or concrete) class
  • only classes can derive from abstract (or any) type, not structs

So, interface is for contract definition, in order to use them, one has to implement them. Abstract class is for basic functionality sharing, they are derived from.

galenus
  • 2,087
  • 16
  • 24
0
  1. Abstract class useful when you need to share some implementation details between similar entities.
  2. Interfaces only declare the contract under which the object is used.

Both methods can be used for the polymorphism.

Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
0

They have similar characteristics. For me, choosing between the two is really about the distinction between "has a/does" and "is a".

One of the aims of OO programming is to represent real objects in the real world. If you use inheritance (i.e. an abstract class), you're saying that the base class is the fundamental type of the object you're modelling. So an Employee class often inherits from some kind of Person class, and it is more natural to derive Employee from Person than to derive it from an IPerson interface.

On the other hand, if you have some sort of behaviour that you want to implement in your classes, like serialization, you would probably want to represent that as an interface. Serialization is a feature offered by your classes, though it is not an intrinsic part of the class of real-world objects your class is trying to represent. It is a prototypical example of what is often referred to as a "cross-cutting concern."

That is why you would probably see a declaration like

public class Employee : Person, ISerializable { ... }

than to see the base class and the interface expressed the other way round. It also fits nicely with the fact that you can use multiple interfaces: in the world of the CLR, your class can have only one fundamental base class, but it can express any number of different behaviours.

Rob Lyndon
  • 12,089
  • 5
  • 49
  • 74
0

The main difference between abstract class and interface is , in abstract class you can have some logic implemented but in interface you can have only contracts defined but no implementation within interface. An abstract class with all the method as abstract is equivalent to interface.

srsyogesh
  • 609
  • 5
  • 17
  • *An abstract class with all the method as abstract is equivalent to interface.* - it really isn't. For a start, you can implement multiple interfaces in a single class. – Ant P Oct 27 '13 at 12:54
  • Ah well you are right , what i meant was that an abstract class with all the method as abstract and an interface is similar i.e the implementor or inheritor has to write all the functionality right from the scratch . – srsyogesh Oct 27 '13 at 13:01
0

They are certainly similar and many things can be accomplished by using both.

Consider following classes:

public abstract class PersonBase
    {
        public string Name { get; set; }
        public DateTime BirthDate { get; set; }

        public PersonBase(string name, DateTime bd)
        {
            this.Name = name;
            this.BirthDate = bd;
        }

        public int CalculateAge()
        {
            return DateTime.Now.Year - BirthDate.Year;
        }

        public abstract string HealthRecord { get; set; }

        public abstract string DisplayData();
    }

    public class TeenagerPerson : PersonBase
    {
        public PersonBase BestFriendForever { get; set; }
        public override string HealthRecord { get; set; }
        public TeenagerPerson(string name, DateTime bd)
            : base(name, bd)
        {
        }

        public override string DisplayData()
        {
            return string.Format("Name:{0}  Age:{1}  Health record:{2}  BFF:{3}", Name, CalculateAge(), HealthRecord, BestFriendForever.Name);
        }
    }

    public class AdultPerson : PersonBase
    {
        public override string HealthRecord { get; set; }
        public AdultPerson(string name, DateTime bd)
            : base(name, bd)
        {
        }

        public override string DisplayData()
        {
            return string.Format("Name:{0}  Age:{1}  Health record:{2}", Name, CalculateAge(), HealthRecord);
        }
    }

This sample uses abstract base class for providing some common properties while allowing derived classes to declare their own properties too. The solution enforces derived classes to implement DisplayData() method that shows the contents of the class to outside world - as they please of course.

Testing out the code:

var teenager = new TeenagerPerson("Joanne", new DateTime(2000, 11, 05));
teenager.BestFriendForever = new TeenagerPerson("Julia", new DateTime(2000, 10, 05));
Console.WriteLine(teenager.DisplayData());

var adult = new AdultPerson("Mike", new DateTime(1960, 01, 03));
adult.HealthRecord = "2003 - Heart Bypass";
Console.WriteLine(adult.DisplayData());

Gives following output:

Name:Joanne Age:13 Health record: BFF:Julia

Name:Mike Age:53 Health record:2003 - Heart Bypass

The options to work with abstract classes are limitless and I certainly think of them as more versatile as interfaces. The disadvantage with abstract classes of course is that since C# doesn't have multi-inheritance then your one shot at inheritance is already used with an abstract class.

PS: Just notices that my age calculation sucks, might be better off using timespan. :)

Pompair
  • 7,083
  • 11
  • 60
  • 69