Search code examples
javaabstractsuperclass

constructor Book in class Book cannot be applied to given types;


in main

Book new_novel=new MyBook(title,author,p);

Abstract Class:

abstract class Book 
{

    Book(String t,String a){
       title=t;
       author=a;
    }
}

Inherited Class:

class MyBook extends Book{

    MyBook(String t,String a,int p){
        t= super.title;
        a = super.author;
        price = p;
    }         

}

Solution

  • As user ppeterka suggested in the comments, some Java tutorials would do you wonders. Here's a great place to start: https://docs.oracle.com/javase/tutorial/

    However, here's a little help:

    You need to define fields in Book and MyBook. For example:

    public class Book {
    
        private String title;
        private String author;
        ...
    }
    

    As for the constructor, since MyBook extends Book, MyBook's constructor can call Book's constructor like this:

    MyBook(String t,String a,int p){
        super(t, a); // Calls Book(title, author)
        price = p;
    }
    

    As an aside, user coder's statement that

    you can't define object of type Book because Book is abstract

    is false. Your code Book new_novel=new MyBook(title,author,p); is a perfectly good line of code assuming you've defined title author and p already.

    I hope you enjoy learing Java!