473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Interface

hi
My question might be a simple questions. I am doing C# maintanance project
I have seen so many places Interface is used in my main class. But I could't not understan
what is the use of Interface. Because Interface having only the Property and method
declaration only. Why we have to use that instead of writing directly in the main class?

1. What is the advatage of use of Interface (or disadvantage) instead of writing property and
method directly

2. When we have to use Abstract class and when we have to use Interface

Is there is any site which is explaining about the difference and usage?? Please let me kno

thank
Kanna
Nov 16 '05 #1
3 3211
You'd write an interface when you want to use the interface as a data type -
and have many classes that you could use.. for example (interfaces always
have an "I" in the front):

IDataAccess objMyObject;

objMyObject = new OfflineAccess();
objMyObject = new DirectAccess();
objMyObject = new WebSvcAccess();

Assuming that OfflineAccess, DirectAccess and WebSvcAccess all "implemented"
that IDataAccess interface - you could instantiate that object with any of
them..
And you use an interface when you want to define JUST the interface (what
methods and properties a class should have) - and you implement an abstract
class when yuo want to do that, and include some base logic or default
functionality of those methods or properties.

HTH
"Kannan" <an*******@discussions.microsoft.com> wrote in message
news:FF**********************************@microsof t.com...
hi,
My question might be a simple questions. I am doing C# maintanance project. I have seen so many places Interface is used in my main class. But I could't not understand what is the use of Interface. Because Interface having only the Property and method declaration only. Why we have to use that instead of writing directly in the main class??
1. What is the advatage of use of Interface (or disadvantage) instead of writing property and method directly?

2. When we have to use Abstract class and when we have to use Interface?

Is there is any site which is explaining about the difference and usage?? Please let me know
thanks
Kannan

Nov 16 '05 #2
Client code can refer to objects which implement an interface through an
interface variable. Eg, if two classes implement IPersist, some client code
can use instances of both classes through an IPersist-typed variable.

An abstract class can be partially implemented, ie, it can have both
abstract members AND implemented members. An interface is functionally akin
to an abstract base class with ONLY abstract members.

Brad Williams
"Kannan" <an*******@discussions.microsoft.com> wrote in message
news:FF**********************************@microsof t.com...
hi,
My question might be a simple questions. I am doing C# maintanance project. I have seen so many places Interface is used in my main class. But I could't not understand what is the use of Interface. Because Interface having only the Property and method declaration only. Why we have to use that instead of writing directly in the main class??
1. What is the advatage of use of Interface (or disadvantage) instead of writing property and method directly?

2. When we have to use Abstract class and when we have to use Interface?

Is there is any site which is explaining about the difference and usage?? Please let me know
thanks
Kannan

Nov 16 '05 #3
You will need to read a little about Object oriented programming...
Essential what does it mean when we say Interface means we are
specifying a "contract" is the following...

Going back to OOPS 101

Consider Animal as an Interface (read as Animal exhitbits certain
behavior i.e breathing)
Now Man is characterized by this animal behaviour of breathing ( read as
implements interface) Take a different Animal, Fish, it implements the
same behaviour but differently!

So what has the Interface achieved, extracting some common behaviour
"contract" from the different animals

Now if you think certain behaviour can be similarly exhibited you'd use
the abstract class. For example you could say Apes are an classification
(read as abstract) of Animals which breath with lungs. So if you're
dealing with different apes then you would use the behaviour described
by Apes.

Hope that clarifies things


So

Kannan wrote:
hi,
My question might be a simple questions. I am doing C# maintanance project.
I have seen so many places Interface is used in my main class. But I could't not understand
what is the use of Interface. Because Interface having only the Property and method
declaration only. Why we have to use that instead of writing directly in the main class??

1. What is the advatage of use of Interface (or disadvantage) instead of writing property and
method directly?

2. When we have to use Abstract class and when we have to use Interface?

Is there is any site which is explaining about the difference and usage?? Please let me know

thanks
Kannan


--
Regards,
Dilip Krishnan
MCAD, MCSD.net
dilipdotnet at apdiya dot com
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
4
by: Doug | last post by:
I am working on an existing .NET (C Sharp) component that had a com interface that was used by a VB component. When it was originally written, it had the SSEAssemblyCom class below - minus the two...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
6
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
2
by: Alex Sedow | last post by:
Why interface-event-declaration does not support multiple declarators like event-declaration? Grammar from C# spec: variable-declarators: variable-declarator variable-declarators ","...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
8
by: rn5a | last post by:
Suppose I have the following class code: Imports System Imports System.Data Imports System.Data.SqlClient Public Class DBSettings Private sqlCmd As SqlCommand Private sqlConn As...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.