Generic classes with parameterized super class and interface

Hello.
I'm trying to write a generic class that looks like the following pseudo-code.
public interface Interface {
    public Interface getSibling(...);
public class Klass {...}
public class MyKlass<T extends Klass, T2 extends Interface>
        extends T implements T2 {
    public T2 getSibling(...) {
        return this;
}where Interface and Klass each have various extensions.
I came across this problem, or challenge, while writing classes for testing my EJBs. I tried and failed various attempts.
Is it possible to write generic classes of this nature in Java?
If so, please tell me and others who are like me.
Thanks in advance.

No. That would not work.
Beside being forbidden by the compiler, to my understanding, it cannot be done in theory either, as the parameterized types get bound at instantiation time and are not available for static reference, which both extends and implements require.

Similar Messages

  • [svn:osmf:] 13083: Updated MASTAdapter class with new trait names and events.

    Revision: 13083
    Revision: 13083
    Author:   [email protected]
    Date:     2009-12-17 16:10:05 -0800 (Thu, 17 Dec 2009)
    Log Message:
    Updated MASTAdapter class with new trait names and events.
    Modified Paths:
        osmf/trunk/plugins/MASTPlugin/org/osmf/mast/adapter/MASTAdapter.as
        osmf/trunk/plugins/MASTPluginIntegrationTest/src/org/osmf/test/mast/TestMASTPluginInfo.as
        osmf/trunk/plugins/MASTPluginIntegrationTest/src/org/osmf/test/mast/adapter/TestMASTAdapt er.as

    Silviu!
    I'm really sorry, but as all the office is trying to fix the problem, someone replaced the url content with the stuff that was pointing to the local machine.
    I've fix it, and the video player is available here again: http://pp.player.webtv.flumotion.com/params/apiKey/5PzLR2ULL3z2rumJ/videoId/60
    If there are other references to the localhost, please do not bother with them, as they do not affect player functionality.
    The stream that it uses is an RTMPE, so i'm afraid it will only works with this player. Anyway, the stream url is as follows: rtmpe://87.248.205.81/a4349/e2/veo7/ondemandrtmpe/mp4/med/first-person-mario-4d8741aa.mp4
    I really appreciate your help!
    Thank you!

  • Why isn't there a simpler way to initialize a subclass with its super class

    Let me explain my doubt with an example...
    public class Parent {
    �    public String parent;
    public class Child extends Parent{
    �    public String child;
    I've an instance of Parent p. I want to construct Child c, with the data in p.
    The only way that is provided by Java language seems to be, having a constructor in Child like
    public class Child extends Parent{
    �    public String child;
    �    public Child(Parent p){
    �    �    parent = p.parent;
    �    }
    The problem with this is there is lot of redundant assignment code.
    What I don't understand is why there is not a simpler way of doing this when a subclass is after all super class data + some other data(excuse me for not looking at the bahavior part of it)
    I'm looking for something as simple as Child c = p, which I know is wrong and know the reasons too.
    Yes, we can wrap Child over Parent to do this, but it necessitates repeating all the methods in Parent.
    Why is the language writers didn't provide a simple way of doing this? I should be missing something here...I'm just searching for an explanation. May be I'm asking a dumb question, but this bugs me a lot...
    Regards,
    Kothapalli.

    To answer DrClap, I'm demanding it now :-). Let me
    not suggest something that Mr.Gosling didn't think of;
    he should be having some reasons in not providing it.Because it's essentially impossible in the general case. One of the reasons you may be extending a class is to add new invariants to the superclass.
    eg- extend java.awt.Rectangle by BoundedRectangle - extra invariant that none of its corner points may be less than 0 or more than 100 in any dimension. (The rectangle must be entirely contained in the area (0,0)-(100,100))
    What would happen if you try to create a BoundedRectangle from a rectangle representing (50,50)-(150,150)? Complete invariant breakdown, with no generic way to handle it, or even recognise it.
    Actually, BIJ and sgabie are asking for it. Provide an
    automatic copy constructor. Every object should have
    an implicit copy constructor defined. From any
    subclass, I should be able to invoke
    super(parentClass). From the subclass, we can invoke
    super(parentClass) first and then go on to initialize the
    subclass data.
    I really don't know the implementation issues of this,
    but what I'm looking for is something like this. I'm just
    curious to know the implementation issues.Implementation issue #1: Classes that should not, under any circumstane, be copied.
    * eg 1- Singleton objects. If there's an automatic copy constructor, then multiple singletons can be created, making them essentially useless. This by extension kills the Typesafe Enum pattern also.
    * eg 2- Objects with extra information, such as java.sql.Connection - if you copied this, would the copy be using the same socket connection, or would another connection be required? What happens if opening another connection, and the JDBC driver requires the password to be entered for every new connection? If the wrong password is entered, what happens to the newly creating connection?
    Implementation issue #2: Copying implementation?
    * Already mentioned by RPWithey. The only guaranteed way to perform the copy would be with a shallow copy, which may end up breaking things.
    Why can't you write the copy constructor yourself? If it's a special case, it only has to be done once. If it's a recurring case, you could write a code generation tool to write them for you, along with most of the rest of the class.

  • How to override a method in an inner class of the super class

    I have a rather horribly written class, which I need to adapt. I could simply do this if I could override a method in one of it's inner classes. My plan then was to extend the original class, and override the method in question. But here I am struggling.
    The code below is representative of my situation.
    public class ClassA
       ValueChecks vc = null;
       /** Creates a new instance of Main */
       public ClassA()
          System.out.println("ClassA Constructor");
          vc = new ValueChecks();
          vc.checkMaximum();
         // I want this to call the overridden method, but it does not, it seems to call
         // the method in this class. probably because vc belongs to this class
          this.vc.checkMinimum();
          this.myMethod();
       protected void myMethod()
          System.out.println("myMethod(SUPER)");
       protected class ValueChecks
          protected boolean checkMinimum()
             System.out.println("ValueChecks.checkMinimum (SUPER)");
             return true;
          protected boolean checkMaximum()
             return false;
    }I have extended ClassA, call it ClassASub, and it is this Class which I instantiate. The constructor in ClassASub obviously calls the constructor in ClassA. I want to override the checkMinimum() method in ValueChecks, but the above code always calls the method in ClassA. The ClassASub code looks like this
    public class ClassASub extends ClassA
       public ClassAInner cias;
       /** Creates a new instance of Main */
       public ClassASub()
          System.out.println("ClassASub Constructor");
       protected void myMethod()
          System.out.println("myMethod(SUB)");
       protected class ValueChecks extends ClassA.ValueChecks
          protected boolean checkMinimum()
             System.out.println("ValueChecks.checkMinimum (SUB)");
             return true;
    }The method myMethod seems to be suitably overridden, but I cannot override the checkMinimum() method.
    I think this is a stupid problem to do with how the ValueChecks class is instantiated. I think I need to create an instance of ValueChecks in ClassASub, and pass a reference to it into ClassA. But this will upset the way ClassA works. Could somebody enlighten me please.

    vc = new ValueChecks();vc is a ValueChecks object. No matter whether you subclass ValueChecks or not, vc is always of this type, per this line of code.
    // I want this to call the overridden method, but it does not, it seems to > call
    // the method in this class. probably because vc belongs to this class
    this.vc.checkMinimum();No, it's because again vc references a ValueChecks object, because it was created as such.
    And when I say ValueChecks, I mean the original class, not the one you tried to create by the same name, attempting to override the original.

  • Invoke super class constructor of super class' parent class

    I would like to invoke a constructor of a super class that is the parent of the direct super class. For instance:
    class C extends class B, and class B extends class A.
    From class C, is it possible to invoke class A's constructor without first invoking class B constructor?
    Something like super.A() ??
    Thanks.
    Joe

    try this,
    abstract class GrandParent
          private Object pObj = null;
          protected Via via = new Via();
          protected class Via
             public Object doMethod1( Object obj )
                pObj = obj;
                return "GrandParent m1: " + pObj;
             public Object doMethod2( Object obj )
                pObj = obj;
                return "GrandParent m2: " + pObj;
             public Object get()
                return "gp get: " + pObj;
          public abstract Object doMethod1( Object obj );
          public abstract Object doMethod2( Object obj );
          public abstract Object get();
    class Parent extends GrandParent
          private Object pObj = null;
          public Object doMethod1( Object obj )
          pObj = obj;
          return "Parent m1: " + pObj;
          public Object doMethod2( Object obj )
          pObj = obj;
          return "Parent m2: " + pObj;
          public Object get()
          return "p get: " + pObj;
    public class GPMethod extends Parent
          public static void main( String[] args )
          GrandParent gp = new Parent();
          System.out.println( gp.doMethod1( "calling parent" ) );
          System.out.println( gp.doMethod2( "calling parent m2" ) );
          System.out.println( gp.get() );
          System.out.println( "" );
          System.out.println( gp.via.doMethod1( "calling via to GP" ) );
          System.out.println( gp.via.doMethod2( "calling via to GP m2" ) );
          System.out.println( gp.via.get() );
    }

  • SSRS cube report with parameters...and more

    Hi,
    I have created some basic SSRS reports in the past with parameters, but I was just given the following project by my manager and I would like to find out 2 things:
    1.  Can this be accomplished using SSRS
    2.  If yes, what are the High-Level steps needed to get this accomplished
    First off, the SSRS report(s) will be connecting to an SSAS cube, so there will be some MDX involved.  The cube is fairly simple, 1 measure group and 3 dimensions.
    The main piece of this project will be 4 parameters that will be displayed to the user; Quarter, Division, Operations, and ReportList.
    The Quarter is just the Quarter that the user wants to run the report for (Q1-2012, Q1-2013, etc..).  This will come from the Date Dimension  The Division will be a list of all of our internal divisions that we have setup in our Client Dimension
    table (Eastern Division, Western Division, etc...).  The Operations will be list of operations for each division selected in the Division parameter (Boston Ops, NY Ops, etc...).  This list will also come from the Client Dimension.  The ReportList
    will be a list of all reports that are related to the Division and Operations Selected.  For example, Eastern Division and Boston Ops could have 3 reports, Western Division and San Francisco Ops has 5 reports, etc...
    The final piece to this is that when the user selects the reports to be generated, they will have to be created into 1 report.  This way the user can convert the report to 1 single PDF file or print the report.  So if 3 reports were selected, each
    report would be a separate page and could then be converted to a single PDF file or printed.
    I know that SSRS has the ability to create cascading parameters, so that should take care of dynamically generating the Operations parameter drop down as well as the REportList drop down.  Of course, using MDX to accomplish this will tricky since I
    haven't done much with MDX.
    I guess my last concern is how to generated multiple reports given the parameters selected and have them be combined into 1 report?  I was thinking subreports, but not sure if that would work.
    Any feedback or concerns/ideas on how this can be accomplished using SSRS?
    **Using SSRS 2012
    thanks
    Scott

    Do you have a question? The title doesn't provide sufficient information what you are after.

  • How to get fully qualified name for a class with just the class name

    I want to get the fully qualified name of the class at runtime. The only piece of information which I have is the class name.
    I want to create a runtime instance of a class with the class name [Just the class name].
    Is there is any way to achieve this using ClassLoader?
    Could anyone suggest me an idea?
    Cheers,
    Vinn.

    Nope. If you are given "List", will you decide that means "java.util.List" or "java.awt.List"? Of course you could prompt the user to make the decision for you, I suppose. But using a class loader isn't going to make the task any easier.

  • Where find java classes corresponding to message mapping and interfaces ?

    Hi
    Forum,
    when i create my objects in Repository, like Message interface and messgae mapping, correspoding to them, java class in created,  where can i see these java classes in the XI's file system,

    Hi sudeep,
    During the installation of Xi we select a database.So all the objetcs and related things that we create in IR and ID will be saved in the database only.I dont know how to check the .class file for each object...
    Check these weblogs from sravya where she has given you the table names where the IR and ID objects are stored:
    /people/sravya.talanki2/blog/2007/01/11/ripping-off-sap-xi-stack-133sharing-the-goodies-of-abap-api146s
    /people/sravya.talanki2/blog/2005/12/02/sxicache--ripped-off
    /people/sravya.talanki2/blog/2006/12/28/skelton-of-mapping-runtime-in-sap-xi
    regards
    BILL

  • Generics: Requiring a generic type with multiple super-interfaces

    Is it possible to use generics to require that a type be a composition of two superclasses? For instance, let's say that I have a method that serializes a List. Since the List interface is not itself serializable (but most List implementations are), I cannot have a compile-time guarantee that a method with the following signature will succeed:
    public void serializeList(List serializableList);
    However, could I use generics to construct a new signature (sorry about bad generics syntax) like this:
    public void serializeList(<? extends List, Serializeable> serializableList);
    Thanks.

    It's not exactly generics, you want the object to implement both List and Serializable. Generics would let you declare that the List should contain only Serializable objects (don't ask me to write the declaration though).
    However you can't even guarantee that a Serializable object can be serialized reliably, since it could contain (directly or indirectly) a reference to an object that isn't serializable. So I wouldn't work too hard on solving that problem.

  • Dynamically Loading a Class with Parameters

    Hi,
    I'm working on small program during my spare time and I have run into a problem. I have one abstract class (Foo) which is extended by several other subclasses (subFoo1, subFoo2). I am trying to avoid making a whole host of if statements depending on user input by coding in dynamic class loading.
    The newInstance() method I've read about works well except that it does not accept arguments, something that I sorely need:
    Foo a = Class.forName(input).asSubclass(Foo.class).newInstance();The other method works for creating the objects but I get an error:
    Class foo = Class.forName(input);
    Constructor cons = cs.getConstructor(new Class[]{String.class, Long.TYPE});
    Object a = cons.newInstance(new Object[]{ FooString, FooInt});
    a.method()
    cannot find symbol
    symbol : method(java.math.BigDecimal)
    location : java.lang.ObjectI can understand the problem that the new 'Object' can't find the methods of 'foo', but i don't know how to solve the problem.

    Object a = cons.newInstance(new Object[]{ FooString, FooInt});
    a.method();That isn't very strange, is it? a is an Object, and class Object doesn't have the method you're calling. You have to cast it to a Foo:
    Foo a = (Foo)cons.newInstance(new Object[]{ FooString, FooInt});
    a.method();

  • Class with composite Key,  attribute and relation

    Hello
    I am implementing the jpa part of my project however I have a quite common relation to set up between 2 tables and I think I am in the wrong way
    I have 3 entities : question - answer and project
    The code below should allow you to understand easierly :
    ----- project
    public class Project implements Serializable{
         private static final long serialVersionUID = 6478117913922953539L;
         @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
         private Long ProjId;
         @Column(nullable = false)
         private String Name;
         @OneToMany(mappedBy="project",targetEntity=AnswerPK.class)
         private List<Answer> Answers;
    ------- question
    public class Question implements Serializable{
         private static final long serialVersionUID = -1231353969246302525L;
         @Id @GeneratedValue
         private Long QuestId;
         @Column(nullable = false)
         private String Label;
         @Column(nullable = false)
         private String Type;
    -------- Answer
    public class Answer implements Serializable{
         private static final long serialVersionUID = -690621748466361598L;
         @EmbeddedId
         private AnswerPK key;
         private String result;
    ----------- AnswerPK
    public class AnswerPK implements Serializable { 
         private static final long serialVersionUID = 9172011321649468776L;
         @ManyToOne(optional=false)
         @JoinColumn(name = "ProjId",nullable = false)
         private Project project;
         @OneToOne(optional=false)
         @JoinColumn(name = "QuestId",nullable = false)
         private Question question;
    however I got this error
    Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.unmod.Domain.Project.Answers[com.unmod.Domain.AnswerPK]
    so I start thinking that's not the way to do that

    Numerous other things started going odd so I cleared out and re-installed jdev.
    The entities are generated correctly now.
    blew 4 hours on it though

  • Problem with parameterized reports in InfoView interface with Russian local

    Please, help!
    We have a problem with users, who have u201CRussianu201D as Info View portal interface language. Users have problems with reports which are parameterized. When user tries to select a custom date he gets prompt with incorrect template "Enter Date Report: - jj.nn.aaaa H:mm:ss". As a result the report is not updated. If User uses English interface all is OK.

    Maksim,
    Could you provide the following info:
    1) BOE XIR2 or BOE XI3.1?
    2) Which Patch-level?
    3) Are you using the JAVA infoview or .NET infoview?
    4) Which kinds of reports are you attempting to put the custom date range in (CR, Webi, Deski)
    Thanks!
    -Brian

  • Can we chagne Super class method parameters in Sub class

    Hi,
    I created a super class.  In that class i created a method. That method is having 4 input parameters and 4 export parameters.
    I created a sub class for that super class. I need to use only 2 input parameters in this class rather than 4 parameters. I want to delete 2 Input parameters in the sub class of the super class method.  Is it possible.
    If possible. can we give an simple code or pseudo code?
    regards,
    krishna

    Hi,
    I think you can not.
    Because, only public attributes can be inherited and they will remain public in the subclass.
    for further detail check,
    http://help.sap.com/saphelp_nw70/helpdata/en/1d/df5f57127111d3b9390000e8353423/content.htm
    regards,
    Anirban

  • Default class with public constructor - why?

    Noticed some code that had a class with package-level access and a public constructor - wondered what the benefit/use of that would be... Thanks!

    GarudaJava wrote:
    Noticed some code that had a class with package-level access and a public constructor - wondered what the benefit/use of that would be... Thanks!If the class itself doesn't need to be exposed, but it implements some interface, and is assumed to have a public no-arg consturctor, it could be created reflexively. This is a contrived situation, but it's the only one I can think of in Java where that'd be useful.
    package foo;
    class Foo implements SomeInterface {
      public Foo() {
    package factory;
    class Factory {
      public static SomeInterface create(Class<? extends SomeInterface> clazz) { // not sure if I got the generics right, but they're incidendtal to the example
        return clazz.newInstance();
    package foo;
    import factory.Factory;
    public class Bar {
      public void bar() {
        SomeInterface si = Factory.create(Foo.class);
        si.doStuff();
    }Like I said, pretty contrived, and I can't think of a real-world use case that matches it offhand, but structurally it'd look something like that.
    You could also maybe imagine that the factory package might do more than just create and return an instance. It might create it and use that SomeInterface type for its own ends.

  • Recursive get inheritance family , but It stop at It's super class.

    I make this class for the purpose to getting specified class name's inheritance family.
    import java.util.*;
    import java.lang.reflect.Field;
    // A simple class for getting specify class name's super class family.
    // Usage: java GetSuperClass java.util.ArrayList.
    public class GetSuperClass {
      //recursive get super class.
      public static void get(String str) {
        Class ref = null;
        try {
          // get Class Objcet.
          ref = Class.forName(str);
        } catch(ClassNotFoundException e) {
          e.printStackTrace(System.err);
          System.exit(0);
          System.out.println(ref);
          System.out.println("field:");
          // get fields
          Field[] field = ref.getDeclaredFields();
          for(int i = 0; i < field.length; i++) {
            System.out.println(field);
    //get super class
    Class superClass= ref.getSuperclass();
    //recursive call get()
    if(superClass != null) {
    get(superClass.toString());
    return;
    public static void main(String[] args) {
    // must input a class name for getting super class family.
    if(args.length != 1) {
    System.out.println("incorrect parameters");
    System.exit(0);
    // call static method to start.
    get(args[0]);
    Input as into console command prompt.
    java GetSuperClass java.util.ArrayListHere is result:
    class java.util.ArrayList
    field:
    private static final long java.util.ArrayList.serialVersionUID
    private transient java.lang.Object[] java.util.ArrayList.elementData
    private int java.util.ArrayList.size
    java.lang.ClassNotFoundException: class java.util.AbstractList
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:164)
    at GetSuperClass.get(GetSuperClass.java:9)
    at GetSuperClass.get(GetSuperClass.java:23)
    at GetSuperClass.main(GetSuperClass.java:33)
    Question: what's wrong with it?
    Thanks for your response.

    No need to lokup the super class! You already have it!
    import java.util.*;
    import java.lang.reflect.Field;
    // A simple class for getting specify class name's super class family.
    // Usage: java GetSuperClass java.util.ArrayList.
    public class GetSuperClass
        public static void get(String str)
            Class ref = null;
            for (ClassLoader cl = GetSuperClass.class.getClassLoader(); cl != null; cl = cl.getParent())
                try
                    // get Class Objcet.
                    ref = Class.forName(str, false, System.class.getClassLoader());
                catch(ClassNotFoundException e)
                    //e.printStackTrace(System.err);
            get(ref);
        private static void get(Class ref)
            System.out.println(ref);
            System.out.println("field:");
            // get fields
            Field[] field = ref.getDeclaredFields();
            for(int i = 0; i < field.length; i++)
                System.out.println(field);
    //get super class
    Class superClass= ref.getSuperclass();
    //recursive call get()
    if(superClass != null)
    get(superClass);
    return;
    public static void main(String[] args)
    // call static method to start.
    get("java.util.ArrayList");

Maybe you are looking for

  • Upload pics to one folder

    ok guys and girls: i need to knw how to upload my pics to 1event. example: if i upload 5pics to my mac pc, then upload 10pics 10min later, how come it automatically seperates the pics/event? then when i create a new folder to paste them all in, it wo

  • Finding Angle with respect to X axis in VB?

    In my VB program, I used the FindStraightEdge function to get a line for a particular edge. I would like to know what is the angle with respect to X axis. In the Measurement Studio, I use the "Caliper" to find this angle; however, in VB the Caliper f

  • SNMP traps from BPEL

    Hi I have a requirement to send SNMP traps from BPEL. I am using FMW11.1.1.3 over weblogic. I was going through some documentation where i could see that SNMP traps can be created from the Weblogic Server console. Can some one help me on how to gener

  • Example of calling utl_http command on trigger

    I am generating trigger on oracle 11g db table,on trigger i want to communicate to other linux machine.Can any one provide me running example of utl_http command,so that i can refer it? thanks in advance

  • Is it possible to do dual boot in Mountain Lion using Ubuntu 14.2?

    Since I updated my Snow Leopard to Mountain Lion my Macbook (white) has become extremelly slow and have frequently locked. Is it possible to dual boot using Ubuntu 14.2 (latest version) or even erase OSx and install Ubuntu only in my Macbook? Thanks.