abstract class and interface difference c#

Abstract Class & interfaces


Q:In what scenarios will you use a abstract class and in what scenarios will you use a interface?

    If you want to increase reusability in inheritance then abstract classes are good. 

    If you want implement or force some methods across classes must be for uniformity you can use a interface.

     So to increase reusability via inheritance use abstract class as it is nothing but a base class and to force methods use interfaces.

Some points about an Abstract Class are:

1.    We cannot create an object of an Abstract Class.

2.    An Abstract Class can be inherited with its derived class.

3.    Can have both concrete and abstract methods but at least one abstract method is compulsory in an Abstract Class.

4.    An Abstract Class is used as a base class for projects.

5.    An Abstract Class can inherit another base class and base interfaces.

6.    The constructor of an Abstract Class can be called through the derived class constructor.

7.    An Abstract Class should be used for a large project to share the common functionality with its derived class.

8.    All abstract methods must be implemented in its derived class, if inherited.

Some important things about interfaces:

1.    We cannot create an object of an interface; we can only create a reference or Interface Variable.

2.    An interface will have only abstract methods (method declaration).

3.    Interfaces support Inheritance, Polymorphism (Overloading, Overriding (using the "new" keyword to hide the interface methods above)).

4.    We cannot create variables in an interface.

5.    Only Properties, Indexers, Methods and Events are allowed in an interface.

6.    A class can inherit multiple interfaces, which is also shown in the snapshot above.

7.    We cannot create any Access modifiers with Interface Members (like private, public, protected, internal, protected internal, virtual, override, static, abstract etc.)

8.    The new keyword is allowed in an interface.

9.    All methods of an interface must be defined in every derived class.

10.    An interface is good for small or medium level projects.


Abstract Class                                         Interface


1.    An abstract class can have constructor declaration     1.    While an interface cannot do so.

2.    An abstract class is allowed to have all access modifiers for all of its member declaration     2.    While in interface we cannot declare any access modifier (including public) as all the members of interface are implicitly public.  

3.    An abstract class can declare or use any variables    3.    While an interface is not allowed to do so.

4.    An abstract class can have non-abstract Methods(concrete methods)     4.    While in case of Interface all the methods has to be abstract.

5.    In an abstract class all data member or functions are private by default     5.    While in interface all are public, we can’t change them manually.


About interfaces. 

1.          Extensibility

2.          Implementation Hiding

3.          Accessing object using interfaces

4.          Loose Coupling. 

Q. Difference between an Abstract method and a Virtual method


=> Both similarities use the override keyword. 

An Abstract Method can only be declared in an Abstract Class. That means no body part for an abstract method in an Abstract class. However, for virtual it can have a body part.

Q. The Differences between Abstraction and Encapsulation

Abstraction    Encapsulation

1. Abstraction solves the problem at the design level.    1. Encapsulation solves the problem in the implementation level.

2. Abstraction hides unwanted data and provides relevant data.    2. Encapsulation means hiding the code and data into a single unit to protect the data from the outside world.

3. Abstraction lets you focus on what the object does instead of how it does it    3. Encapsulation means hiding the internal details or mechanics of how an object does something.

4. Abstraction: Outer layout, used in terms of design.


For example: An external of a Mobile Phone, like it has a display screen and keypad buttons to dial a number.    4. Encapsulation- Inner layout, used in terms of implementation.

For example: the internal details of a Mobile Phone, how the keypad button and display screen are connected with each other using circuits.

Real-world Example:Use an example of a Mobile Phone

    You have a Mobile Phone; you can dial a number using keypad buttons. You don't even know how these are working internally. This is called Abstraction. You only have the information that is necessary to dial a number. But not internal working of the mobile.

But how does the Mobile Phone work internally? How are the keypad buttons connected with internal circuit? That is called Encapsulation.

Summary

    "Encapsulation is accomplished using classes. Keeping data and methods that access that data into a single unit."


    "Abstraction is accomplished using an Interface. Just giving the abstract information about what it can do without specifying the details."


    "Information/Data hiding is accomplished using modifiers by keeping the instance variables private or protected."

Important note : 

    Abstraction is accomplished through interfaces.

    It is also accomplished by abstract classes as well but all methods should be abstract.

    Encapsulation is accomplished by access modifier


http://www.c-sharpcorner.com/UploadFile/asmabegam/basic-concept-of-oop-in-C-Sharp/

Table of Contents

1.    Class

2.    Object

3.    Variable

4.    Method

5.    Access Modifiers

6.    Encapsulation

7.    Abstraction

8.    Inheritance

9.    Polymorphism

10.    Abstract Class/Method

11.    Virtual Method

12.    Sealed Class/Method

13.    Static Class/Method

14.    Interface


Can “this” be used within a static method?

We can't use this in static method because keyword 'this' returns a reference to the current instance of the class containing it.


Comments

Popular posts from this blog

data annotation attributes c#

how to create windows service setup in c#.net

difference between var and dynamic c#