Justin Michel
Justin Michel

Reputation: 113

Inheritance and private/public variables

So I have three classes: books, booksOut and Allbooks. booksout and Allbooks are extensions/ are subclasses of books. However I have a problem/ question:

I get an error from allBooks in the toString methods. Saying that bookID etc. (i.e. all the variables in books ) are set to private. I was taught that you should always try to avoid public variable, so I am hesitant to do it. Is there another way? Or is making them public the easiest/best way to handle this?

Here's the code:

Books

public class books {

private int bookID;
private String title;
private String author;


public books() {
}

public books(int bookID, String title, String author) {
    this.bookID = bookID;
    this.title = title;
    this.author = author;
}

public int getBookID() {
    return bookID;
}

public void setBookID(int bookID) {
    this.bookID = bookID;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}


public String addSpaces(String s, int w) {
    String spc = "";
    for (int i = 1; i <= (w - s.length()); i++) {
        spc = spc + " ";
    }
    return spc;
}

public class AllBooks extends books{
private String genre;
private String status;
private String Location;
private String condition;

Allbooks

public AllBooks(int bookID, String title, String author, String genre, String status, String Location, String condition ) {
    super(bookID, title, author);
    this.genre = genre;
    this.status = status;
    this.Location = Location;
    this.condition = condition;
}

public String getGenre() {
    return genre;
}

public void setGenre(String genre) {
    this.genre = genre;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getLocation() {
    return Location;
}

public void setLocation(String Location) {
    this.Location = Location;
}

public String getCondition() {
    return condition;
}

public void setCondition(String condition) {
    this.condition = condition;
}



    @Override
public String toString() {
    String stg = "";
    stg = stg + bookID + '\t' + title + addSpaces(title, 30) + author + addSpaces(author, 30) + genre + addSpaces(genre, 15) + status + addSpaces(status, 5) + Location + addSpaces(Location, 20) + condition;
    return stg;
}

    public String toString(int i){
    String stg = "";
    stg += bookID + "#" + title + "#" + author + "#" + genre + "#" + status + "#" + Location + "#" + condition + "#";
    return stg;
}

PS

I am sorry if this is a stupid question. This is a school project and it is due for after my holidays, which I am on now, thus the reason why I don't ask my teacher. I did check online for the answer however the tutorials I came across did not mention much about etiquette. Thanks for your help, and/or sorry for wasting your time.

Upvotes: 1

Views: 175

Answers (3)

Andy Thomas
Andy Thomas

Reputation: 86509

Use the existing public accessors. For example, use getBookID() in the subclass rather than attempting to access the superclass's private field directly.

@Override
public String toString() {
    String stg = getBookID() + '\t' + getTitle() 
        + addSpaces(title, 30) + getAuthor() 
        + addSpaces(author, 30) + getGenre() 
        + addSpaces(genre, 15) + getStatus() 
        + addSpaces(status, 5) + getLocation() 
        + addSpaces(Location, 20) + getCondition();
    return stg;
}

An alternative would be to make the field protected. However, this allows a potentially arbitrary number of subclasses to couple directly to your superclass's representation. Encapsulating the data behind a method is usually preferable.

Incidentally, you might find String.format() useful. For the syntax, see Format String Syntax.

Upvotes: 6

Ric
Ric

Reputation: 1144

As pointed by Andy Thomas you can directly use the public method to access it.The public method is in effect encapsulating the private variable.

In case you still need to use it directly you should make it protected, which implies that only classes extending it can access it.

Upvotes: 0

H3XXX
H3XXX

Reputation: 647

Since you already have a getter for bookID, can't you just use that in your other class, for example:

System.out.println(books.getBookID());

Upvotes: 1

Related Questions