Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface assignments

 
Shashank Gokhale
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shashank Gokhale
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic