Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a small confusion in this topic "Abstraction vs Interface" can any one help me in this.

I heard that what ever the methods are there in the interface they are public Abstract in nature!

Similarly in an abstract class i can declare abstract methods and define this in the child class.

When i define this in the child class i need to Override this. Why not this thing required in

the case of Interface because interface will also contain the Abstract methods?

And also i want to know what is the differences between Abstraction and Encapsulation?

C#
abstract class abschandan
  {
      public abstract void add();
      public void sub()
      {
          Console.Write("dfg");
      }
  }
  class chandan : abschandan
  {
     public override void add()
      {
          Console.Write("hi");
      }
  }




Thanks
-------
Vamsi krishna
Posted

When to use Interface?
Interfaces are best suited for providing common functionality to unrelated classes.

When to use abstrac class?
If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes should be used primarily for objects that are closely related.


In Interface there is only signature, there is no definition, you have to inherit the Interface to class and you must give the definition.

In Abstract class , if you want to give new definition for a method. you can override the method. otherwise you can use the Abstract class method itself.

Encapsulation is used to hide its members from outside class or interface, whereas abstraction is used to show only essential features.


Abstraction - http://www.onlinebuff.com/article_oops-principle-abstraction-in-c-with-an-example-and-explanation_5.html

Encapsulation - http://www.onlinebuff.com/article_oops-principle-encapsulation-in-c-with-an-example_15.html[^]
 
Share this answer
 
First of all Refer this article...

All about abstract classes.[^]

Here is the difference between Interfaces vs Abstract class...
Abstract Class versus Interface[^]

And if you want to explore basic concept of OOP refer this article...
Generally all we know "How" can we use OOP concepts(polymorphism, encapsulation, inheritance, abstaction ) But this Article will show you "WHEN" & "WHY" to use OOP.

This article worth reading...
Object Oriented Design Principles[^]

Hope This Help You
--------------------
Pratik Bhuva
 
Share this answer
 
v2
In an abstract class, you can have both abstract and non-abstract methods.
Interfaces do not contain any implementation.

Try All about abstract classes.[^].
 
Share this answer
 
"I heard that what ever the methods are there in the interface they are public Abstract in nature!"

No, they aren't.
Methods defined in an Interface cannot be abstract - they never have any form of implementation except in the derived class, which absolutely must implement all methods declared in the interface class.

Methods defined in an Abstract class can have an implementation which may be overridden and implemented in a derived class, but doesn't have to be. If it isn't then the base class version is used.

Abstraction and encapsulation can't be compared: they are not strictly related, and do not even try to do the same job!

Encapsulation hides the implementation details and provides a layer of code over the top to access it - the external consumer of the class cannot interact with the encapsulated class directly, but must go via the class in which the object is embedded - and probably doesn't even know it exists.

Abstraction is providing a generalization (say, over a set of behaviors).

Time for some reading: Abstraction, Encapsulation, and Information Hiding[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900