3

I tried to keep the code as generic as possible, this only represents the basic setup. I am a Java beginner trying to understand Interfaces, Classes and Methods. I did change the Interface name and Class names to make referencing them easier. I am fully aware that this code as it is won't compile. I am trying to understand the concept.

Given is an Interface, with an existing InterfaceClass and another class using the interface.

Interface:

public interface IInterface extends Comparable<IInterface>{    
    String getContent() throws DumbException;
}

Class:

public class InterfaceClass implements IInterface{
    public String getContent() throws DumbException {
        // here I would need a parameter (filepath) to get a file 
        // and read its content and return the content as string
        // however the interface doesn't provide a parameter for this
        // method. So how is this done?
    }
}

The class using the method:

public class Frame extends AbstractFrame {
    public void setDetails(IInterface details) {
        // This is the call I don't understand...
        details.getContent();     
    }
}

The part I don't understand is: How does the details object give any parameter to getContent()? I mean I don't even see this object being defined other than IInterface details

CMA
  • 287
  • 1
  • 2
  • 11

4 Answers4

1

Solution 1

You can redefine IInterface to

public interface IInterface extends Comparable<IInterface>{    
    String getContent(String filepath) throws DumbException;
}

public class InterfaceClass implements IInterface{
    public String getContent(String filepath) throws DumbException {
        // use filepath to get the file's content
    }
}

Usage is

public class Frame extends AbstractFrame {
    public void setDetails(IInterface details) {
        details.getContent("/path/to/some/folder");     
    }
}

Solution 2

You can't change IInterface but you add a constructor to InterfaceClass

public class InterfaceClass implements IInterface{
    private String filepath;

    public InterfaceClass(String filepath) {
        this.filepath = filepath;
    }

    public String getContent() throws DumbException {
        // use filepath to get the file's content
    }
}

Usage is

new Frame().setDetails(new InterfaceClass("path/to/some/folder"));
Spotted
  • 4,021
  • 17
  • 33
  • Thank you for not only understanding my confused question but also for making it simple and clear! – CMA Aug 12 '16 at 15:01
0

details is the argument passed to setDetails as type implementing IInterface. Anything that implements IInterface can be used as details, since the interface gives you contract assurance that getContent is in fact a strict member for that implementation.

wulfgarpro
  • 6,666
  • 12
  • 69
  • 110
0

You can pass a data in constructor in base class

public class InterfaceClass implements IInterface{

    private String data;

    public InterfaceClass(String data) {
        this.data = data;  
    }

    public String getContent() throws DumbException {
        //do something here with data

        // here I would need a parameter (filepath) to get a file 
        // and read its content and return the content as string
        // however the interface doesn't provide a parameter for this
        // method. So how is this done?
    }
}

You can use this class now:

IInterface myNewClass = new InterfaceClass("blablabla");
frameClass.setDetails(myNewClass);

And pass it to second class.

ByeBye
  • 6,650
  • 5
  • 30
  • 63
0
    public interface IInterface extends Comparable<IInterface>{

            String getContent() throws DumbException;
        }

    public class InterfaceClass implements IInterface{
        public String getContent() throws DumbException {//this method is of string type so you must provide return type 
            return "some details";
        }
    }

public class Frame extends AbstractFrame {
    public void setDetails(IInterface details) {
       System.out.println(details.getContent()); //output:- some details

    }
}

public class SomeClass{
public void someMethod(){
new Frame().setDetails(new InterfaceClass)
} 
}

Just pay note here to SomeClass. In SomeClass you are passing object of extended class i.e interfaceClass (and this is ploymorphism).

And like you did ,you need to pass object to interface variable like IInterface details=new InterfaceClass() or some any other class that implements IInterface but note that the class must extend thatIInterfaceand in your case it isinterfaceClass`

If you fail to that it will throw nullpointerExecption because it is just a reference variable , so you need to pass the object of actual class with that method and ploymorphism will handle rest.

note This is common way to access child classes and provides loose coupling and if you'll work on spring you'll know better.

Hope it helps

bananas
  • 1,176
  • 2
  • 19
  • 39