473,396 Members | 1,847 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,396 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 3214
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.