Forums » .NET » .NET »

What is the difference between abstract and interface?


Posted Date: 02 Aug 2008      Posted By:: Vikranth      Member Level: Silver    Member Rank: 0     Points: 1   Responses: 11



Please give me the clear points.



Responses

#275466    Author: Kumar Velu      Member Level: Gold      Member Rank: 122     Date: 02/Aug/2008   Rating: 2 out of 52 out of 5     Points: 6

Abstract Class:

1. Abstract Class Can contain Abstract Methods and Non-
Abstract Methods.

2. When a Non-Abstract Class is Inherited from an Abstract
Class, the Non-Abstract Class should provide all the
implementations for the inherited Abstract Method.

Interface:

1. Interface is nothing but Pure Abstract Class ie
Interface can contain only the function declaration.

2. All the members of the interface are Public by Default
and you cannot provide any access modifiers.

3. When a class is inherited from the interface, the
inherited class should provide actual implementations for
the inherited members.


 
#275469    Author: Vidhya      Member Level: Gold      Member Rank: 0     Date: 02/Aug/2008   Rating: 2 out of 52 out of 5     Points: 6

hi,

A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class. Additionally, a class extending an abstract class can implement an infinite number of it's own methods.

another key difference between abstract class and interface is that , in abstract class we can put sharable code, but that is not possible in case of interface.

Another key difference between abstract class and interface is that,

We can use interface as marker, ( we can use abstract class also as abstract but then we can't extends any oter class, so it is better always use interface as marker)


 
#275478    Author: vishal       Member Level: Gold      Member Rank: 502     Date: 02/Aug/2008   Rating: 2 out of 52 out of 5     Points: 6

Adding more ,

Use abstract classes and interfaces in combination to optimize your design trade-offs.
Use an abstract class
* When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. (COM was designed around interfaces.)
* Use an abstract class to define a common base class for a family of types.
* Use an abstract class to provide default behavior.
* Subclass only a base class in a hierarchy to which the class logically belongs.

Use an interface
* When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.
* Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
* Use an interface to design a polymorphic hierarchy for value types.

* Use an interface when an immutable contract is really intended.

* A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality





 
#275511    Author: Ultimaterengan      Member Level: Gold      Member Rank: 11     Date: 02/Aug/2008   Rating: 2 out of 52 out of 5     Points: 2

Hi,
Interface Class:-
1)All The Methods are Abstract.its a pure Abstract Class
2)There No Access Modifier by Default : Public
3)There is No implementtation


Abstract Class:-
1)Abstract Class can have Abstract Methods and Non-Abstract Methods.
2)All The Access Modifiers Allowed in Abstract Class
3)implementtation Allowed in Abstract Class


i hope this may help u.



Thanks & Regards
G.Renganathan
Nothing is mine ,Everything is yours!!!



 
#275518    Author: chandramohan      Member Level: Gold      Member Rank: 216     Date: 02/Aug/2008   Rating: 2 out of 52 out of 5     Points: 6

Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. Inteface is a Java Object containing method declaration and doesn't contain implementation. The classes which have implementing the Interfaces must provide the method defination for all the methods.2. Abstract class is a Class prefix wtih a abstract keyword followed by Class definaton. Interacace is a Interface which starts with interface keyword.3. Abstract class contatins one or more abstract methods. where as Interface contains all abstract methods and final declarations.4. Abstract class contains the method defination of the some methods. but Interface contains only method declaration, no defination provided.5. Abstract classes are useful in a situation that Some general methods should be implemented and specialization behaviour should be implemented by child classes. Interafaces are useful in a situation that all properties should be implemented we can use this scenario
----------------


 
#275688    Author: Ashok Khanna      Member Level: Gold      Member Rank: 266     Date: 04/Aug/2008   Rating: 2 out of 52 out of 5     Points: 5

Differences between Abstract Class and Interface :
Abstract Class:
* can contain access modifiers for subs,properties and functions.
* a class may inherit only one abstract class.
* can contain defined fields and constants

Interface :
* cannot contain access modifiers for subs,properties and functions.
* a class may inherit several interfaces.
* cannot contain any defined fields and constants.

Please rate this post, if it is useful for you.

Thanks & Regards
Ashok


 
#277006    Author: RobinHood      Member Level: Gold      Member Rank: 44     Date: 06/Aug/2008   Rating: 2 out of 52 out of 5     Points: 4

Verify this link..This may help u..

http://www.geekinterview.com/question_details/22958
http://www.allinterview.com/showanswers/28553.html
http://java.ittoolbox.com/groups/technical-functional/java-l/what-is-the-difference-between-interface-and-abstract-class-1563512

Regards
Sridhar R
Nothing is illegal, Until You Get Caught


 
#289735    Author: Raghav        Member Level: Gold      Member Rank: 186     Date: 30/Aug/2008   Rating: 2 out of 52 out of 5     Points: 5

Vikranth,

Before putting any question in the forum, please search for it if the answer is already available in dotnetspider website. Because so many people are asking the same question about interface and abstract class. Please do not repeat the same questions again and again. There is a searchbox at the top right of the dotnetspider website. You can search for the answer by using that search box. If you do not find any answer, then you can post the question in the forum.

Raghav
Web Master

Raghav
Web Master


 
#295816    Author: Bunty      Member Level: Gold      Member Rank: 15     Date: 16/Sep/2008   Rating: 2 out of 52 out of 5     Points: 4

Hi,


Interfaces are similar to abstract classes.However,interface represent the highest level of abstraction in object-oriented programming.This is because all the methods in an interface are abstract and do not have implementation.In contrast,the abstract classes that are created using Abstract keyword might contain a method that has a body.

Regards
S.S.Bajoria


Thanks & Regards


Bunty


 
#298567    Author: Bunty      Member Level: Gold      Member Rank: 15     Date: 22/Sep/2008   Rating: 2 out of 52 out of 5     Points: 6

Hi,

Following are the difference between abstract and interface,

1>Abstract class having method declaration as well as method method definition whereas interface having method declaration only.

2>Abstract class are known as partial abstract class whereas interface is known as fully abstract class.

3>Abstract class features we have to inherit to the child class whereas interface features we have to implement in the child classes.

4>Abstract class support access specifiers whereas interface doesn't support access specifiers.

5>Abstract class have normal variable as well as constant variable whereas interface have only constant variables.

6>We can write constructor in abstract class whereas we can't write constructor in interface.

Regards
S.S.Bajoria


Thanks & Regards


Bunty


 
#307363    Author: Miss Meetu Choudhary      Member Level: Gold      Member Rank: 14     Date: 16/Oct/2008   Rating: 2 out of 52 out of 5     Points: 6

Following are the differences

• Interface contains methods that must be abstract; abstract class may contain concrete methods.
• Interface contains variables that must be static and final; abstract class may contain non-final and final variables.
• Interface can "extends" another interface, abstract class can "extends" another class and "implements" multiple interfaces.
• Interface is absolutely abstract; abstract class can be invoked if a main() exists.
• Interface is more flexible than abstract class because one class can only "extends" one super class, but "implements" multiple interfaces.
• Members in an interface are public by default; abstract class may contain non-public members.
• Interface is used to "implements"; whereas abstract class is used to "extends".
• Interface can be used to achieve multiple inheritance; abstract class can be used as a single inheritance.

==
Thanks and Regards
Meetu Choudhary

Thanks and Regards
Miss Meetu Choudhary (Site Coordinator)
Go Green Save Green


 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Return to Discussion Forum
Start new thread

Subscribe to Subscribers
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India