Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Norm

Thread: Interface trouble

  1. #1
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Interface trouble

    My book is giving too much information about how to use interfaces at once. In fact, it doesn't even explain how the "private static" access modifier works or where to put the printMultiple method. To start, here is my interface:
    Printable.java:
    Java Code:
    package java_interface.business;
    
    public interface Printable {
    	void print();//this method is automatically public and abstract
    	
    	private static void printMultiple(Printable p, int count){
    		for(int i = 0; i < count; i++){
    			p.print();
    		}
    	}
    }
    Yes, I know that defining private methods in interfaces is not allowed, but my book gave it as a code snippet the way it is. So, not only do I not understand what the "private static" access modifier does, but I also don't know where to put that printMultiple method in my program, or even why it has to be private static. Hopefully someone with more Java intuition can help me with that.
    Next, here is the superclass:
    Product.java:
    Java Code:
    package java_interface.business;
    import java.text.NumberFormat;
    
    public class Product implements Printable{
    	private String code;
    	private String description;
    	private double price;
    	
    	public Product(String code, String description, double price){
    		this.code = code;
    		this.description = description;
    		this.price = price;
    	}
    	
    	public void setCode(String code){
    		this.code = code;
    	}
    	public String getCode(){
    		return code;
    	}
    	public void setDescription(String description){
    		this.description = description;
    	}
    	public String getDescription(){
    		return description;
    	}
    	public void setPrice(double price){
    		this.price = price;
    	}
    	public double getPrice(){
    		return price;
    	}
    	
    	public void print(){
    		System.out.println(description);
    	}
    }
    And finally, here is my class with the main method:
    InterfaceApp.java:
    Java Code:
    package java_interface.ui;
    import java_interface.db.*;
    import java_interface.business.*;
    
    public class InterfaceApp {
    	public static void main(String [] args){
    		Product product = new Product("Beat", "Character from Jet Grind Radio", 9.99);
    		printMultiple(product, 2);//error: The method printMultiple(Product, int) is undefined for the
    		//type InterfaceApp
    	}
    }
    Hopefully after we find out where printMultiple interface method is supposed to go, we can address the error in the code comment of the InterfaceApp.java code above.

    Due to the print() method in Product.java that makes the product's description print out, my intention is to have the printMultiple method output "Character from Jet Grind Radio" 2 times.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Interface trouble

    You can put the method anywhere you would put any other method within the class. The difference is that you must provide a
    body to the method. When properly defined you can access a static method within the interface declaration by either doing
    an import static or Printable.printMultiple(). And as of Java 8 you can also have default methods.


    What is the name of the book you are using (publisher and edition number)?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Interface trouble

    Quote Originally Posted by jim829 View Post
    What is the name of the book you are using (publisher and edition number)?
    Murach's Beginning Java with Eclipse
    Edition ? [copy write is 2015]
    Quote Originally Posted by jim829 View Post
    You can put the method anywhere you would put any other method within the class.
    Quick NOOB question. What is the difference between static and public again? And why would you ever want to make a method private static?
    Quote Originally Posted by jim829 View Post
    The difference is that you must provide a body to the method.
    So you're telling me that the printMultiple() method doesn't have a body already? The curly braces aren't exactly empty:
    Java Code:
    private static void printMultiple(Printable p, int count){
    	for(int i = 0; i < count; i++){
    		p.print();
    	}
    }
    Quote Originally Posted by jim829 View Post
    When properly defined you can access a static method within the interface declaration by either doing
    an import static or Printable.printMultiple().
    import static what?

    Anyway, here's my updated code, with the errors and warnings in the code comments:

    Printable.java:
    Java Code:
    package java_interface.business;
    
    public interface Printable {
    	void print();//this method is automatically public and abstract	
    }
    Product.java:
    Java Code:
    package java_interface.business;
    import java.text.NumberFormat;
    
    public class Product implements Printable{
    	private String code;
    	private String description;
    	private double price;
    	
    	public Product(String code, String description, double price){
    		this.code = code;
    		this.description = description;
    		this.price = price;
    	}
    	
    	public void setCode(String code){
    		this.code = code;
    	}
    	public String getCode(){
    		return code;
    	}
    	public void setDescription(String description){
    		this.description = description;
    	}
    	public String getDescription(){
    		return description;
    	}
    	public void setPrice(double price){
    		this.price = price;
    	}
    	public double getPrice(){
    		return price;
    	}
    	
    	public void print(){
    		System.out.println(description);
    	}
    	
    	private static void printMultiple(Printable p, int count){//warning: The method 
    		//printMultiple(Printable, int) from the type Product is never used locally
    		for(int i = 0; i < count; i++){
    			p.print();
    		}
    	}	
    }
    InterfaceApp.java:
    Java Code:
    package java_interface.ui;
    import java_interface.db.*;
    import java_interface.business.*;
    
    public class InterfaceApp implements Printable{
    	public static void main(String [] args){
    		Product product = new Product("Beat", "Character from Jet Grind Radio", 9.99);
    		Printable.printMultiple(product, 2);//error: The method printMultiple(Product, int) is undefined for the
    		//type Printable
    		
    		print();//Cannot make a static reference to the non-static method print() from the type Printable
    	}
    }

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Interface trouble

    PrintMultiple is not an abstract method since it has a body. But you still need to implement the print() method.

    Comparing static to public does not make sense.

    Public vs private vs protected vs package private are access modifiers.
    Static (class methods and fields) and non-static (instance methods and fields) can
    contrasted as to what they offer.

    All the above are covered in the tutorials in my signature.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Interface trouble

    If they're only just introducing interfaces then I'm not sure why they're also talking about static methods in interfaces.
    Indeed, I'm not sure why they're talking about it in the first place...I've never seen one out in the wild.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Interface trouble

    I assume you are familiar with the concept of function prototypes in C++? If so, then an interface in java, if I'm understanding correctly, is like an encapsulation of method prototypes (some with method bodies) conveniently contained in a separate implementable file. Is that accurate?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Interface trouble

    I think of interfaces as labels that can be attached to an object. A person coming upon an object can read one of the labels attached to the object and know what methods it has that can be useful for some project.
    Sam_JavaTheHut5580 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Interface trouble

    An interface is simply a contract.
    Something that implements an interface is simply saying that it fulfils the contract.

    Until Java 8 there were no default implementations for those methods.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 11
    Last Post: 11-05-2015, 07:39 PM
  2. Replies: 5
    Last Post: 12-29-2014, 04:28 PM
  3. Replies: 1
    Last Post: 07-08-2012, 05:23 PM
  4. Interface trouble, cannot override method
    By Tom_Snake in forum New To Java
    Replies: 1
    Last Post: 02-13-2012, 12:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •