javaNewb
javaNewb

Reputation: 29

java creating an object in subclass

Having trouble creating an author object in the Book class.This is indeed Homework, I have came up with all the methods on my own and have been staring at this assignment for 2 hours now. any tips hints will be appreciated. I believe I am only allowed this one Author constructor with 3 parameters otherwise I would just have made an Author constructor with no arguments and the problem would be gone.

public class Author {

    protected String name;
    protected String email;
    protected char gender;

    public Author(String name, String email, char gender)
    {
        this.name = name;
        this.email = email;
        this.gender = gender;
    }

    public String getName()
    {
        return name;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public char getGener()
    {
        return gender;
    }

    public String toString()
    {
        return ( name + "(" + gender + ")@" + email);
    }


}

public class Book extends Author{

    private String name;
    private Author author;
    private double price;
    private int qtyInStock = 0;

    public Book(String name, Author author,Double price)
    {
        this.author = new author;
        this.name = name;
        this.price = price;

    }

    public Book(String name, Author author, double price, int qtyInStock)
    {

        this.name = name;
        this.author = author;
        this.price = price;
        this.qtyInStock = qtyInStock;
    }

    public String getName()
    {
        return name;
    }

    public Author getAuthor()
    {
        return author;
    }

    public double getPrice()
    {
        return price;
    }

    public void setPrice(double price)
    {
        this.price = price;
    }

    public int getQtyInStock()
    {
        return qtyInStock;
    }

    public void setQtyInStock(int qtyInStock)
    {
        this.qtyInStock = qtyInStock;
    }

    public String toString()
    {
        return (name + " by " + author + "(" + super.gender + ")at" + super.email);
    }
}

Upvotes: 1

Views: 276

Answers (7)

Matt
Matt

Reputation: 3760

Seems strange that Book extends Author. A Book is not an Author.

I think what you want to do is create a Book object that has an Author, but you are already passing in an Author to your Book constructor, so what is the problem?

class Book {
    public Book(String title, Author author) {
        this.title = title;
        this.author = author;
    }
}

If you're wondering how to create the Author, just create it before you pass it to the Book.

Author author = new Author("bob", "[email protected]", 'm');
Book book = new Book("some title", author);

Does that make sense?

Upvotes: 0

javaNewb
javaNewb

Reputation: 29

public class Book extends Author{

private String name;
//private final String email;
// private char gender;
private Author author;// = new Author(name,super.email,super.gender);
private double price;
private int qtyInStock = 0;

public Book(String name, Author author,double price)
{
    //super();
    this.author = author;// new Author(name,super.email,super.gender);
    super.name = author.getName();
    super.gender = author.getGender();
    super.email = author.getEmail();
    this.name = name;
    this.price = price;

}

public Book(String name, Author author, double price, int qtyInStock)
{
    this.author = author;
    super.name = author.getName();
    super.gender = author.getGender();
    super.email = author.getEmail();
    this.name = name;
    this.price = price;
    this.qtyInStock = qtyInStock;
}

public String getName()
{
    return name;
}

public Author getAuthor()
{
    return author;
}

public double getPrice()
{
    return price;
}

public void setPrice(double price)
{
    this.price = price;
}

public int getQtyInStock()
{
    return qtyInStock;
}

public void setQtyInStock(int qtyInStock)
{
    this.qtyInStock = qtyInStock;
}

public String toString()
{
    return (super.name + " by " + this.name + "(" + super.gender + ")at" + super.email);
}

also added this to the author class

 public Author()
{

}

Upvotes: 0

Dicky Ho
Dicky Ho

Reputation: 244

Besides changing tothis.author = author. You also need to add super(name, name, 'M'); to the two constructors of the Book class.

Upvotes: 0

Chetan Lovanshi
Chetan Lovanshi

Reputation: 16

you should replace new author with author, also when you call the constructor of a sub Type, the Parent class's default constructor is invoked by default. so you need to specify a default constructor in your parent class, or to prevent this explicitly call your parametric constructor of parent class in the child class constructor .

Upvotes: 0

SomeJavaGuy
SomeJavaGuy

Reputation: 7347

There are multiple mistakes in your code:

  1. as @Guy said, you need to assign the author with this.author = author, without the new keyword.
  2. you are shadowing the name variable from the Author class by also including a variable with the same name in the Book class. Remove that variable and the getter and setter methods from the Book class.
  3. You don´t have a no parameter constructor, so you have to call the parent constructor in your Book class by calling super(name, email, gender);.

Upvotes: 0

Sindhoo Oad
Sindhoo Oad

Reputation: 1194

You are inheriting Author in Book, inheritance is is a relationship, means Book is a Author which is not true in real world scenario , you need has a relation which makes sense , that Book has a Author.

So don't extend author in Book , keep a field of author in it as you have already did. Change you design.

Upvotes: 0

Guy
Guy

Reputation: 50809

It should be this.author = author; without the new keyword.

You are assigning the Author object in the constructor, not creating new one.

By the way, Book inherits from Author which means it already has the Author functionality. You don't have to save it as member.

Upvotes: 2

Related Questions