Simple inheritance question

Discussion in 'Java' started by knocte, Jul 17, 2004.

  1. knocte

    knocte Guest

    class A implements Cloneable {
    [...]

    public Object clone() {
    [...]
    }
    }

    class B extends A {
    [...]
    }



    Does B need to implement Cloneable interface to be cloneable? Or it is
    not necessary because B is subclass of A and inherits its interfaces?

    Thanks in advance.
    knocte, Jul 17, 2004
    #1
    1. Advertising

  2. knocte

    VisionSet Guest

    "knocte" <> wrote in message
    news:cdbfar$ip6$-data.net...
    > class A implements Cloneable {
    > [...]
    >
    > public Object clone() {
    > [...]
    > }
    > }
    >
    > class B extends A {
    > [...]
    > }
    >
    >
    >
    > Does B need to implement Cloneable interface to be cloneable? Or it is
    > not necessary because B is subclass of A and inherits its interfaces?
    >


    subclasses inherit interfaces of superclass, so class B is Cloneable.
    Obviously you must override clone().

    --
    Mike W
    VisionSet, Jul 17, 2004
    #2
    1. Advertising

  3. knocte

    Dave Neary Guest

    Hi,

    In article <6scKc.529$%>, VisionSet wrote:
    > "knocte" <> wrote:
    >> class A implements Cloneable {
    >> [...]
    >>
    >> public Object clone() {
    >> [...]
    >> }
    >> }
    >>
    >> class B extends A {
    >> [...]
    >> }
    >>

    > Obviously you must override clone().


    *must*? I don't think he must. Although

    B b1 = new B();
    B b2 = (B)b1.clone();

    would (probably) give a ClassCastException, there is no obligation to
    override clone().

    Cheers,
    Dave.
    Dave Neary, Jul 19, 2004
    #3
  4. knocte

    Adam Maass Guest

    "Dave Neary" <> wrote in message
    news:...
    > Hi,
    >
    > In article <6scKc.529$%>, VisionSet wrote:
    > > "knocte" <> wrote:
    > >> class A implements Cloneable {
    > >> [...]
    > >>
    > >> public Object clone() {
    > >> [...]
    > >> }
    > >> }
    > >>
    > >> class B extends A {
    > >> [...]
    > >> }
    > >>

    > > Obviously you must override clone().

    >
    > *must*? I don't think he must. Although
    >
    > B b1 = new B();
    > B b2 = (B)b1.clone();
    >
    > would (probably) give a ClassCastException, there is no obligation to
    > override clone().
    >
    > Cheers,
    > Dave.
    >


    No, there would be no ClassCastException in this code if the clone() method
    in A is coded correctly. Namely:

    public Object clone(){ return super.clone(); }


    Note that this does not use A's constructor; you get a B when executed in a
    B.


    -- Adam Maass
    Adam Maass, Jul 19, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,120
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    806
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,026
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,880
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    764
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page