Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Calling methods via interface?

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    121
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Calling methods via interface?

    So lets say that we have two classes and one interface.

    "Main", "Person" and "Interface".
    If i wanted to call a method from Person to Main, would i have to call it from
    the Interface first or directly from the Person?
    Assuming that the interface doesn't have its own methods and such.

    Note that this is not related to any other question that i might've been posting recently.

    Thanks!
    "Tick, tack"

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,333
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: Calling methods via interface?

    call a method from Person to Main
    That's a confusing way to ask. Are you asking how a method in the Person class can call a method in the Main class?
    The same way as always: The code in the Person class must have a reference to an instance of the Main class:
       refToInstanceOfMain.methodInMain();    // call method in Main class

    An interface is a way of defining an attribute of a class. It guarantees that the class will have certain method(s) that can be called.

    Can you post a small, simple example of the problem? Both Jim and I have given you examples of how code can use an interface.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    121
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Calling methods via interface?

    Sure!
    For example :
    Main:
    package show;
     
    public class Main {
     
    	public static void main(String[] args) {
     
    		// Okay. So this just tests if i can do something or not.
    		// I can do it this way :
     
    		Grade pm = new Grade();
    		// And input a number like this
    		System.out.println("Did you pass? : " + pm.pass(4));
    		// This is only if the Interface doesn't have one or more methods to it, such as
    		// "Comparable".
     
    		// Here is another way of doing it.
    		PassMe interP = new Grade();
    		System.out.println("Did you pass? : " + interP.pass(4));
     
    		// I can call them from two different ways but which of these ways of calling
    		// one would be CORRECT if i
    		// was given an Interface with fields to begin with?
    	}
     
    }

    Grade Class:
    package show;
     
    public class Grade implements PassMe {
     
    	@Override
    	public boolean pass(int y) {
    		if (y > 10) {
    			return true;
    		}
    		return false;
    	}
     
    }

    And the Interface:
    package show;
     
    public interface PassMe {
     
    	boolean pass(int y);
     
    }

    And yes, thank you both.
    The reason to why i am asking this is because i was once given an assignment where i had an interface defined and not from the java classes.

    In other words, which one would be a correct way of calling it? And if i called it directly then wouldn't the interface just be useless at that point?
    Thanks and sorry if i am confusing.
    "Tick, tack"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,333
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: Calling methods via interface?

    if i called it directly
    You do not call an interface directly. A class implements an interface and defines the code for the required methods.
    Other code can then call those methods.

    An interface is just a label you can attach to a class's definition and which guarantees that the class will define the required methods.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    121
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Calling methods via interface?

    Quote Originally Posted by Norm View Post
    You do not call an interface directly. A class implements an interface and defines the code for the required methods.
    Other code can then call those methods.

    An interface is just a label you can attach to a class's definition and which guarantees that the class will define the required methods.
    Thank jesus for that hahaha
    Thanks again!
    "Tick, tack"

  6. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Calling methods via interface?

    The main purpose of an interface is to do just what its name implies. To provide a well known or defined interface between the class and the outside world (i.e. the other users of the class). So when you implement an ActionListener to listen for a button to be clicked, the button simply looks at its list of ActionListeners (provided via the addActionListener method of button) and see who wants to know about the event. Then the button calls the actionPerformed() as defined by the interface. If it wasn't for the interface the button would not know which method to call. If each listener had its own method to call, the button would not have any idea what that method was. But the interface provides that definition so there is no guess work.

    An interface can also be used to tie other methods together. Remember Norm's example of the animals talkng. Each Animal class implements the TalkToMe interface. The implementation specifies the response for that particular animal. The user does not need to know the animal or anything about how they sound other than to call the speak() method. And when that is done, the animal talks in its "native" tongue. A similar interface could be written called SupperTime with a feedMe() method. And the result would be the food for the particular animal.

    One final example. Wouldn't it be nice if every class that had the need to provide a Color of itself, implemented a ProvideMyColor interface with a method getColor(). Then you could do the following:

    Color color;
    If (Someclass instanceof ProvideMyColor) {
    color = Someclass.getColor();
    } else {
    // select a default color or ignore
    }

    Regards,
    Jim

  7. #7
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Calling methods via interface?

    When you say "person", is this from any program? Say like directly from the web browser. The interface is the java program itself. Ok clearer now. Thanks...

Similar Threads

  1. Need help with calling methods in Java.
    By searock in forum Java Theory & Questions
    Replies: 2
    Last Post: February 2nd, 2013, 01:22 PM
  2. Calling upon methods
    By jonathanfox in forum Java Theory & Questions
    Replies: 4
    Last Post: August 3rd, 2012, 11:58 AM
  3. Calling methods in other files
    By zdavos in forum Object Oriented Programming
    Replies: 4
    Last Post: March 2nd, 2012, 06:57 AM
  4. [SOLVED] Calling methods from other classes
    By knightknight in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 7th, 2011, 06:16 PM
  5. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2010, 11:13 PM