I am trying to do my assignment but I am having a problem instantiating an object that has a String parameter. when I compile and run what I have of the application so far, it returns the String value of "Null" instead of what I expect it to.
This is my abstract Superclass
public abstract class Book
{
//Declaration of class variable
private String title;
protected double price;
// contructor for Book class objects
public Book(String bookTitle)
{
bookTitle = title;
}
//method that gets and returns books title
public String getTitle()
{
return title;
}
//method that gets and returns books price
public double getPrice()
{
return price;
}
//abstract method with no parameters
public abstract void setPrice();
}
This is my subclass
public class Fiction extends Book
{
//subclass contructor
public Fiction(String bookTitle)
{
//calling superclass constructor
super(bookTitle);
}
//override annotation and setPrice method override
@Override
public void setPrice()
{
price = 19.99;
}
}
This is my main method class where the object fictionBook is supposed to be instantiated with the title The White Unicorn. However, for some reason my println is printing out null instead
public class BookTester
{
//Main method
public static void main(String[] args)
{
//Instantiate object
Fiction fictionBook = new Fiction("The White Unicorn");
NonFiction nonFictionBook = new NonFiction("Autobiography of Curtis Sizemore");
//call to the setPrice() method
fictionBook.setPrice();
nonFictionBook.setPrice();
//Print information on books
System.out.println("The Fiction book titled \"" + fictionBook.getTitle() + "\"costs $" + fictionBook.getPrice());
}
}
I cannot figure out what the problem is. Can anyone assist me? I also have a subclass for the nonFiction book, but I have not gotten to that point yet.