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?
m_srini_srini040400Asked:
Who is Participating?
 
Ravindra76Connect With a Mentor Commented:
From ravindra76

Hi ,

Yaa. Interface is a set of abstract methods.

Java provides partial support for multiple inheritance.

Multiple inheritance means exteding two base classes. Since java allows extending single base class, you can use
interfaces. Now you are able to implement one or more interfaces.

class classA extends classB implements interface1,interface2,interface3,....interfacen {


}

In multiple inheritance, you are extending multiple base classes. Here instead of multiple base classes, implement
multiple interfaces.

But in interfaces, you have no variables. In my view variables in sense means the thing which will allowed to
change. But interface "variables" are all constants.Variables define state. In interfaces, since variables are constants,
no change in state.

In c++, you will get the state of base object variables because you have no restrictions.
So you can not get 100% multiple inheritance in java. Interface will allow grouping of methods.


IF you have doubt, how can i use multiple interfaces? I can use single interface with all methos.


Here an example to define multiple interfaces.

public interface1 {

public void method1();
public void method2();

}

public interface2 {

public void method1();
public void method2();

}

public interface3 {

public void method1();
public void method2();

}

class A implements interface1,interface2 {


}

class B implements interface2,interface3 {


}

In this case, class A don't want methos in interface3 and class B do't want methos in interface1.

That's why multiple interfaces came.

Come to the point,

There is no full workaround for multiple inheritance in java.

Interface is a set of abstract methods and constant. It has no variables.

So if you have variables, there is no other way to come to composition.

SO in this case you have to use composition as

Class C{

A a;
B b;

public C() {
a = new A();
b = new B();
a.callYourMethod();
b.callYourMethod();
}

}


Interface with example:


First create a file Inter.java with the following code.

public interface Inter {

public void callMe();

}


Then create a file ExInter.java with the following code.






class ExInterA implements Inter {

public void callMe() {
System.out.println("Hello World from ExinterA");
}

}

class ExInterB implements Inter {

public void callMe() {
System.out.println("Hello World from ExinterB");
}
}

public class ExInter {

public static void main(String args[]){

Inter inter= new ExInterA();
inter.callMe();

// Output is :Hello World from ExinterA

inter = new ExInterB();
inter.callMe();

// Output is :Hello World from ExinterB

}


}


Compile these two classes.

run as

java ExInter

You will find output as specified


Summary:


Inteface is not a full work around of Multiple inheritance. It is to a some extent.
If you want exact Myltiple Inheritance,
composition is the solution.

Usage in real applications:

Interfaces : If Ur class has all abstrat methods and constants, use interfaces.
Note: Interface will contain constants only. If Ur class has abstract methods and
variable data use abstract class instead of interface.

Abstract Class : If Ur class needs combination of abstract and non abstract methods,
use abstract classes.



0
 
idCommented:
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.
0
 
Ravindra76Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.