What is the difference between and interface and an
abstract class ?

Answers were Sorted based on User's Feedback



What is the difference between and interface and an abstract class ?..

Answer / wilbur j. pereira

What is an Interface?

An interface is a contract, a specification that concrete
classes MUST follow. It defines method signatures but
cannot have any implementations; the latter must be
provided by the classes that implement the interface.

C# differs from C++ in this regard because C++ lacks native
language support for interfaces. As a C++ programmers you
have to create an interface by defining an abstract class
with pure virtual methods.

what is an abstract class.................

An Abstract class lets you define some behaviors and force
your subclasses to provide others.
For example, if you have an application framework, an
abstract class may provide default services such as event
and message handling. Those services allow your application
to plug in to your application framework. However, there is
some application-specific functionality that only your
application can perform. So instead of trying to define
these behaviors, the abstract class can declare abstract
methods.

Differences between Interfaces and Abstract classes Which
we use ?

I. multiple inheritance

A class may implement several interfaces but can only
extend one abstract class.

II. default implementation

An interface cannot provide any code at all, much less
default code. An abstract class can provide complete code,
default code, and/or just stubs that have to be overridden.

III. adding functionality

If you add a new method to an interface, you must track
down all implementations of that interface in the universe
and provide them with a concrete implementation of that
method.
If you add a new method to an abstract class, you have the
option of providing a default implementation of it. Then
all existing code will continue to work without change.

IV. is-a vs -able or can-do

Interfaces are often used to describe the abilities of a
class, not its central identity, e.g. an Automobile class
might implement the Recyclable interface, which could apply
to many otherwise totally unrelated objects.

An abstract class defines the core identity of its
descendants.

********************************************************

There are lost of discussion on the internet about the
Interface vs Abstract class. Also, as base class whether we
have to use interface, abstract class or normal class.

I am trying to point out few considerations on which we can
take decision about Interface vs Abstract class vs Class.

Abstract Class vs Interface

I am assuming you are having all the basic knowledge of
abstract and interface keyword. I am just briefing the
basics.

We can not make instance of Abstract Class as well as
Interface.

Here are few differences in Abstract class and Interface as
per the definition.

Abstract class can contain abstract methods, abstract
property as well as other members (just like normal class).

Interface can only contain abstract methods, properties but
we don?t need to put abstract and public keyword. All the
methods and properties defined in Interface are by default
public and abstract.


//Abstarct Class

public abstract class Vehicles
{
private int noOfWheel;
private string color;
public abstract string Engine
{
get;
set;
}
public abstract void Accelerator();
}

//Interface
public interface Vehicles
{
string Engine
{
get;
set;
}
void Accelerator();
}

We can see abstract class contains private members also we
can put some methods with implementation also. But in case
of interface only methods and properties allowed.

We use abstract class and Interface for the base class in
our application.

This is all about the language definition. Now million
dollar question.

Thanks
Manoj(InfoAxon Technology Limited)


Is This Answer Correct ?    26 Yes 3 No

What is the difference between and interface and an abstract class ?..

Answer / porchelvi

ABTRACT CLASS
*************
• It can not be instantiated

• It allow us to specify all access modifier except
Private

• A class inheriting this must implement all of its
abstract method


• A class can inherit only one abstract class at a
time.

• Abstract class can add more functionality with out
destroying child classes that were using old version.


• We can declare the following
1. Fields
2. Constructors
3. Static Constructors
4. Static Functions
5. Concrete Functions

INTERFACE
*********
• It can not be instantiated

• It allows only public Access modifier

• A class implementing interface must provide body
for its entire member.

• A class can implement more than one interface at a
time.

• Adding of additional functionality will have an
effect on its child class due to the necessary
implementation of interface methods.

• We can not declare the following
1. Fields
2. Constructors
3. Static Constructors
4. Static Functions
5. Concrete Functions

Is This Answer Correct ?    11 Yes 2 No

What is the difference between and interface and an abstract class ?..

Answer / murali

1.interface contains only abstract methods and varibles
2.whenever a class is not implementing that interface that
class should be an abstract class
3.An interface cannot implement another interface but an
abstract class can implement onther interface
4.An interface can extend more than one interface
5.An abstract can implment more than one interface

Is This Answer Correct ?    5 Yes 5 No

What is the difference between and interface and an abstract class ?..

Answer / sathish kumar.t

ABSTRACT CLASS Interface

It consist of Static It consist of non- static member
member variables variables

It consist of non-abstract It consist of only abstract
and abstract methods methods

it consist of private and It consist of public classes
protected classes

It uses the keyword Extends uses the keyword Inplements

If subclass extends the If the subclass implements
abstact class, cannot extend interface, can implement any
any other class number of interfaces

Is This Answer Correct ?    19 Yes 23 No

Post New Answer

More OOPS Interview Questions

difference between structure and union.

2 Answers   ADP, Convergys,


What are the 5 oop principles?

0 Answers  


Can we create object of interface?

0 Answers  


What does and I oop mean in text?

0 Answers  


What is overriding vs overloading?

0 Answers  






for example A,B,C,D are class all the 4 class contain one method who() but the method who() implementaion is differnet among each class. the relation among the 4 class are A is base class and is inherited by B and C.and from this two B and C where D is inherited. the question is i want to display the output who() method in all the classes(A,B,C,D)the output of who() method is diferrent amond all the class(A,B,C,D) ------A------ virtuval who(print a) override | | who(print b) B C override who(print c) | | -------D------ override who(print d)

2 Answers  


hi, this is raju,iam studying b.tech 2nd year,iam want know about group1 and group2 details, and we can studying without going to any instutions? please help me.

0 Answers  


What is class encapsulation?

0 Answers  


What is overloading in oop?

0 Answers  


c++ is a pure object oriented programming or not?

5 Answers   Wipro,


What is encapsulation in simple terms?

0 Answers  


what is the difference between ERROR and EXCEPTION?

14 Answers   NIIT, nvidia,


Categories