Java Generic and Inheritance Question

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1550 Views - Last Post: 15 February 2012 - 11:07 AM Rate Topic: -----

#1 Shene  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-February 12

Java Generic and Inheritance Question

Posted 14 February 2012 - 06:05 PM

Hi,
Could any one help me in solving this issue of using generic in inheritance?
here, what I am trying to do is defining a generic ArrayList in my supper class and
reusing the ArrayList in my sub classes.

import java.util.*;

class Test
{
	int x;
    ArrayList<? extends Test> thing;

    public Test(int x)
    {
	    this.x=x;
	}

	int getX()
	{
		return x;
	}
}

class Testme extends Test{

	Testme(int x)
	{
		super(x);
		thing=new ArrayList <Testme>();
	}

	void addme()
	{
		thing.add(new Testme(3));
	}

	void print()
	{
		System.out.print(thing.getX());
	}
}


I get two compile time errors

cannot find symbol
symbol : method add(Testme)
location: class java.util.ArrayList<capture#684 of ? extends Test>
thing.add(new Testme(3));
^

and

cannot find symbol
symbol : method getX()
location: class java.util.ArrayList<capture#49 of ? extends Test>
System.out.print(thing.getX());

Any Ideas please?
Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Java Generic and Inheritance Question

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 06:18 PM

Use ArrayList<Test>. It accepts all subtypes of Test. And, ArrayList.getX is not defined in the Java Class Library. Perhaps you meant to say thing.get(index).getX().
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 06:32 PM

ArrayList do not have a method getX(). That solves error #2.

Why don't you simply make an ArrayList<Test> ? Testme being a Test object it will fit into it.
If you have to be as restrictive as only accepting in the ArrayList only object that extend Test you will have to cast all of your operations

Actually as a father class is not supposed to know about the classes that extend it, actually not even supposed to know if it has been extended, not a really good OO approach to have a father class holding an ArrayList containing only possible descendants.
Was This Post Helpful? 0
  • +
  • -

#4 Shene  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-February 12

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 06:35 PM

View Postblackcompe, on 14 February 2012 - 06:18 PM, said:

Use ArrayList<Test>. It accepts all subtypes of Test. And, ArrayList.getX is not defined in the Java Class Library. Perhaps you meant to say thing.get(index).getX().


Thank you, In the subclass I want to create an arraylist of "Testme" object by reusing the ArrayList<Test> which will be define in the super class. Do you mean I have to change thing=new ArrayList <Testme>(); as well?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 06:37 PM

No. A Testme is a Test so it can be contained in an ArrayList<Test>
Was This Post Helpful? 1
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 06:38 PM

Quote

Do you mean I have to change thing=new ArrayList <Testme>(); as well?


Yes. A list of Testme is not the same thing as a list of Test. If they were I could add an instance of another type derived from Test. E.g.

This post has been edited by blackcompe: 14 February 2012 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

#7 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 06:48 PM

Using <? extends SomeClass> is intended for get polymorphic behavior when you declare methods but declaring an ArrayList with this wild card you cannot add anything to the list. This is an example how you could use it:
import java.util.*;

public class Test {
			
	public static void main(String[] args){
		
		ArrayList<Ford> list1 = new ArrayList<Ford>();
		ArrayList<Honda> list2 = new ArrayList<Honda>();
		ArrayList<Chevrolet> list3 = new ArrayList<Chevrolet>();
		
		list1.add(new Ford());
		list2.add(new Honda());
		list3.add(new Chevrolet());
		
		Car c = new Car();
		c.iterateCars(list1);
		c.iterateCars(list2);
		c.iterateCars(list3);
	}

}

class Car {

	public  void iterateCars(List<? extends Car> list) {
	
		for (Car car : list) {
			car.ignition();
		}
		

	}

	public void ignition() {
		System.out.println("Start Car " );
		
	}

}

class Ford extends Car {
	public void ignition() {
		System.out.println("Start Ford " );
		
	}
}

class Honda extends Car {
	public void ignition() {
		System.out.println("Start Honda " );
		
	}
}

class Chevrolet extends Car {
	public void ignition() {
		System.out.println("Start Chevrolet " );
		
	}
}


As you can see the method iteratorCars doesn't care what kind of car is in the list. It just traverse the list and invoke the method ignition.

This post has been edited by guido-granobles: 14 February 2012 - 06:49 PM

Was This Post Helpful? 1
  • +
  • -

#8 Shene  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-February 12

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 07:06 PM

View Postblackcompe, on 14 February 2012 - 06:38 PM, said:

Quote

Do you mean I have to change thing=new ArrayList <Testme>(); as well?


Yes. A list of Testme is not the same thing as a list of Test. If they were I could add an instance of another type derived from Test. E.g.


Yes I see, but if I change thing=new ArrayList <Testme>(); to thing=new ArrayList <Test>(); in the subclass,then my "thing " could only be used for accessing the super class's attributes and methods. I mean if I add getY() to Testme for instance I cannot say thing.get(0).getY(), can I? So, How can i define "thing" as Testme's ArrayList type? cheers
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 07:11 PM

As I have already said, it is a very bad design to have a father class to hold an ArrayList of its son
you will need to cast and even double cast

Have Testme to overload the definition of thing = ArrayList<Test> in Test by thing = ArrayList<Testme> in Testme.
Was This Post Helpful? 1
  • +
  • -

#10 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 07:14 PM

Quote

Yes I see, but if I change thing=new ArrayList <Testme>(); to thing=new ArrayList <Test>(); in the subclass,then my "thing " could only be used for accessing the super class's attributes and methods. I mean if I add getY() to Testme for instance I cannot say thing.get(0).getY(), can I? So, How can i define "thing" as Testme's ArrayList type? cheers


There's no way around that. If you want a list of differing types, you must handle them with a super type. At best, you can use instanceof and a cast.

Quote

Have Testme to overload the definition of thing = ArrayList<Test> in Test by thing = ArrayList<Testme> in Testme.


You cannot do that.

Shene: Don't you find it weird that your Test class has a list of itself? Lol.

This post has been edited by blackcompe: 14 February 2012 - 07:17 PM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 07:19 PM

View Postblackcompe, on 14 February 2012 - 09:14 PM, said:

Quote

Have Testme to overload the definition of thing = ArrayList<Test> in Test by thing = ArrayList<Testme> in Testme.


You cannot do that.

Why not ?

import java.util.*;

class Test
{
	int x;
    ArrayList<Test> thing;

    public Test(int x)
    {
	    this.x=x;
	}

	int getX()
	{
		return x;
	}
}

class Testme extends Test{

	ArrayList<Testme> thing;
	Testme(int x)
	{
		super(x);
		thing=new ArrayList <Testme>();
	}

	void addme()
	{
		thing.add(new Testme(3));
	}

	void print()
	{
		System.out.print(thing.get(0).getX());
	}
	
	int getX() {
		return 1234; // whatever
	}
}



Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 07:19 PM

View Postblackcompe, on 14 February 2012 - 09:14 PM, said:

Quote

Have Testme to overload the definition of thing = ArrayList<Test> in Test by thing = ArrayList<Testme> in Testme.


You cannot do that.

Why not ?

import java.util.*;

class Test
{
	int x;
    ArrayList<Test> thing;

    public Test(int x)
    {
	    this.x=x;
	}

	int getX()
	{
		return x;
	}
}

class Testme extends Test{

	ArrayList<Testme> thing;
	Testme(int x)
	{
		super(x);
		thing=new ArrayList <Testme>();
	}

	void addme()
	{
		thing.add(new Testme(3));
	}

	void print()
	{
		System.out.print(thing.get(0).getX());
	}
	
	int getX() {
		return 1234; // whatever
	}
}



Was This Post Helpful? 1
  • +
  • -

#13 Shene  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-February 12

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 07:23 PM

View Postguido-granobles, on 14 February 2012 - 06:48 PM, said:

Using <? extends SomeClass> is intended for get polymorphic behavior when you declare methods but declaring an ArrayList with this wild card you cannot add anything to the list. This is an example how you could use it:
import java.util.*;

public class Test {
			
	public static void main(String[] args){
		
		ArrayList<Ford> list1 = new ArrayList<Ford>();
		ArrayList<Honda> list2 = new ArrayList<Honda>();
		ArrayList<Chevrolet> list3 = new ArrayList<Chevrolet>();
		
		list1.add(new Ford());
		list2.add(new Honda());
		list3.add(new Chevrolet());
		
		Car c = new Car();
		c.iterateCars(list1);
		c.iterateCars(list2);
		c.iterateCars(list3);
	}

}

class Car {

	public  void iterateCars(List<? extends Car> list) {
	
		for (Car car : list) {
			car.ignition();
		}
		

	}

	public void ignition() {
		System.out.println("Start Car " );
		
	}

}

class Ford extends Car {
	public void ignition() {
		System.out.println("Start Ford " );
		
	}
}

class Honda extends Car {
	public void ignition() {
		System.out.println("Start Honda " );
		
	}
}

class Chevrolet extends Car {
	public void ignition() {
		System.out.println("Start Chevrolet " );
		
	}
}


As you can see the method iteratorCars doesn't care what kind of car is in the list. It just traverse the list and invoke the method ignition.


Thank you so much that does help :smile2:
Was This Post Helpful? 0
  • +
  • -

#14 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 07:44 PM

pbl: I thought you meant List<Test> lst = new ArrayList<TestMe>();. The OP wants re-usability. He would like Test to implement Test specific things, so subtypes don't have to. If I was to do what you're doing, why declare the list in Test in the first place? You get no re-usability. He's trying to do this:

class Vehicle {
    String make;
    List<Vehicle> lst;
    public String getMake(int i) {
        return lst.get(i).make;
    }
}

class Truck extends Vehicle  {
    int bedLengh;
    Truck() {
        lst = new ArrayList<Vehicle>();
    }
    public int  getBedLength(int i) {
        return lst.get(i).bedLength;
    }
}



But
return lst.get(i).bedLength;
won't compile. Instead he could do this:

return ((Truck)lst.get(i)).bedLengh;



The design needs to be re-evaluated anyway. I can't think of a good reason a class would need a list of itself.

This post has been edited by blackcompe: 14 February 2012 - 07:45 PM

Was This Post Helpful? 1
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Generic and Inheritance Question

Posted 14 February 2012 - 08:28 PM

As I said twice now, really bad design you will need cast if not casts
You can always, not very elegant:

class Test {
    ArrayList<Test> thing;

    Test get(int n) {
       return thing.get(n);
    }
}

class Testme {
    ArrayList<Testme> thing;

    Testme get(int n) {
      return (Testme) super.get(n);
    }
}


:^:

But really not a good design. as already said: very bad OO practice to have a super class to hold a Collection of its descendants
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2