-3

I haven't included the main function since it's empty.I am trying to access the private variable name from my parent class to the child class but then it throws an error message.It would be nice if you could help me out.I am new to JAVA btw.

public class Author {
        private String name;
        private String email;
        private char gender;
        public Author(String name,String email,char gender)
        {
            this.name=name;
            this.email=email;
            this.gender=gender;         
        }
        protected String getName()
        {
            return name;
        }
        //protected String 

    }
    public class Book extends Author {
        private String name;
        private String authorname;
        private double price;
        private int qtyInStock;
        Book(String name,Author a,double price,int qtyInStock)
        {
            this.name=name;
            this.authorname=a.getName(); //Error
            this.price=price;
            this.qtyInStock=qtyInStock;
        }

    }

Error Message:

Implicit super constructor HelloWorld.Author() is undefined. Must explicitly invoke another constructor.

M Reza
  • 18,350
  • 14
  • 66
  • 71

2 Answers2

2

Implicit super constructor HelloWorld.Author() is undefined. Must explicitly invoke another constructor

The error message is not because you're accessing a protected variable, it's because Author has no default constructor. If a class does not have a default constructor (a constructor with no arguments), then you must supply the necessary arguments through a super call in any child classes.

Book(String name,Author a,double price,int qtyInStock)
{
    //pass variables to parent class
    super(a.getName(), a.getEmail(), a.getGender());
    this.name=name;
    this.authorname=a.getName(); //Error
    this.price=price;
    this.qtyInStock=qtyInStock;
}
Strikegently
  • 2,251
  • 20
  • 23
0

You are mixing several concepts. In your Author class you have a protected getName() function. This means that only Author classes and Author subclass can access it.

Further in your project you define a Book as being an Author. However, a Book is not an Author it has an Author.

Because you define your Book as extends Author the book constructor needs to call an explicit Author constructor. The Java compiler would call the 'default constructor' (Author()) if it could, but as you didn't also provide a default constructor it asks you to tell you what should be called.

My assumption is that you would rather have a structure where a Book has an Author:

public class Author {
  public enum Gender {
    FEMALE, MALE
  }
private final String name;
private final String email;
private final Gender gender;

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

public String getName() {
    return name;
}

public String getEmail() {
    return email;
}

public Gender getGender() {
    return gender;
}
}

class Book  {
  private final String name;
  private final double price;
  private final int qtyInStock;
  private final Author author;

  Book(String name,Author a,double price,int qtyInStock) {
    this.name =name;
    this.author = a;
    this.price = price;
    this.qtyInStock  = qtyInStock;
}

public String getName() {
    return name;
}

public double getPrice() {
    return price;
}

public int getQtyInStock() {
    return qtyInStock;
}

  public Author getAuthor() {
     return author;
 }
}

For type safety I have defined Gender as an enum, instead of a char, which has way to may acceptable values. Also I have extended your example to make the classes ìmmutable (the final fields, no set methods). You may or may not want that.

M. le Rutte
  • 3,525
  • 3
  • 18
  • 31