Dear Sir, I have a problem with a question involving polymorphism. Here is the question:
Develop an application that creates an array of books, and initialize it with instances of Books, Scientific books and Novels. The application then has to call the methods getInitials, description and equals on all instances of the array inside a loop. Check with the debugger that the expected methods get called. Does the behaviour of the application agree with your idea of polymorphism?
The classes Book, Scientific books and Novels are listed below. Scientific books and Novels are inherited from Book.
The method description in Book,Scientific books and Novels is declared:
public static String description()
When Class TestArrayBooks is run, the output from
System.out.println(book.description()); is
Book instances can store information on books
for all instances of Book, Scientific books and Novels. Therfore not polymorphic.
However, when static is removed form all declaration of description(), the behaviour is polymorphic.
Finally, if an instance of Scientific is initialised, and calls the method description(), a print of the output is what I expect.
My question is why does declaring description() STATIC override the polymorphic behaviour.
/** * Books Application */
class Book { String title; String author; int numberOfPages; String ISBN; static String owner;
/** This constructor creates a Book with a specified title, * author, number of pages and unknown ISBN */
Book(String tit,String aut,int num) { title = tit; author = aut; numberOfPages = num; ISBN = "unknown"; }
/** This constructor creates a Book with a specified title, * author, number of pages and ISBN */
Book(String tit,String aut,int num,String isbn) { title = tit; author = aut; numberOfPages = num; ISBN = isbn; }
/** This method returns a string containing the initials of * the author */
public String getInitials() { String initials = "";
class ScientificBook extends Book { String area; boolean proceeding = false;
/** This constructor creates a Scientific Book with a * specified title, author, number of pages, ISBN and * area. Proceeding is set to false */
ScientificBook(String tit,String aut,int num,String isbn,String a) { super(tit,aut,num,isbn); area = a; }
/** This method returns true if both the receptor and the * argument correspond to the same book */
public boolean equals(ScientificBook b) { return super.equals(b) && area.equals(b.area ) && proceeding == b.proceeding; }
/** This method returns a description of the book */
public String description() { return "ScientificBook instances can store information" + " on scientific books"; }
/** This method sets proceeding to true */
public void setProceeding() { proceeding = true; }
/** This method sets proceeding to false */
public boolean isProceeding() { return proceeding; } }
/** * Q20 * Filename: TestArrayBooks * * Program that uses a single variable to refer * to objects of different classes and calls the * appropriate overriden method. */
class TestArrayBooks { public static void main(String[] args) {
Book book[] = new Book[3]; ScientificBook book2;
book[0] = new ScientificBook("Neural Networks1, A Comprehensive " + "Foundation","Simon Haykin",696,"0-02-352761-7", "Artificial Intelligence1");
book[1] = new NovelBook("Novel Networks1, A Comprehensive " + "Foundation","Simon Umoh",69,"0-02-352761-7", "Drama"); book[2] = new Book("Novel Networks1, A Comprehensive " + "Foundation","Simon Umoh",69,"0-02-352761-7");
book2 = new ScientificBook("Neural Networks1, A Comprehensive " + "Foundation","Simon Haykin",696,"0-02-352761-7", "Artificial Intelligence1");
The reason is that there is only 1 method for the class because you declared it static, and since your array is of type Book it calls the static for book.
The correct way to do this is create a interface and have all your classes implement the interface,then they would be required to implement thier own desc method.