Class parametrized type extends and implements, possible?

Hi all,
Maybe I'm not understanding it well, but every time I try to use generics for something useful I get to a point in which I need to define something like this:
public interface MyInterface<T> {
public T get();
public void set(T var);
public class MyClass<T extends Component & MyInterface<T2> > {
T myObject;
.... somewhere in the code:
T2 value;
value = myObject.get();
To me, this would be very useful, but MyClass does not compile. I hope what I try to do is clear (I want T to be a sub class of Component that also implements a parametrized interface). I don't know what I'm doing wrong nor if this is even possible. Believe me when I say I have done a lot of research (and people thought C++ templates where complicated...).
My main problem is having a parametrized type that contains a parametrized type and trying to have both types' parameters available for use in the class.
Any ideas?

This compiles in beta2:
public class MyClass<T2, T extends Component & MyInterface<T2> > {
   T myObject = null;
  void x() {
      myObject.bar();
      T2 a = myObject.get();
interface MyInterface<T> {
      T get();
      void set(T var);
class Component {
    public void bar() {}
}but removing the public modifier from Component.bar() will give the error: cannot find symbol.

Similar Messages

  • Difference between extends and implements

    hi
    i am new to java. i need to know the difference between extends class implement class.
    can anybody explain in simple words? i am new too oops concept also?
    what are the conditions to use extends class, implement and interface?
    class b extends a implements c
    i know class a is a super class and class b is a sub class.
    if class b extends class a means how should be the class a, what about the methods?
    and class a implements class c means what should be the conditions?
    i searched in Internet but the explanation is not very to me.can body explain me please?
    thank you

    sarcasteak wrote:
    Your class can implement an interface, which means it must use the methods defined in the interface.No, it doesn't need to use them. It needs to implement them. Or be declared abstract.
    Note that the methods in the interface are empty,No, they're not. They're abstract.
    // empty method
    void foo() {}
    // abstract method
    abstract void foo();(The abstract keyword is optional for interface methods, since they're all abstract.
    and you have to define what they do in your class that implements the interface.Just like they have to for abstract methods in a class you extend, if the child class is not declared abstract.
    There really isn't any difference between "extends" and "implements." There is no situation where you can choose. Any case where one is legal, only that one is legal. They could just as easily be a single keyword.
    Presumably the OP's real question is, "When do I use a class for a supertype vs. using an interface for a supertype?" The answer to that, of course, is:
    Use a class when at least some of the methods have valid default implementations. Use an interface when that is not the case. And of course the two are not mutually exclusive. It's quite common to do both.
    At the end of the day, an interface is really nothing more than a class that has no non-final or non-static member variables and whose instance methods are all abstract, and from which you can multiply inherit.
    Edited by: jverd on Feb 4, 2010 1:56 PM

  • Use extends and implements or not?

    What is considered a better way of implementing classes? with the use of extends and/or implements or not?
    ex:
    public class TabPanel extends JPanel implements ActionListeneror have it return the needed Type
    ex:
    public class TabPanel implements ActionListener
    public JPanel TabPanel()
    JPanel jp = new JPanel();
    jp.add(new JLabel("Test"));
    return jp;
    }

    To extend or not to extend, that is a question. Defenitely, you must subclass when you need to override some method. You want to subclass to reuse the customized class. You want to implement an interface when you need a class implementing it.
    Regarding your example:
    public class TabPanel implements ActionListener {
         public JPanel TabPanel() {
              JPanel jp = new JPanel();
              jp.add(new JLabel("Test"));
              return jp;
    }Are you sure it will work? IMO, you do not have to specify result type and must not return anything in the constructors. Why to bother with TabPanel class? If you like to hide the scope, use {} braces in the code.
    JPanel panel = new JPanel();
    jp.add(new JLabel("Test"));
    public class NonAnanymousListener implements ActionListener {

  • TextLayout not Serializable AND final class - can't extend and save objects

    As the title reads, I am using a TextLayout object in a class called Foo.
    I want to be able to serialize Foo and save it to the HD. This can't be done because TextLayout does not implement Serializable.
    So I tried to create a new class that extended the TextLayout class. This wasn't possible because TextLayout is defined as:
    public final class TextLayout
    extends Object
    implements Cloneable
    Basically, I have no clue how to be able to save my Foo class to the HD since I can't serialize the TextLayout object inside the Foo class. I would really appreciate help, because it is quite important that this works.
    Thanks.

    Siniz wrote:
    I have never dabbled with with transient variables, but I just read up on it and understand what you are getting at. Is this really the only way?
    The reason why I need to use TextLayout is because I need the bounding box of the text. I see no other way to get a bounding box around text than using TextLayout.That's a question for the swing/awt forums.
    I am also not entirely sure what you mean when you say I should write my own writeObject and readObject methods? You are saying I should entirely ditch the ObjectOutputStream methods?No, you implement those methods on your class in order to customize serialization. Please see the javadocs on the Serializable interface.

  • Difference between Extend and Implement

    Hello guys, I still dont get it, i'm confuse, when to use Extend and when to use Implement . What's the big diference? Can anyone give me an idea?

    I keep getting an error with this class which states: "Type parameter RectangleOperand is not within it's bounds"
    import java.util.*;
    public class RectangleOperand extends Rectangle2D.Double implements Addable<RectangleOperand> 
        public RectangleOperand (double w, double h)
           super(0.0,0.0,w,h);
    public static RectangleOperand readRectangle(Scanner scan)
        double doubleValue;
        doubleValue = scan.nextDouble();
        RectangleOperand rectangle;
        return rectangle = new RectangleOperand(w,h);
    public boolean equalTo(RectangleOperand opnd2)
         boolean equal = true;
         if(this.width == opnd2.width && this.height == opnd2.height)
             equal = true;  
         else
             equal = false;
         return equal;
    public RectangleOperand plus(RectangleOperand opnd2)
         double newWidth, newHeight, width, height;
         newWidth =  (this.width)  + (opnd2.width);
         newHeight = (this.height) + (opnd2.height);
         RectangleOperand rectanglePlus = new RectangleOperand(newWidth,newHeight);
         return rectanglePlus;
    }

  • Extends and implements

    "extends" => inheritance?
    and "implements" => interface?
    or the opposite?
    and what is the difference between an
    abstract class and interface?
    Thanks in advance
    Jack

    EXTENDS
    Yes, "extends" means inheritance.
    E.g.:
    public class Chihuahua extends Dog {
    }So, from the moment you use "extends" we can say that Chihuahua IS-A Dog.
    IMPLEMENTS
    We use "implements" keyword when a class must implement one or more interfaces.
    E.g.:
    public class Chihuahua extends Dog implements SmallDog, NiceDog{
    }Interfaces are a way to simulate multiple inheritance in Java language.
    Now we also can say that a Chihuahua IS-A Dog, IS-A SmallDog and IS-A NiceDog. Yes, interfaces also causes IS-A relationships.
    ABSTRACT CLASSES x INTERFACES
    Well, some book authors says that interfaces are a more "pure version" of a abstract class. If you don't know yet, all methods of a interface are abstract, that is: the class that implements an interface MUST (with no exceptions) implements (override) all his methods. Is also important to know that all methods in a interface has a header only (no implementation), for instance:
    public interface SmallDog {
    public abstract void walkFast( );
    public abstract void hiddenBelowTheDesk( );
    }But why all methods of an interface must not be implemented? Simply because they are abstract.
    An abstract class can contain OR NOT abstract methods. In mention of the abstract methods the behavior of a class that inherits from a abstract class is the same. Look this example:
    Dog.java
    public abstract class Dog{
       private String raceName;
       public abstract void bark();
       public abstract void run();
       public String getRaceName(){
          return this.raceName;
    }You can see that we have two abstract methods: bark and run. It means that if a class extends from Dog it MUST override the methods bark and run methods. getRaceName is a method, but it isn't an abstract abstract method.
    Now let's see the Chihuahua class again:
    public class Chihuahua extends Dog{
    private BigEye leftEye;
    private BigEye rightEye;
    private BigEar leftEar;
    private BigEar rightEar;
    public void bark(){
    public void run(){
    INTERFACES INHERITS FROM OTHER INTERFACES
    Yes, it can sound crazy, but it is possible in Java. An interface can inherits from other interfaces. It ins't hard to understand. Based on the two interfaces SmallDog and NiceDog we will see how an interface can inherits from another one:
    public interface CompleteCoolDog extends SmallDog, Nicedog{
    }The interface CompleteCoolDog contains all methods from SmallDog and Nicedog, but is not obbligatory to implement them because an interface can not implement methods, only declare them. And who must implement them? The first concrete class that implements CompleteCoolDog interface.
    You can see very nice informations about interfaces and abstract classes in the books of Kathy Sierra and Bert Bates (Study guides for SCJP).
    I hope I helped you a little. Some doubt please, mail me.
    See you =)

  • Extending and implementing

    Is there a way to tell the compiler: this variable is of this class and implements that interface.
    Say for instance a Component that implements ActionListener
    extending component with an abstract class that implements ActionListener and then extend this class wouldn't work since then i can'T as well extend button textArea ect.

    if(variable instanceof Component) {
    //do something
    if(variable instanceof ActionListener) {
    //do something else
    if( (variable instanceof Component) &&
    (variable instanceof ActionListener) ) {
    // really do something else
    }

  • BADI - Diff. Class generated in Definition and Implementation

    Hi all,
    When we define a BADI,  a BADI class is generated.  When we implement the BADI, another class is generated. 
    1) What is the difference between these 2 classes?
    2) As I know, we use the class generated in Implementation to instantiate and call the BADI method in our program.  What about the class generated in Definition?  What is it used for?
    Thanks.

    I noticed that under Definition, the class generated is with cat= Exit class while under Implementation, the class generated is with cat= general object type.  Both can be instantiated for use in ABAP program.
    What is the difference/usage of these 2 generated classes?

  • How to resolve Comparison between a value with static type string and a possible unrelated type numb

    I am new to this and any help would be greatly appreciated, Im trying to display custom numbers for values of a Hslider.
    Here is my code so far:
    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                                     xmlns:s="library://ns.adobe.com/flex/spark"
                                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
      <fx:Declarations>
      <!-- Place non-visual elements (e.g., services, value objects) here -->
      </fx:Declarations>
      <fx:Script>
                        <![CDATA[
                                  import mx.events.SliderEvent;
                                  protected function ValueSlider_changeHandler(event:SliderEvent):void
                                  ValueLabel.text = String(ValueSlider.value);
                                            if(ValueSlider.value == "0")
                                            ValueLabel.text = "150";
                                            if(ValueSlider.value == "1")
                                            ValueLabel.text = "333";
                                            if(ValueSlider.value == "2")
                                            ValueLabel.text = "543";
                                            if(ValueSlider.value == "3")
                                            ValueLabel.text = "9342";
                        ]]>
      </fx:Script>
              <s:Panel x="199" y="141" width="250" height="200">
                        <s:HSlider id="ValueSlider" x="74" y="68" maximum="5" minimum="0" stepSize="1"/>
                        <s:Label id="ValueLabel" x="109" y="38" text="Label"/>
      </s:Panel>
    </s:Application>

    Thank you for your help, I was using quotes around the number from a suggestion on another forum. Now how to I display the result on valueLabel? "valueLabel.text"? {valueLable.text}, "const"?,
    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                                     xmlns:s="library://ns.adobe.com/flex/spark"
                                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
      <fx:Declarations>
      <!-- Place non-visual elements (e.g., services, value objects) here -->
      </fx:Declarations>
      <fx:Script>
                        <![CDATA[
                                  import mx.events.SliderEvent;
                                  protected function ValueSlider_changeHandler(event:SliderEvent):void
                                            const CONVERSION:Array = ["150","333","543","9342"];
                                            valueLabel.text = CONVERSION[valueSlider.value];
                        ]]>
      </fx:Script>
              <s:Panel x="199" y="141" width="250" height="200">
                        <s:HSlider id="valueSlider" x="78" y="69" maximum="3" minimum="0" stepSize="1" value="1"/>
      <s:Label id="valueLabel" x="109" y="38" text= "(valueLabel.text)"/>  <-----what do I place here?
      </s:Panel>
    </s:Application>

  • Extending BADI Implementation class

    Hello,
    I extended a class C1 implementing my BADI interface I1 obtaining class C2.
    I redefined a interface I1 method method in C2.
    I created a new BADI implementation for my class C2.
    However, for this new BADI implementation the BADI builder does not show the interface methods from I1 for my implementation class c2?
    Is it not possible to extend a BADI implementation class and use it as a new implementation?
    If so, why?
    Thanks Matthias

    Hello,
    thanks for your input.
    I might not have made my question clear enaugh:
    I have created a BADI definition D1. There I de-activated option "multiple use' in the usability options.
    1) I have a BADI Interface I1 for D1 and a standard Implementation of this interface for which I use class C1.
    2) I need a second BADI implementaion in which I only want to redefine one method M1 of class C1.
    3) I created a class C2 which has class C1 as superclass and redefined method M1.
    4) I created a second BADI implementation for which I specified class C2 as implementation class.
    As C2 extends C1 which implements the BADI interface I1 everything went fine in the BADI builder for this second implementation.
    The BADI builder seems to be buggy there, as it would not display the business methods of the interface I1, and this really confused me.
    However, I have now tested my second implementation and it works!
    Thanks
    Matthias

  • Concrete classes implement abstract class and implements the interface

    I have one query..
    In java collection framework, concrete classes extend the abstract classes and implement the interface. What is the reason behind extending the class and implementing the interface when the abstract class actually claims to implement that interface?
    For example :
    Class Vector extends AbstractList and implements List ,....
    But the abstract class AbstractList implements List.. So the class Vector need not explicitly implement interface List.
    So, what is the reason behind this explicit definition...?
    If anybody knows please let me know..
    Thanx
    Rajendra.

    Why do you post this question again? You already asked this once in another thread and it has been extensively debated in that thread: http://forum.java.sun.com/thread.jsp?forum=31&thread=347682

  • Yet another case of new type[array] and parametrized type

    I suppose this is an often asked question, to which I seem not to be able to find a simple answer. I know of the "incompatibility" of arrays and generics, and yet in such a seemingly simple case I run into a mystery, or a compiler bug. Look at this warning:
    Set<Integer>  C[];
    foo/bar/some.java:1148: warning: [unchecked] unchecked conversion
    found   : java.util.HashSet[]
    required: java.util.Set<java.lang.Integer>[]
                    C=new HashSet[H-L+1];So far so good, but precisely this *"Set<type>[]"* does not exists, does it? When I try to add a parametrized type to this new statement, compiler says:
    Set<Integer>  C[];
    foo/bar/some.java:1148: generic array creation
                    C=new HashSet<Integer>[H-L+1];
                      ^
    1 errorIn a regular case we can easy deal with the "unchecked" warnings, e.g. Effective Java 2nd Ed, Item 24 discusses this, but all this seem to be ignoring this case. When generic array creation is not permitted, than why this warning stating that *"java.type<typeparam>[] required"* appear, and how to resolve this for safety and correctness? Of course, I can still ignore the warning and do a few instanceof checks and casts, like before, but I am really curious how to transfer this into the new paradigm.
    As much generics improve and help in many cases, I regret their arcane complexity. To paraphrase C.A.R. Hoare, the Java language itself becomes now the problem, not its solution.
    Thomas

    I wonder if we have someplace recent measure just an array stack against a current implementation of a set in Java? In JDK1.4 I remember array was several times faster, 350% faster if my memory serves me, and I do have the classic "inner loop" operation, very repetitive. I can live with the warning, what's the problem?
    Of course all this are considerations fully aside of the issue at hand, that the JDK1.6 compiler prints a nonsense warning message that a parametrized array is expected, what is in fact illegal in current implementation of generics. Which is in my eyes severely faulty, but that would be yet another assault on the Braha Ivory Tower, wouldn't it?
    Thomas

  • Implementation of IDOC types FINSTA01 and PEXR2002

    Hi All,
    I want to implement the 2 IDOC types PEXR2002 and FINSTA01 using EDI.The requirement is like I have to generate both the inbound and outbound processing for both the IDOC types using EDI.
    I have the following queries :
    1)What are the basic fields that are required for both the inbound and outbound processing(like file format)
    2)How the inbound and outbound processing will be triggered like how can I send the file from SAP to EDI or how can I get the file from EDI to SAP and how to uplaod the record into SAP is there any SAP standard transactions or any standard reports or do I have to configure in the SPRO etc.
    3)What are the settings that I have to configure?
    Please help me out and your help will be appreciated.
    Thanks and Regards,
    Ramesh.

    Hello Ramesh,
    The answers to your questions are a few chapters in some books, meaning its quite large.
    But in short.
    1. the minimum fields are what is required by that particular transaction in SAP for inbound and for outbound you gotta check with the receiving party what they need.
    example , if you are posting a PO in SAP, then what are the mandatory fields, thats the minimum.
    With every idoc , incoming, there are a few ways to post, most common is via function modules.
    Lets say you know a partner which will communicate via EDI, you have to maintain a partner profile in WE20.
    There you define the incoming and outgoing messages.
    Every message has, idoc type.
    Transactions WE42 and WE43 are where you define a process code for attaching the message type/idoc type to a Funtion module.
    Also there is config for setting up output types, see transaction NACE.
    This is a process with lots of parameters.
    You would be better off getting a book which explains it all in a proper order.

  • Extending and sub classing

    i'm experimenting with extending and subclassing components in flex. i am trying to add an event listener to the datagridcolumn. i want to add a function to occur when i click the column (using a mouseclickevent).
    each of my pieces of data in the datagrid have a custom style to look like a hyperlink (blue and underlined). the user will drill into details behind the numbers they see. i got this to work with itemClick and i found some good code to use for totaling the datagrid numbers by using the code from Alex's Flex closet http://blogs.adobe.com/aharui/2008/03/flex_3_datagrid_footers.html
    my problem is i'm finding that i cannot use the styling or itemClick function to work with the  footers and so am trying to subclass another footerdatagridcolumn to  have a mouse click event to do something with. i'd like for the footer data to be blue and underlined as well if thats possible but not sure how to refer to my Flex css style in AS3. see the attached screenshot i'm trying to get the footer to work with itemClick and have the same style of blue and underlined for the columns that are highlighted below.

    DataGridColumns do not dispatch mouse events.  I would listen to the
    DataGrid and use mouseEventToItemRenderer as needed.

  • Is it possible to put the java classes of an application and run?

    Hi All,
    Please excuse me if this question seems meaningless / laughable.
    We get java class files by compiling the source code with java compiler. To run the application, classpath can be set to the folder where the class files are stored and then run the main class.
    Now the question is, is it possible to read the class files to RAM and then run the application. I highly appreciate your help.
    Thanks in advance.
    Chinnaswamy

    You could
    1.) copy the class files to a ramdisk
    2.) write another class loader that deletes the class files after loading >them from ram
    but all of this is probably not necessary.
    What exactly would you like to achieve by doing this? Thanks, JoachimSauer.
    The idea I have in mind is to first read and store the java class files in C/C++ code and then while running the C/C++ code, read & put the java classes into RAM and run the java application.
    This way the decompilation problem can be alleviated for valuable java applications. ( I am not sure, this will work)
    Another reason could be speed improvement. Classes in RAM is expected to make the application to run faster than when class files are in hadr disk
    With regards
    Chinnaswamy

Maybe you are looking for