File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Interface assignments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface assignments" Watch "Interface assignments" New topic
Author

Interface assignments

Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
I have a question
Say if you have a class
public class dingBat implements Runnable
{
public void run()
{}
public static void main(String arg[])
{
Runnable r=new dingBat();
}
}
what is the point of saying Runnable r=new dingBat();


May the force of the Java be in all of us !!!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Shashank,
Probably not much use in your example. Consider the following:
Foo.java

Bar1.java

Bar2.java

Bar3.java

FooBarTest.java

Here is the output:
Bar1: this is Foo #1
Bar2: this is Foo #2
Bar3: this is Foo #3
Granted that this is a trivial example, but notice that we create an array of Foo which is the interface. The implementing class doesn't matter to us since we know that any class that implements Foo must have a fooMe() method. Our Bar classes could have many more methods and state totally different from each other but that is of no concern to us. Interfaces in Java is one of its most powerful features. Many of the Java GUI classes will implement various interfaces to get the work done of loading files, painting an image etc. Probably the most used is ActionListener which has a single method actionPerformed() which is called automatically when certain events occur.
Hope this clears it up,
Michael Morris
[ February 07, 2003: Message edited by: Michael Morris ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Shashank Gokhale:
what is the point of saying Runnable r=new dingBat();

You can later exchange the instanciation of dingBat with any other Runnable and be sure that the program will still compile.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
Michael and Ilja,
Okay I see your example. You say that if we assign class objects to the interfaces that they implement, then we are always sure that each of these objects will implement the method that is declared in the interface.
Then that raises another question.
We know that a class can implement many interfaces but extend only one class. In the example that Michael gave, couldnt an abstract class be used instead of an interface, since in that scenario also we can be sure that each of the extending classes has to implement the method in the abstract class.?
public abstract class foo
{
public abstract void fooBar();
}
public class A extends foo
{
public void fooBar()
{
System.out.println{"Foobarred once");
}
}
public class B extends foo
{
public void fooBar()
{
System.out.println{"Foobarred twice");
}
}
public class C extends foo
{
public void fooBar()
{
System.out.println{"Knee deep in foobar");
}
}
public class footest()
{
public static void main(String arg[])
{
foo[] f={new A(), new B(), new C()};
for (int i=0; i<f.length;i++)
f[i].fooBar();
}
}
Wouldnt this also work the same way as if foo was an interface and not an abstract class? If so, then what is the point of assigning a class to the interface it implements?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Shash,

Wouldnt this also work the same way as if foo was an interface and not an abstract class? If so, then what is the point of assigning a class to the interface it implements?

Sure, that would work. But note what I said in my earlier post:

... The implementing class doesn't matter to us since we know that any class that implements Foo must have a fooMe() method. Our Bar classes could have many more methods and state totally different from each other but that is of no concern to us.
...

You use an abstract class to create a hierarchy of closely related concrete classes with similar behavior. You use an interface for communication between classes that otherwise bear no similarity
to each other. It is by far a better paradigm than the multiple inheritance allowed in C++.
Michael Morris
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
Michael,

... The implementing class doesn't matter to us since we know that any class that implements Foo must have a fooMe() method. Our Bar classes could have many more methods and state totally different from each other but that is of no concern to us.

Still not clear on the difference lies between extending an abstract class and implemenenting an interface. Subclasses that extend an abstract class too can have different methods within themselves that make the subclasses different from each other.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Shash,
Maybe I'm just not articulate enough to explain what is so obvious to me and 1000s of other OO programmers. So let's go straight to the Horse's mouth:
Java Tutorial: Interface concept
Read that and see if it makes more sense to you than my meager attempt.
Michael Morris
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
Michael,
Actually you did a pretty good job so far. I hav read through the java tutorial at the sun site before, and it didnt make as much sense to me as what Im learning at this forum. For example I had no idea that you could do something like
Runnable r=new ImplementingClass();
I just found out recently that you can and now from what you told me I see how it is useful.
I just dont understand why assigning to an interface is more useful than assigning to an abstract class, when both the result are the same. Maybe its something Ill get a better understanding of over time.
Sorry if some of my questions dont make much sense, remember youre a professional Im a novice
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
If Runnable was an abstract class, your implementation couldn't inherit from another class. That's the only reason - really!
In C++, where multiple implementation inheritance is allowed, you *would* simply use an abstract class - no needs for interfaces at all.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Interface assignments
 
Similar Threads
Annonymous Inner Class
Inner classes
Anonymous Inner Class
Anonymous Inner Class
regarding threads