Difference Between Abstract Class & Interface With Best Example????? [Resolved]

Posted by Sabarimahesh under C# on 5/7/2012 | Points: 10 | Views : 202070 | Status : [Member] | Replies : 12
Please all Of Update Ur Answer

with Clear Explanation

For Document Preparation

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M



Responses

Posted by: Sheonarayan on: 5/7/2012 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
Hi Sabri,

Probably below urls will help you understand the difference between Interface and Abstract class and then you will be in better position to write for the documentation purpose.

http://www.dotnetfunda.com/interview/exam4694-what-is-the-difference-between-abstract-class-and-interface.aspx
http://www.dotnetfunda.com/interview/exam332-when-to-use-interface-over-abstract-class.aspx
http://www.dotnetfunda.com/interview/exam4567-difference-between-abstract-class-and-interface.aspx

Hope this helps.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Sabarimahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 5/7/2012 [Member] Silver | Points: 25

Up
0
Down
Hi,

Refer this..

http://www.ronaldwidha.net/2008/06/16/a-good-example-of-abstract-class-vs-interface/

Regards,
Singaravel M

Sabarimahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 5/7/2012 [Member] Silver | Points: 25

Up
0
Down
See this also nice one..

http://geekswithblogs.net/mahesh/archive/2006/07/05/84120.aspx

Regards,
Singaravel M

Sabarimahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 5/8/2012 [Member] Bronze | Points: 25

Up
0
Down
Sheonarayan

Thank You

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Sabarimahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: karunakar5788-15015 on: 5/8/2012 [Member] Starter | Points: 25

Up
0
Down
Abstract Classes:-

-->abstrct methods means only method is defined.body undefined.(Not implented) just like skelton

-->non-abstract methods means method body is defined

just see below..

Sample_Parent is one class having 2 methods :

1--method1()--this is abstract method
2--method2()--this is non abstract method

public abstract class Sample_Parent
{
public void method1();

public void method2()
{
// write code here
}

}

the above clss having One abstract method is there,so should class is prefixed with One keyword 'abstract'.otherwise we wil get Compile time error.

Abstarct class having Some are abstrct methods and some are non-abstrct methods.

some People like this also Abstact class containg 0 or more number of Abstract methods!!

Ex:HttpServlet.

clear right now..

--> Now When we wil move For Abstract Class??

We have some of the Methods implementation we had..and we dont have some of the methods implementation

so then this time we use Abstract classes..

Suppose i write one example on This situtation:-

the above class is Abstart class we are unable to create the Object so we have to that abstrct method make it as a Non-abstrct method in Child class.

Class Sample_child extends Sample_Parent
{
public void method2()
{
//write code here..
}
public static void main(String args[])
{
//here we have to create Child class object..
}
}


and illegal combination of abstarct:

1.private--abstarct classes must inheritd.so the scope must public,default,protected.
2.sealed Or Final--the abstarct methods are must ovvrride in child classes but final ignored inheritance concept.
simply abstarct says am implementing in child clss but final is compltly Opposite.
So these are Illegal combination.
***************************************************************************************************


Now Waht Is Interface:-

Interface is 100% Abstaract Methods(All are Need to Implement to Child Classes)


in interface concept there is no chanse of Non-abstract methods.

and similar to Abstarct classes we are unable to create Object to Interface.

interface is a keyword..

am trying to write a Sample program on interface..

interface Sample
{
public void Method1();
void method2();
}

above Sample is Interface having 2 methods..so thse are ready to implement to in child classes


public class Sapmle_clss implements Sample
{
public void method1()
{
//write code here..
}


public void method2()
{
//write code here..
}

public static void main(string ar[])
{
//here we have to create object to Class.
}
}

We Dont have Any Implementaion so we move for Ineterface.

Example:
Any API's..

JDBC--Api relesed by Sun people
implemnted by OraclePeople,mysql,DB2,etc.

Servlet API--Relesed by Sun People Implemnetd By Apache,Weblogic, guys,,,

in interface methods are By default:public, abstrct
in interface Varibles are By default:public ,static, final


Advantges Abstract class over Interface:-

Multiple inheritance Concept not achiving through Abstract Classes

But Through Interface We can Achive Multiple Inheritance




Karunakar

Sabarimahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gaur1982 on: 5/9/2012 [Member] Starter | Points: 25

Up
0
Down
An Abstract class is a class with some common/certain implementations and defines abstraction for other services which are implemented in its concrete sub classes, whereas interface only have method declaration with zero implementations. The difference between Abstract class and An Interface is that if u call Abstract class then u have to call all method of that particular Abstract class but if u call an Interface then it is not necessary that u call all method of that particular interface. An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature, just the definition of the methods without the declaration(body). As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance.

Reference : http://planetofcoders.com/difference-between-an-abstract-class-and-an-interface/

Gaurav Agrawal
http://www.planetofcoders.com/

Sabarimahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down

Posted by: Rabbil on: 6/8/2012 [Member] Starter | Points: 25

Up
0
Down
Hi!
I think you can add this content also fro you complete documentation
follow the link:http://en.csharp-online.net/Should_I_use_an_abstract_class_or_an_interface%3F

Regards,
Rabbil Ahmad

"I cannot teach anybody anything. I can only make them think"~ Socrates
Thanks And Regards

Sabarimahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sachinp100 on: 12/7/2012 [Member] Starter | Points: 25

Up
0
Down
You can check it out @ http://sachinpachori10.blogspot.in


Sabarimahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 4/8/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
refer this

http://www.c-sharpcorner.com/UploadFile/93126e/difference-between-abstract-class-and-an-interface/

Mark as Answer if its helpful to you

Sabarimahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vivek07088 on: 9/5/2013 [Member] Starter | Points: 25

Up
0
Down
Abstract class is a class that contain complete and abstract (incomplete) both type of member and it can not be instantiated, Abstract classes are one of the essential behaviors provided by .NET.
Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior.

Go through the article -
http://www.dotnetbull.com/2011/11/difference-between-abstract-class-and.html

Sabarimahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajeshatkiit on: 12/17/2015 [Member] Starter | Points: 25

Up
0
Down
http://dotnetmagic.blogspot.in/2015/11/hcl-interview-questions-and-answers-for.html

Sabarimahesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response