The Artima Developer Community
Sponsored Link

Java Answers Forum
Polymorphsim:

1 reply on 1 page. Most recent reply: Oct 18, 2002 11:51 AM by Don Hill

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Stanley Umoh

Posts: 1
Nickname: stanumoh
Registered: Oct, 2002

Polymorphsim: Posted: Oct 18, 2002 12:54 AM
Reply to this message Reply
Advertisement
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 = "";

for(int i = 0;i < author.length();i ++) {
char currentChar = author.charAt(i);
if (currentChar >= 'A' && currentChar <='Z') {
initials = initials + currentChar + '.';
}
}
return initials;
}

/** This method returns true if both the receptor and the
* argument correspond to the same book
*/

public boolean equals(Book b) {
return (title == b.title && author.equals(b.author) &&
numberOfPages == b.numberOfPages &&
ISBN.equals(b.ISBN));
}

/** This method sets the owner of the book
*/

public void setOwner(String name) {
owner = name;
}

/** This method gets the owner of the book
*/

public String getOwner() {
return owner;
}

/** This method returns a description of the book
*/

public String description() {
return "Book instances can store information on books";
}
}


/**
* Q19
* Novel Book Class
*/

class NovelBook extends Book {
String typeOfNovel;
boolean foreign = false;

NovelBook(String tit,String aut,int num,String isbn,String a) {
super(tit,aut,num,isbn);
typeOfNovel = a;
}

public boolean equals(NovelBook nb) {
return super.equals(nb) && typeOfNovel.equals(nb.typeOfNovel ) &&
foreign == nb.foreign;
}

public String description() {
return "NovelBook instances can store information" +
" on Novel books";
}

public void setForeign() {
foreign = true;
}

public String printBookDetails() {
String s=title + " " +author + " " + numberOfPages + " " + ISBN + " " + typeOfNovel;
return s;
}
}


/**
* Scientific Book Class
*/

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");


for(int i=0; i < book.length; i++) {
System.out.println(book.getInitials());
System.out.println(book.description());
System.out.println(book.equals(book));
}

System.out.println(book2.description());

}
}


Don Hill

Posts: 70
Nickname: ssswdon
Registered: Jul, 2002

Re: Polymorphsim: Posted: Oct 18, 2002 11:51 AM
Reply to this message Reply
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.

hth

Flat View: This topic has 1 reply on 1 page
Topic: Runing Precompiled JSP with Tomcat Previous Topic   Next Topic Topic: help on applets

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use