On 1/6/2013 2:19 PM, Deepak A L wrote:

Hi Joel/Sang,
Can u plz provide the complete working example for the below
Example 3--polymorphic behaviour.


OK. The new hands-on lab has been uploaded. I ended up modifying interface code from

public interface BookInterface extends ProductInterface{
    public String getPublisher();
    public void setPublisher(String publisher);
    public int getYearPublished();
    public void setYearPublished(int yearPublished);
}

to

public interface BookInterface {
    public String getPublisher();
    public void setPublisher(String publisher);
    public int getYearPublished();
    public void setYearPublished(int yearPublished);
}

while the Book.java as it is  as shown below

public class Book extends Product implements BookInterface{

    private String publisher;
    private int yearPublished;

    /** Creates a new instance of Book */
    public Book(double regularPrice,
            String publisher,
            int yearPublished) {
        super(regularPrice);
        this.publisher = publisher;
        this.yearPublished = yearPublished;
    }
    ...

This way, we are still showing Interrface-based (ProductInterface.java)
polymorphism while removing the redundancy Joel has pointed out
in his original question.

-Sang

-----
Deepak

On Jan 6, 2013 9:23 PM, "Sang Shin" <[email protected] <mailto:[email protected]>> wrote:

    On 1/5/2013 5:53 PM, Joel wrote:
    Hi Sang,

    In studying the Example 3 (Polymorphic behavior via Java
    Interface) in the Polymorphism lab of Javase, there is an
    interface class defined as follows:

    public interface BookInterface extends ProductInterface {
    public String getPublisher();
    public void setPublisher(String publisher);
    public int getYearPublished();
    public void setYearPublished(int yearPublished);
    }

    Why does this class need to extend ProductInterface? I ask
    because Product class already implements ProductInterface. So
    when you define the Book class as:

    public class Book extends Product*implements BookInterface*{ ...}

    ...it seems as though the Book class inherits ProductInterface
    twice: once from extending Product and again in the BookInterface
    implementation.

    Your observation indeed is correct.


    _I modified the BookInterface definition by removing extends
    ProductInterfaceand the code runs fine without error.

    _So my questions are:
    Is there a reason you extend BookInterface with ProductInterface?
    Or is this simply superfluous coding?

    The latter. :-)  Apparently the only reason it also extends
    Product class is
    to use a constructor method of the Product class but it does not have
    to as you pointed out.  (The sample code actually uses this

    I also found there is some discrenpancy between the document and
    the code sample, which I am in the process of cleaning up.


    And why does the compiler not complain in Book class that it
    inherits ProductInterface interface twice: once from Product and
    again from BookInterface? Is it Ok to inherit the same abstract
    methods twice like this?

    Correct.  This is a not compiler error.

    -Sang


    Joel
-- You received this message because you are subscribed to the
    Google Groups "JPassion.com: Java Programming" group.
    To unsubscribe from this group, send email to
    [email protected]
    <mailto:[email protected]>.
    Visit this group at
    http://groups.google.com/group/jpassion_java?hl=en-US.




-- -------------------------------------------------------------------
                  Sang Shin,[email protected]  
<mailto:[email protected]>
              http://www.linkedin.com/in/javapassion  (Linkedin)
               http://twitter.com/javapassion  (Tweeter)
                 Life is worth living... with Passion!

    Free Webinars: "Ruby on Rails", "MySQL", "Java EE 6", "Java Performance"
http://javapassion.com/webinars ----------------------------------------------------------------------

-- You received this message because you are subscribed to the Google
    Groups "JPassion.com: Java Programming" group.
    To unsubscribe from this group, send email to
    [email protected]
    <mailto:jpassion_java%[email protected]>.
    Visit this group at
    http://groups.google.com/group/jpassion_java?hl=en-US.




--
-------------------------------------------------------------------
             Sang Shin, [email protected]
         http://www.linkedin.com/in/javapassion (Linkedin)
          http://twitter.com/javapassion (Tweeter)
            Life is worth living... with Passion!

Free Webinars: "Ruby on Rails", "MySQL", "Java EE 6", "Java Performance"
             http://javapassion.com/webinars
----------------------------------------------------------------------

--
You received this message because you are subscribed to the Google Groups 
"JPassion.com: Java Programming" group.
To unsubscribe from this group, send email to 
[email protected].
Visit this group at http://groups.google.com/group/jpassion_java?hl=en-US.


Reply via email to