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 5 of 5

Thread: General Question on Interfaces

  1. #1
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default General Question on Interfaces

    Hello everyone,
    Looking for someone who will be so kind as too possible give me their interpretation of what an interface is good for.
    I need to implement one in my assignment and no matter what I read, video's I watch, etc. I am still confused on what the are actually good for.

    I am just practicing with it before implementing it in my assignment so this is what I have,

    package interfacesamplecode;
     
    public interface highScores {
     
       String name = "John";
       int score = 150;
     
        void allScores(String name, int score);
    }

    package interfacesamplecode;
     
    public class InterfaceSampleCode implements highScores {
     
            @Override
        public void allScores(String name, int score) {
                System.out.println(highScores.name);
                System.out.println(highScores.score);
        }
     
     
        public static void main(String[] args) {       
            InterfaceSampleCode newHighScore = new InterfaceSampleCode();      
            newHighScore.allScores(name, score);
        }
    }

     

    run:
    John
    150
    BUILD SUCCESSFUL (total time: 0 seconds)




    To me it seems like you can get the same results if you have the String variables in just another class and you call that class, or even extend that class. You should be able to get the same results, this is were I am having trouble understanding what an interface would actually be good for.

    If you can, could you give some real world examples on how they could improve a Java program.

    Not looking for any code, just why one would use them.

    Thanks,
    Last edited by CADman; August 13th, 2021 at 03:59 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: General Question on Interfaces

    what an interface is good for.
    Look at the APIs examples: ActionListener, List and Map and many others
    They allow a class to tell the world what set of methods it supports.
    One class can implement many interfaces.


    See the tutorial: https://docs.oracle.com/javase/tutor...interface.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    CADman (August 13th, 2021)

  4. #3
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: General Question on Interfaces

    If I understood that tutorial, the main benefit of an interface is so that any third party user, has to adhere to your rules.
    So if you are designing a stand alone program or phone app, an interface is really not necessary.
    Would this be a safe assumption.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: General Question on Interfaces

    A class that does not call any other classes would probably not need any interfaces.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Aug 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: General Question on Interfaces

    To address your question "I am still confused on what the are actually good for."
    Interfaces are only option when you want to implement Mutiple Inheritence in Java which is not possible by using mutiple classes.

Similar Threads

  1. Question about interfaces
    By Skywola in forum Java Theory & Questions
    Replies: 3
    Last Post: November 26th, 2013, 05:17 AM
  2. General question about Interfaces
    By michael.duffy31 in forum Java Native Interface
    Replies: 5
    Last Post: June 30th, 2013, 02:44 PM
  3. Question Regarding Interfaces
    By davidrobin in forum Object Oriented Programming
    Replies: 1
    Last Post: April 18th, 2013, 04:10 PM
  4. General Question; Need Ideas
    By clydefrog in forum Java Theory & Questions
    Replies: 2
    Last Post: March 28th, 2012, 04:42 PM
  5. General Question
    By Becca in forum Collections and Generics
    Replies: 6
    Last Post: November 3rd, 2011, 03:52 PM