interfaces and abstract - seems to be same ?

Posted on 2000-05-15
Last Modified: 2010-04-01
Both interfaces and abstract classes
definies abstract functions and implemented or extended, what is the use of having two different things for same purpose?
Question by:m_srini_srini040400
  • 2

Expert Comment

ID: 2810976
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.

Expert Comment

ID: 2810986
From: g_senthilkumar
Date: Wednesday, January 26 2000 - 09:58PM CST

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
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:


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

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.


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


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:


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.

Accepted Solution

Ravindra76 earned 49 total points
ID: 2810992
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();


Interface with example:

First create a file with the following code.

public interface Inter {

public void callMe();


Then create a file 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();

// Output is :Hello World from ExinterA

inter = new ExInterB();

// Output is :Hello World from ExinterB



Compile these two classes.

run as

java ExInter

You will find output as specified


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.


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
factorial example 4 47
servlet example 17 51
How to configure empty element in XML Document parser? 15 35
Java basic valueOf question 1 29
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question