Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Java interface-based programming

BrownZeus
 Share

Hey folks!

 

So I'm trying to wrap my head around the concept of programming through an interface.

I stumbled upon the idea in a Java Swing Tutorial, but its flying over my head to be perfectly honest.

I don't really understand it and the sources I've found through google either overexplain to the point of confusion or underexplain.

 

Can someone point me to a good source or provide a concise example?

 

Thanks in Advance!

Link to comment
Share on other sites

Link to post
Share on other sites

// Interfaces
// Interface declaration syntax
// <access-level> interface <interface-name> extends <super-interfaces> {
//     // Constants
//     // Method declarations
// }

// Example - Food:
public interface Edible {
    public void eat(); // Any class that implements this interface, must
                       // implement this method.
}

public interface Digestible {
    public void digest();
    // Since Java 8, interfaces can have default method.
    public default void defaultMethod() {
        System.out.println("Hi from default method ...");
    }
}

// We can now create a class that implements both of these interfaces.
public class Fruit implements Edible, Digestible {
    @Override
    public void eat() {
        // ...
    }

    @Override
    public void digest() {
        // ...
    }
}

// In Java, you can extend only one class, but you can implement many
// interfaces. For example:
public class ExampleClass extends ExampleClassParent implements InterfaceOne,
    InterfaceTwo {
    @Override
    public void InterfaceOneMethod() {
    }

    @Override
    public void InterfaceTwoMethod() {
    }

}

This example comes from learnxinyminutes, but both a brief introduction and in-depth explanation are provided by Oracle.

Link to comment
Share on other sites

Link to post
Share on other sites

Interface can also use this way in java 8 and later through lambda.  This is new however and I have yet to see any java programmers ever do things this way except when they wish to be lazy and write some quick code. 

 

class Example{

	public Example(){
    	// passing in a function to a doStuff method using lambda
    	this.doStuff(()->System.out.println("What ever doStuff is gonna do"));
    }
	
    // this is a functional interface
    @functionalinterface
	public interface Function{
    	public void execute();
    }
    
    // a class method that will take in a Function interface and exeute its one and only abstract function
    public void doStuff(Function function){
    	function.execute();
    }
    
    




}

 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, wasab said:

Interface can also use this way in java 8 and later through lambda.  This is new however and I have yet to see any java programmers ever do things this way except when they wish to be lazy and write some quick code. 

 


class Example{

	public Example(){
    	// passing in a function to a doStuff method using lambda
    	this.doStuff(()->System.out.println("What ever doStuff is gonna do"));
    }
	
    // this is a functional interface
    @functionalinterface
	public interface Function{
    	public void execute();
    }
    
    // a class method that will take in a Function interface and exeute its one and only abstract function
    public void doStuff(Function function){
    	function.execute();
    }
    
    




}

 

ouch... brutal for starter like him my friend?

Link to comment
Share on other sites

Link to post
Share on other sites

I wouldn't say I'm a starter, I've done programming work for actual work, and through school for 4 years, its just I've never seen this style/approach to programming.

I know what an interface is but we were never tasked with actually creating them.

 

Thank you guys for your help!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×