Results 1 to 5 of 5

Thread: Genericity and Inheritance

  1. #1
    Join Date
    Jan 2009
    Posts
    638

    Genericity and Inheritance

    I am facing a problem quite unique I put you below.
    Consider first the following abstract class:
    Code:
    public abstract class Lattice<E extends Pattern> {
    public Lattice () {}
    public abstract E meet(E pattern1, E pattern2);
    public abstract E join(E pattern1, E pattern2);
    public abstract boolean isSubsuming(E pattern1, E pattern2);
    public abstract Vector<E> getElements();
    public abstract void addElement(E pattern);
    }
    and the following:
    Code:
    public abstract class Pattern implements Cloneable
    {
    public Pattern() {}
    public abstract Pattern clone();
    }
    I use these two classes this way:
    Code:
    public class IntervalLattice extends Lattice<IntervalPattern> {
    Vector<IntervalPattern> elements = new Vector<IntervalPattern> ();
    //Here I give an implementation for each abstract method inherited. 
    public boolean isSubsuming(IntervalPattern pattern1,IntervalPattern pattern2) 
    //.... 
    }
    and
    Code:
    public class IntervalPattern extends Pattern
    {
    private double inf;
    private double sup;
    // Here I give a default constructor, and another to fill the fields 
    //Then some methods of work not relevant to our problem 
    }
    The problem with that is that I often make as cast
    Code:
    Lattice<IntervalPattern> l = ...
    IntervalLattice il = (IntervalLattice) l;
    and this is very bad... not clean, very poorly reusable, and finally not at all generic. In fact, I seek a way of having a single entity and Lattice <E> "ELattice.

    I hope I have stated the problem clearly, feel free to say otherwise

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Genericity and Inheritance

    It's not ugly, it is an upper cast. All Lattice are not IntervalLattice must therefore wait for you that the conversion fails.

  3. #3
    Join Date
    Jan 2009
    Posts
    638

    Re: Genericity and Inheritance

    Thank you. Yes, I quite agree with you, but there is not any lattice, Lattice is a <IntervalPattern>. So, I modeled wrong because it has redundancy and cast, but I can find no way to avoid ...

    I'm not saying that the cast are not good, I'm just saying that I'd like to do without

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Genericity and Inheritance

    If in your code you have only one variant of Lattice <IntervalPattern> is IntervalLattice, then you can declare the variable as type IntervalLattice, you should not do the cast!

    Otherwise it's like Katty says that you have several kinds of Lattice <IntervalPattern> with whom you work, one of which is IntervalLattice and there you have to actually make a cast if you put them all in the same bag. Otherwise you can always store them in different collections and use variables of the corresponding type.

    On the other hand if any class that implements the interface extends Lattice <E Pattern> (yes you have declared as abstract class but you should make an interface) method declares additional public or she says but you do not use them in most operations, so you can work directly in your code with the interface, ie with variables of type Lattice <IntervalPattern> and you have no need to cast except when you have access to these methods which are not in the interface.

    Otherwise, it's crazy the number of people who continue to use Vector.

  5. #5
    Join Date
    Jan 2009
    Posts
    638

    Re: Genericity and Inheritance

    Thank you, here is an example that exhibits better my problem
    Code:
    public class Lattice<E extends Pattern> extends Vector<E> {
    private static final long serialVersionUID = 1L;
    public E meet (E p, E q)
    {
     return (E) p.methode(q);  // here I am compelled to cast, what I try to avoid
    }
    public static void main (String[] args){
     Lattice<Interval> l  = new Lattice<Interval>();
     l.add(new Interval());
    }
    }
    So I missed something in the generics, and I can not find anything ...
    Thank you!

Similar Threads

  1. Replies: 3
    Last Post: 08-01-2011, 06:32 AM
  2. What are genericity in C++?
    By Ivann in forum Software Development
    Replies: 4
    Last Post: 08-10-2010, 05:08 PM
  3. What is an Inheritance in C#?
    By - Empty Shell - in forum Software Development
    Replies: 4
    Last Post: 09-02-2010, 07:03 AM
  4. What are the advantages of Inheritance?
    By ScarFace 01 in forum Software Development
    Replies: 5
    Last Post: 01-02-2010, 12:28 PM
  5. Polymorphism VS Inheritance
    By Taylor D in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 10:12 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,577,781.80338 seconds with 17 queries