Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

help with interfaces in Java

I have a programming assignment due tomorrow that is supposed to utilize the use of interfaces in java. My issue is that I'm not understanding why interfaces are useful; supposedly they reduce redundant coding, but I'm just not seeing it. Interfaces are not supposed to include any implementation of the coding; they only contain abstract methods. But...if that's true, then you still have to program those methods for EVERY class that implements that interface, even if those methods are virtually the same...

So could anyone explain to me how interfaces do not result in redundant coding, and why they're useful? I don't have a huge amount of programming experience so you might have to explain it in simple terms :P
 

Zeriab

Sponsor

An interface in Java specifies the methods with which an outside object can interact and call.
You can use it to ensure that classes have certain methods. They are usually easy to look over and get a nice overview since the methods have no bodies.

Let's for fun make an interface:
FooI.java
Code:
public interface FooI {
	public void doAction();
	public boolean doAction2(int value);
	public boolean doCompare(FooI foo);
}

Now I'll make two other classes which implements this interface:
Foobar.java
Code:
public class Foobar implements FooI{
	/**
	 * Must implement the following methods
	 * accordingly to the interface
	 */
	public void doAction() {
		// TODO Specify what the action does here
		
	}
	public boolean doAction2(int value) {
		// TODO Specify what the action does here
		return false;
	}
	public boolean doCompare(FooI foo) {
		// TODO Specify what the compare does here
		return false;
	}

}

Foo.java
Code:
public class Foo implements FooI{
	/**
	 * Must implement the following methods
	 * accordingly to the interface
	 */
	public void doAction() {
		// Do the action here
	}
	public boolean doAction2(int value) {
		// Do the action2 here
		if (value > 10) {
			return false;
		} else {
			return true;
		}
	}
	public boolean doCompare(FooI foo) {
		return foo.equals(this);
	}
	
	/**
	 * You can add other methods as well.
	 * You are free to do that
	 */
	public void doTheFoo() {
		System.out.println("Foo!");
	}
}

For a small test I've created this little class
Test.java
Code:
public class Test {
	/**
	 * Just an example if you want to test it
	 */
	public static void main(String[] args) {
		FooI foo1 = new Foobar();
		FooI foo2 = new Foo();
		System.out.println(foo1);
		System.out.println(foo2);
		System.out.println("foo1.doCompare(foo2): " + foo1.doCompare(foo2));
	}
}

Since I use FooI foo1 on the left-hand side of the declaration I know that whatever object I set it too it will fulfill the requirements imposed by the interface. Basically have the methods I specified and I can use them if I want to.
I cannot however use methods defined in the class I instantiate if it is not defined in the interface.
I cannot do foo2.doTheFoo()
If I now did Foo foo = new Foo() I would be able to do foo.doTheFoo() just fine.

On a side note: You cannot implement interfaces in interfaces, but you can extend interfaces.

You can say that interfaces are abstractions.
You know which methods you can call and it is a nice way to insure that your other classes implements the necessary methods.
There is however no code reuse as if you have a super class. (Extends)
If you are using inheritance you can often choose not to implement methods that are present in the superclass and simple use the code specified in the superclass.
The principle of putting shared code among classes in a superclass so you only have it one place. That does not apply to interfaces.
The thing is that multiple inheritance is not supported in Java. You can only have one superclass while you can implement multiple interfaces.
You can use interfaces to support multiple inheritance in Java. This does however mean that you have to copy code.

Though the main idea of an interface is basically to specify the way you are supposed to act with classes of a specific type.

*hugs*
- Zeriab
 
Thanks, that helps quite a bit. It's still taking me a while to get used to object-oriented programming...I'm so used to non-object oriented design...but I want to learn Ruby so understanding these concepts with object-oriented design helps quite a bit.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top