Skip to main content



      Home
Home » Modeling » EMF » problem with inheritance and access to subclass( problem with inheritance and access to subclass)
problem with inheritance and access to subclass [message #1060382] Fri, 24 May 2013 07:20 Go to next message
Eclipse UserFriend
hi, I have a problem with inheritance and access to subclass
My EMF Model has a superclass called Component and two subclasses of Component called Primitive and Composite.
to print the Component name I have to do:

// Create a resource for this file.
Resource resource = resourceSet.createResource(uri);
resource.load(null);

Systeme s = (Systeme)resource.getContents().get(0);

EList< Component > list1 = s.get Components ();
Iterator i1 = list1.iterator();
while (i1.hasNext()){
Component c =( Component) i1.next();
java.lang.System.out.println("Component name : "+c.getNom);
}


but now I want to have access to the subclass and then print their name, so I want to do somthing like this

Component:
type: Primitive : component name

Component:
type: Composite : component name


my first attempt was

EList<Primitive> list2 = c.getPrimitives();//  but c have no access to here subclass!!
Iterator i2 = list2.iterator();
while (21.hasNext()){
Primitive p =(Primitive) i2.next();

java.lang.System.out.println("process "+ p.getNom()+" ("+ " ) {");
}

but c have no access to here subclass Crying or Very Sad !!
pleas help me to print the name of the subclass Sad
I hope that the problem is well explained
Re: problem with inheritance and access to subclass [message #1060427 is a reply to message #1060382] Fri, 24 May 2013 11:18 Go to previous messageGo to next message
Eclipse UserFriend
Amina,

Comments below.

On 24/05/2013 3:34 PM, amina amina wrote:
> hi, I have a problem with inheritance and access to subclass
> My EMF Model has a superclass called Component and two subclasses of
> Component called Primitive and Composite.
> to print the Component name I have to do:
>
> // Create a resource for this file.
> Resource resource = resourceSet.createResource(uri);
> resource.load(null);
It's better to use resourceSet.getResource(uri, true) for this.
>
> Systeme s = (Systeme)resource.getContents().get(0);
>
> EList< Component > list1 = s.get Components ();
> Iterator i1 = list1.iterator();
> while (i1.hasNext()){
Using "for (Component c: s.getComponents())" seems more elegant...
> Component c =( Component) i1.next();
> java.lang.System.out.println("Component name : "+c.getNom);
> }
>
> but now I want to have access to the subclass and then print their
> name, so I want to do somthing like this
>
> Component:
> type: Primitive : component name
> Component:
> type: Composite : component name
Xtend supports switches on types, but in Java you can only use normal
"instanceof" testing. EMF also supports a generated XyxSwitch that you
can specialize to handle casePrimitive and caseComposite; that can
perform much better when there are many cases to cover, but for this
simple example, an instanceof test seems sufficient.
>
> my first attempt was
>
> EList<Primitive> list2 = c.getPrimitives();// but c have no access to
> here subclass!!
> Iterator i2 = list2.iterator();
> while (21.hasNext()){
> Primitive p =(Primitive) i2.next();
>
> java.lang.System.out.println("process "+ p.getNom()+" ("+ " ) {");
> }
> but c have no access to here subclass :cry: !!
What do you mean by "no access mean"? The way you've modeled it you'd
have to iterate over all the components and check each one to see if
it's Primitive or Composite.
> pleas help me to print the name of the subclass :( I hope that the
> problem is well explained
Re: problem with inheritance and access to subclass [message #1060563 is a reply to message #1060427] Sun, 26 May 2013 18:05 Go to previous messageGo to next message
Eclipse UserFriend
I have the same problem Crying or Very Sad
Re: problem with inheritance and access to subclass [message #1060643 is a reply to message #1060563] Mon, 27 May 2013 08:54 Go to previous message
Eclipse UserFriend
So the same answer applies...

On 27/05/2013 2:33 PM, Fatima Salhi wrote:
> I have the same problem :cry:
Previous Topic:[CDO] Understanding proxy state.
Next Topic:[Teneo] org.hibernate.proxy.HibernateProxy not found when running as RCP
Goto Forum:
  


Current Time: Fri Jul 18 10:58:08 EDT 2025

Powered by FUDForum. Page generated in 0.05944 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top