Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 30-10-2009
Member
 
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
Reply With Quote
  #2  
Old 30-10-2009
Member
 
Join Date: May 2008
Posts: 2,008
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.
Reply With Quote
  #3  
Old 30-10-2009
Member
 
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
Reply With Quote
  #4  
Old 30-10-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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.
Reply With Quote
  #5  
Old 30-10-2009
Member
 
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!
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Genericity and Inheritance"
Thread Thread Starter Forum Replies Last Post
What is the difference between Single inheritance and multilevel inheritance? Venugopala Software Development 3 08-01-2011 06:32 AM
What are genericity in C++? Ivann Software Development 4 08-10-2010 05:08 PM
What is an Inheritance in C#? - Empty Shell - Software Development 4 09-02-2010 07:03 AM
What are the advantages of Inheritance? ScarFace 01 Software Development 5 01-02-2010 12:28 PM
Polymorphism VS Inheritance Taylor D Software Development 5 22-01-2010 10:12 AM


All times are GMT +5.5. The time now is 02:06 AM.