using Java interfaces

Posted by mike_hornbeck on Stack Overflow See other posts from Stack Overflow or by mike_hornbeck
Published on 2010-03-28T19:33:06Z Indexed on 2010/03/28 19:43 UTC
Read the original article Hit count: 242

Filed under:

I need to create interface MultiLingual, that allows to display object's data in different languages (not data itself, but introduction like "Author", "Title" etc.).

Printed data looks like this :

3 grudnia 1998

10th of June 1924

Autor: Tolkien
Tytul: LoTR
Wydawnictwo: Amazon 2010

Author: Mitch Albom
Title: Tuesdays with Morrie
Publishing House: Time Warner Books 2003   

37 360,45 PLN

5,850.70 GBP

3rd of December 1998

10th of June 1924

Author: Tolkien
Title: LoTR
Publishing House: Amazon 2010

Author: Mitch Albom
Title: Tuesdays with Morrie
Publishing House: Time Warner Books 2003

37,360.45 GBP

5,850.70 GBP

Test code looks like this :

public class Main {

public static void main(String[] args){

  MultiLingual gatecrasher[]={ new Data(3,12,1998),
                               new Data(10,6,1924,MultiLingual.ENG),
                               new Book("LoTR", "Tolkien", "Amazon", 2010),
                               new Book("Tuesdays with Morrie",
                                        "Mitch Albom", "Time Warner Books",2003,
                               new Money(1232895/33.0,MultiLingual.PL),
                               new Money(134566/23.0,MultiLingual.ENG),

  for(int i=0;i < gatecrasher.length;i++)

  for(int i=0;i < gatecrasher.length;i++)


So i need to introduce constants ENG, PL in MultiLingual interface, as well as method get(int language) :

public interface MultiLingual {

int ENG = 0;
int PL= 1;

String get(int lang);


And then I have class Book. Problem starts with the constructors. One of them needs to take MultiLingual.ENG as argument, but how to achieve that ? Is this the proper way? :

class Book implements MultiLingual {

private String title;
private String publisher;
private String author;

public Book(String t, String a, String p, int y, MultiLingual lang){      

Or should I treat this MultiLingual.ENG as int variable , that will just change automatically constants in interface?

Second constructor for book doesn't take MultLingual as argument, but following implementation is somehow wrong :

public Book(String t, String a, String p, int y){
    Book someBook = new Book(t, a, p, y, MultiLingual m);

I could just send int m in place of MultiLingual m but then I will have no control if language is set to PL or ENG.

And finally get() method for Boook but I think at least this should be working fine:

public String get(int lang){

    String data;
    if (lang == ENG){
        data = "Author: ""\n"+
                "Title: "+this.title+"\n"+
                "Publisher: "+this.publisher+"\n";
    else {
        data = "Autor: ""\n"+
                "Tytul: "+this.title+"\n"+
                "Wydawca: "+this.publisher+"\n";            

    return data;

public String toString(){
    return "";


© Stack Overflow or respective owner

Related posts about java

Related posts about interface