Link to home
Create AccountLog in
Avatar of m_srini_srini040400
m_srini_srini040400

asked on

interfaces and abstract - seems to be same ?

Both interfaces and abstract classes
definies abstract functions and implemented or extended, what is the use of having two different things for same purpose?
Avatar of id
id

The main distinction between those two is that you can have multiple inheritance with interfaces.

To answer a question why then can't they have interfaces only - abstract classes allow actually to implement some methods (which you can happily overwrite later :-) and interfaces don't.
From: g_senthilkumar
Date: Wednesday, January 26 2000 - 09:58PM CST

Hi,
As all the guys told,both abstract class and interface will delcare the methods which all the
derived classes has to override.
In abstract class,
it contains atleast one abstract method(ie method prototype,but no implementation) and all
the derived classes extending that abstract classes has to override that abstract
method.Otherwise the derived class will become abstract.Abstract classes can contain normal
methods(ie mehtods with implementations).That normal derived class dont need to override.
In Interface it can contain only method prototype,no implementation at all.By default all the
methods in interface becomes public and abstract.
Thats why if you implementing the interface and that while overriding the interface methods you
have define that methods as public,since in jave you can not reduce visibility.
In interface all the methods are be default public and abstract and all the variables are by
default public,static and final.So you can only use the variables and you can not change the
value.
In abstract normally,it should contain atlest one abstract method.That time compilier enforce
you to declare the class as abstract.Even the abstract class can exist without any abstract
methods.That time the compiler wont enforce you to declare the class as abstract.That time if
you declare the class as abstract,it wont give any error.The only thing is you cannot create
instances of that abstract classes.
So both abstract class and interface are used to define the methods which can be overriden
by all the derived classes.But in abstract class you can define normal methods also.But
interface,by default all the methds are abstract.So for that reason you can say interface is a
pure abstract class,otherwise one step ahead of abstract class.


From: Jod
Date: Wednesday, January 26 2000 - 10:19PM CST

[Hi benson - your other question has got rather technical so I'll try to keep it simple here :)]

A good basic principle to use is this:

**********************
1)
**********************

Use an interface to group a number of classes into one parent type (such as sheep and wolves
into animals for example).

Use inheritance and possibly an abstract base class when you are refining the behaviour of the
same type of object. For example if you have a sheep object and you want to divide your sheep
into two subclasses such as sheep with horns and sheep without horns.


One way to look at it is how much of the code I inherit from the base class is actually useful to
me.

If you inherit lots of code and are only changing one method for example then it is practical to
inherit from super class or abstract base class.


**********************
2)
**********************

If you abstract class has no implementations or you find that you always need to override the
base class implementations in most cases then your code will be more flexible by using an
interface.

**********************
3)
**********************

Another point is, if your classes need to inherit from more than one thing then you should use
an interface.

For example, if you need to express information about animals your subclasses (such as sheep)
might have the need to inherit from two things:

Animal
Carnivore
Herbivore

In this case if you use an abstract base class you cannot make your sheep class be of type
animal and of type herbivore.

You may want to do this, because you might want to deal with animals that eat grass differently
to animals that eat other animals.

If you use interfaces in Java, then you can do this.
ASKER CERTIFIED SOLUTION
Avatar of Ravindra76
Ravindra76

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer