Class implementing interface using generics

I have an interface that looks like this:
public interface BinaryTree<T extends Comparable<? super T>> {
     public enum TraverseOrder { PREORDER, INORDER, POSTORDER }
     public boolean isEmpty();
     public boolean search(T key);
     public void remove ( T key ) throws TreeException;
     public void insert(T key) throws TreeException;
     public void print ( TraverseOrder order);
     public int getSize();
}My shell class that implements it looks like this:
public interface BinaryTree<T extends Comparable<? super T>> {
     public enum TraverseOrder { PREORDER, INORDER, POSTORDER }
     public boolean isEmpty(){
                  return true;
     public boolean search(T key) {
                  return true;
     public void remove ( T key ) throws TreeException {
     public void insert(T key) throws TreeException {
     public void print ( TraverseOrder order) {
     public int getSize() {
                    return 0;
}Im getting 2 errors one being
BST.java:1: > expected
public class BST implements BinaryTree<T extends Comparable<? super T>> {
and the other is an enum error. Im pretty sure the second enum error will be resolved by fixing the first error. Any help is apreciated. Thanks
BLADE

Yeah tottaly right but the code i posted is wrong sorry lets try one more time
public interface BinaryTree<T extends Comparable><? super T>> {
     public enum TraverseOrder { PREORDER, INORDER, POSTORDER }
     public boolean isEmpty();
     public boolean search(T key);
     public void remove ( T key ) throws TreeException;
     public void insert(T key) throws TreeException;
     public void print ( TraverseOrder order);
     public int getSize();
public class BST implements BinaryTree<T extends Comparable><? super T>> {
     public enum TraverseOrder { PREORDER, INORDER, POSTORDER }
     public boolean isEmpty(){
                  return true;
     public boolean search(T key) {
                  return true;
     public void remove ( T key ) throws TreeException {
     public void insert(T key) throws TreeException {
     public void print ( TraverseOrder order) {
     public int getSize() {
                    return 0;
}

Similar Messages

  • Public class implements interface

    I am taking my first crack at interfaces: I have superclass A which is a parent of class B, which implements interface C. I need to use class B to make 3 variable instances within class A. I think I will use interface C to make calculations based on the 3 variables. Where do you recommend that I declare and set values for the 3 variables if I need to output the values at the top, in superclass A?
    I'm just a little unclear on pulling these objcts together... thanks in advance.

    I am taking my first crack at interfaces: I have
    superclass A which is a parent of class B, which
    implements interface C. I need to use class B to make
    3 variable instances within class A. I think I will
    use interface C to make calculations based on the 3
    variables. Where do you recommend that I declare and
    set values for the 3 variables if I need to output the
    values at the top, in superclass A?
    I'm just a little unclear on pulling these objcts
    together... thanks in advance. If your variables are going to be used by your superclass A then they had better be declared there. You can work with them whereever you want.
    I'm not sure what you are saying about "...use interface C to make calculations based on the 3 variables." You can't do calculations inside an interface. Furthermore, if B extends A and implements C then A and C are going to be completely separate entities. Any reference to C, even if it is actually an object of type B, will not be able to use anything in A--unless you cast it to B, in which case there is no use in making an interface at all.
    I probably just confused you, but oh well...
    Jeff

  • Internal class implementing interface extending abstract interface :P

    Confused ha? Yeah me too. RIght here it goes.
    Theres an abstract interface (abstractIFace) that has for example method1() method2() and method3(). There are two other interfaces iFace1 and iFace2 that extend abstractIFace. Still with me? :P iFace1 only uses method2() whereas iFace2 uses method1(), method2() and method3(). Internal classes implementing these are then used. The reason is so that when returning an object one method can be used that will return different types of objects. But this doesnt work. It says that all the classes in the abstractIFace must be used/implemented but I only want method2() in iFace1 and all in iFace2.
    Just say what the f*ck if this is too confusing cos i think it is and i did a crap job explaining!! :P

    public interface IFace {
        void method1();
        void method2();
        void method3();
    public class Test {
        private static class Class1 implements IFace {
            public void method1() {
                System.out.println("method1");
            public void method2() {
                System.out.println("method2");
            public void method3() {
                System.out.println("method3");
        private static class Class2 implements IFace {
            public void method1() {
                throw new UnsupportedOperationException();
            public void method2() {
                System.out.println("method2");
            public void method3() {
                throw new UnsupportedOperationException();
        public static IFace createObject(boolean flag) {
            return flag ? (IFace) new Class1() : new Class2();
    }

  • Sample Javadoc comments for classes or methods using generics?

    Can anyone show me a sample Javadoc comment for a class or a method that uses generics. All my attempts so far ended up in warnings from the Javadoc tool..
    I also looked in the Sun forums, in the Javadoc documentation, but nowhere could I find anything that would help me write correct javadoc comments.
    Any luck for you guys?
    Thanks a lot,
    -Laurent

    Thanks man, sorry it must seem obvious to you, but
    for some reason I couldn't get it to work.No. The first time I also had to ask.
    Now an even more difficult (for me) question: how
    would you refer to that method in a @see or {@link
    ..} javadoc comment?Manually erase the method:
    * The see tag refers to this method
    * @param <T> a type variable
    * @see #foo(Object)
    <T> void foo(T t) {}

  • Extend abstract class & implement interface, different return type methods

    abstract class X
    public abstract String method();
    interface Y
    public void method();
    class Z extends X implements Y
      // Compiler error, If I don't implement both methods
      // If I implement only one method, compiler error is thrown for not
      // implementing another method
      // If I implement both the methods,duplicate method error is thrown by 
      //compiler
    The same problem can occur if both methods throw different checked exceptions,or if access modifiers are different..etc
    I'm preparing for SCJP, So just had this weired thought. Please let me know
    if there is a way to solve this.

    Nothing you can do about it except for changing the design.
    Kaj

  • Implementing interface using Oracle Fusion

    Hey All, my company is responding to a tender but one of the things it specifies is that if any middleware is required it should be implemented using Oracle Fusion.
    Up until this point I'd never even heard of Oracle Fusion but am trying to get up to speed fast, I need to know just enough to know we can do it and how hard it would be.
    To my question then, I guess they want us to use Fusion to build the interface between their existing app and ours, but Fusion seems to be a big stack of products... Can anybody point me to the specific product we would be using to build something like that, and some good resources to get an understanding of it. Also what is the learning curve like with this product suite?
    Any help appreciated.
    Martyn

    Hi Martyn,
    Fusion Middleware is a big stack but for integrations you can use SOA suite (or AIA). This in turn is a collection of multiple components. See below for some guidance.
    http://docs.oracle.com/cd/E21764_01/integration.1111/e10223/index.htm
    http://www.oracle.com/us/technologies/soa/soa-suite/oracle-soa-suite-11g-198758.pdf
    Regards,
    Neeraj Sehgal

  • Inner static class implementing interface

    Hello,
    I have a sample program to test on reflections. here the code goes
    import java.lang.reflect.*;
    import java.awt.*;
    import java.lang.*;
    import java.io.*;
    public class SampleName {
       public static void main(String[] args) {
          NewClass b = new NewClass();
           System.out.println("Calling printModifiers");
           printModifiers(b);
             System.out.println("Calling printInterfaces");
           printInterfaces(b);
       static void printModifiers(Object obj){
              Class cl = obj.getClass();
              int mod = cl.getModifiers();
              if(Modifier.isPublic(mod ))
                   System.out.println("Public");
              if(Modifier.isPrivate(mod ))
                   System.out.println("Private");
              if(Modifier.isFinal(mod ))
                   System.out.println("Final");
              if(Modifier.isStatic(mod ))
                   System.out.println("Static");
              if(Modifier.isAbstract(mod ))
                   System.out.println("Abstract");
       static void printInterfaces(Object o) {
              Class c = o.getClass();
              String str = c.getName();
              System.out.println("name  " +str);
              Class[]  theInterfaces= c.getInterfaces();
              if(theInterfaces.length ==0)
                   System.out.println("no interfaces here ");
              else
                   for(int counter = 0; counter<theInterfaces.length;counter++)
                             String interfaceName = theInterfaces[counter].getName();
                             System.out.println("Interface Name :- " +interfaceName);
    static class NewClass implements newInterface {
    }i have an interface in t same folder as
    public interface newInterface
    }i get an error as below while compiling this java code
    SampleName.java:54: cannot resolve symbol
    symbol  : class newInterface
    location: class SampleName.NewClass
    static class NewClass implements newInterface {
                                      ^
    1 errorplease explain me what is the fault i am making and how can it be resolved.
    tnx

    java -cp .;<any other directories or jars> YourClassNameYou get a NoClassDefFoundError message because the JVM (Java Virtual Machine) can't find your class. The way to remedy this is to ensure that your class is included in the classpath. The example assumes that you are in the same directory as the class you're trying to run.
    The Gateway to Classpath Nirvana
    Setting the class path (Windows)
    How Classes are Found

  • Class implementation for interface

    Hello,
    I am a beginner in ABAP Objects.
    In the coding of method if_swf_ifs_workitem_exit~event_raised of class CL_SWF_UTL_EXIT_NEW
    There is the instruction follow :
    *Get the workflow container
    irh_container ?= im_workitem_context-> get_wf_container()
    Im_workitem_context is interface type  "IF_WAPI_WORKITEM_CONTEXT"
    If I execute in debug mode I see the implemtation class of this interface (im_workitem_context)  is "CL_SWF_RUN_WORKITEM_CONTEXT"
    But where this information of implementation is defined ? (I saw nothing)
    Regards
    Christine
    Edited by: chris_75 on Sep 7, 2010 4:22 PM

    Interfaces allow to implement highly scalable object oriented applications.
    Interface is a kind of a template for a real class which forces this class to implement methods defined in an interface.
    The main characteristics of an interfaces are:
    - they DO NOT contain any implementations (so there is nothing like INTERFACE ... IMPLEMENTATION - they have only DEFINITIONS - implementations are within classes)
    - they have only PUBLIC sections.
    Why we need an interface. The answer is simple:
    We want to handle some objects uniformly from one application compotent, whereas these objects may behave differently inside.
    Example:
    Let's say we need to build a sorting program for numbers.
    The program would have an interface variable L_RIF_SORTER of an interface LIF_SORTER. LIF_SORTER has a method definition SORT with an C_TAB changing parameter.
    Sorting application would call the sorting algorithm as follows:
    L_RIF_SORTER->SORT( CHANGING c_tab = l_tab ).
    Now is the main point:
    We want to have 2 kinds of sorting algorithms implemented, let's say BUBBLE SORT and QUICK SORT.
    To do so, we implement 2 classes: LCL_BUBBLE_SORT and LCL_QUICK_SORT.
    Both classes implement interface using the statment INTERFACES in a public section.
    The user would have to choose the algorithm from an input field. Depending on the content of this field the sorting application would instantiate one class or the other using the statement:
    CREATE OBJECT l_rif_sorter TYPE (variable_with_class_name).
    THis is the point where ABAP gets to know the real object and its type behind the interface.
    This approach is generally called the STRATEGY PATTERN. See Wikipedia for this.
    I hope I answered your question.
    Regards,

  • Can a class implements more than one interface?

    Hello
    Can a class implements more than one interface?
    Thanks

    Of course, this doesn't mean that it won't be a problem though. If the two interfaces have methods with the same signature, but different return types, you won't be able to implement them together. I.E.
    interface InterfaceA {
      public int doSomething(String myString);
    interface InterfaceB {
      public String doSomething(String myString);
    // Now the classes
    // Gives error "Duplicate method doSomething(String) in type ClassA"
    public class ClassA implements InterfaceA, InterfaceB {
      public int doSomething(String myString) {
        System.out.println("A");
        return 0;
      public String doSomething(String myString) {
        System.out.println("B");
        return 0;
    // Gives error "The return type is incompatible with InterfaceB.doSomething(String)"
    public class ClassB implements InterfaceA, InterfaceB {
      public int doSomething(String myString) {
        System.out.println("A");
        return 0;
    // Gives error "The return type is incompatible with InterfaceA.doSomething(String)"
    public class ClassC implements InterfaceA, InterfaceB {
      public String doSomething(String myString) {
        System.out.println("B");
        return 0;
    }

  • Right way to use generics for cellular automatons?

    Hi,
    i've got a question about using generic types in combination with subclassing (which should be used in a cellular automaton),
    In fact there are only 3 classes that are necessary to understand my problem:
    - Cell (abstract)
    - CellularAutomaton
    - SubclassedCell (extends Cell)
    These classeses work as follows:
    A cellular automaton holds some cell's with respect to the order they were added. The cell itself holds some properties and the most important function in this small "framework". This function is "performCellAction(CellularAutomaton ca)". It retrieves the "neighbor" cells from the given parameter and calculates a new state using these neighbors.
    The way i implemented this using generics is:
    public class CellularAutomaton<T extends Cell>{
      private List<T> cells;
      public T getLeftNeighbor(T cell) { ... } // gets the left neighbor of the given cell
      public T getRightNeighbor(T cell) { ... } // gets the left neighbor of the given cell
    public abstract class Cell [
      public void performCellAction(CellularAutomaton<? extends Cell> ca) {}
    public class SubclassedCell extends Cell {
      private String str;
      public void performCellAction(CellularAutomaton<? extends Cell> ca) {
        Cell left = ca.getLeftNeighbor();
        // PROBLEM:
        // How to call left.getStr() here???
        // How can i be sure that ca contains objects of class subclassedCell (or objects of subclasses of subclassedCell) ... and not objects of class cell or another subclasses cell
      public String getStr() {}
    }In fact i found "something like a solution", which looks like this:
    public class CellularAutomaton<T extends Cell>{
      private List<T> cells;
      public T getLeftNeighbor(T cell) { ... } // gets the left neighbor of the given cell
      public T getRightNeighbor(T cell) { ... } // gets the left neighbor of the given cell
      public void computer() {
        for (T cell: cells) {
          // compiler warning here!!
          cell.performCellAction(this);
    public abstract class Cell<T extends Cell<?>> [
      public void performCellAction(CellularAutomaton<T> ca) {}
    public class SubclassedCell extends Cell<SubclassedCell> {
      private String str;
      performCellAction(CellularAutomaton<SubclassedCell> ca) {
        SubclassedCell left = ca.getLeftNeighbor();
        left.getStr(); // works here
      public String getStr() {}
    }However, even in this "working" solution i get a compiler warning at the method CellularAutomaton->computer():
    The warning is Type safety: The method performCellAction(CellularAutomaton) belongs to the raw type Cell. References to generic type Cell<T> should be parametrized and Cell<T extends Cell<?>> also looks very uncommon to me.
    Is there a betted way to achieve the goal to access left.getStr() in the performCellAction of the subclassed cell??
    Regards,
    Niko

    I thought of that myself, but if i do this, i get the following error:
    The method performCellAction(CellularAutomaton<capture#1-of ?>) in the type Cell<capture#1-of ?> is not applicable for the arguments (CellularAutomaton<T>)

  • Implementing interface views

    I have to add a main view to the portfolio items and integrate  the view WI_DMS of the web dynpro component DMS_DPR to the same. When I try to do it, it says  'WI_DMS does not implement a valid interface". Is there any way to implement the valid interface dynamically? PLease help.

    >i have been using ( implementing ) interfaces in the components, for example i used iwd_value_help. however i still have no clarity how WD framework is able to communicate with the component implementing interface iwd_value_help.
    Web Dynpro can create a component usage based upon the interface type alone.  This is very similar to normal ABAP OO when you create/get an instance of a class but only know the interface.  Basically this is polymorphism at the component level.
    >the basic question here is, what is wd framework, is it class implementing interfaces generated when we create views and other stuff. and this class( WD fw ) is going to call these implemented methods of the interface??
    Simple answer: yes, exactly as you describe.  Everything relates back to generated and local classes.  The framework can interact with them becuase all the generated classes implement SAP provide framework interfaces.

  • Defining return values of the same class in interface implementations

    hello,
    i'm trying to update older code to java 1.5 using generics. i'm new to generics and i have a problem i cannot solve: assuming the interface
    public interface Stake
         public Stake makeACopyAndShift( long delta );
    }and the class
    public class BasicStake
          private final long pos;
          public BasicStake( long pos )
                 this.pos = pos;
          public Stake makeACopyAndShift( long delta )
                  return new BasicStake( pos + delta );
    }... and a container class Trail:
    public class Trail<S> {
           private final List<S> stakes = new ArrayList<S>();
           public Trail() {}
           public void add( S stake ) { stakes.add( stake ); }
           public S get( int index ) { return stakes.get( index ); }
           public void shiftAll( long delta )
                  final List<S> stakesNew = new ArrayList<S>( stakes.size() );
                  S orig, copy;
                  for( int i = 0; i < stakes.size(); i++ ) {
                          orig = stakes.get( i );
                          copy = (S) orig.makeCopyAndShift( delta );
                          stakesNew.add( copy );
                  stakes.clear();
                  stakes.addAll( stakesNew );
    so that i could do:
    Trail<BasicStake> t = new Trail<BasicStake>();etc.
    how do i manage to get rid of the warning
    "Type safety: The cast from Stake to S is actually checking against the erased type Stake"
    with casting to (S) in the for-loop in the shiftAll function? Obviously the problem is that in interface Stake the return type of makeCopyAndShift should not be Stake but "<SameClassAsMe>", so that in BasicStake the method would be
    public BasicStake makeCopyAndShift( long delta ) { ... }. how do i accomplish that?
    thanks a lot!
    ciao, -sciss-

    This seems to work just fine for me:
    public class Foo {
        public static void main(String[] args) {
            Trail<Stake> trail = new Trail<Stake>();
            trail.add(new BasicStake(1L));
            trail.add(new AnotherStake(1L));
            trail.shiftAll(10L);
    interface Stake {
         public Stake makeACopyAndShift(long delta);
    class BasicStake implements Stake {
        private final long pos;
        public BasicStake(long pos) {
            this.pos = pos;
        public Stake makeACopyAndShift(long delta) {
            return new BasicStake(pos+delta);
    class AnotherStake implements Stake {
        private final long pos;
        public AnotherStake(long pos) {
            this.pos = pos;
        public Stake makeACopyAndShift(long delta) {
            return new AnotherStake(pos*delta);
    class Trail<S extends Stake> {
        private final List<Stake> stakes;
        public Trail() { stakes = new ArrayList<Stake>(); }
        public void add(S stake) { stakes.add(stake); }
        public Stake get(int index) { return stakes.get(index); }
        public void shiftAll(long delta) {
            final List<Stake> stakesNew = new ArrayList<Stake>(stakes.size());
            for(int i = 0; i < stakes.size(); i++ ) {
                Stake orig = stakes.get(i);
                Stake copy = orig.makeACopyAndShift(delta);
                stakesNew.add(copy);
            stakes.clear();
            stakes.addAll(stakesNew);
    }

  • variable-class using Generic Collection

    Within my TLDs I would like to use the <variable-class> attribute as much as possible for the sake of JSP authors.
    I cannot figure out how to specify that a varAttribute will be a generic collection. IE, if I want to return a set of strings, I would like to do
    <variable-class>
    java.util.set<java.lang.String>
    </variable-class>
    Of course I must escape the <,> and I tried using < and > but it was not effective.
    Is this even possible? I would appreciate any comments suggestions.

    Currently we are using a single domain account on every machine in this kiosk area. They are using just Internet Explorer and Adobe Reader. We are using Group Policy to lock down the machines. Each station is a full computer (older Dell OptiPlex) running
    Windows 7. We are looking at the possibility of removing the OptiPlex computers and replacing them with Wyse terminals. The backend would be a cluster of 3 servers running Hyper-V 2012 R2. On those would be running Windows Server 2012 R2 RDS.  We have
    tested this setup, but when creating the VDI collection there doesn't seem to be a way to use generic domain accounts. 
    Every person that uses these does not have an AD account and it looks like that would be a problem when trying to implement this.  I was just checking here to see if anyone had any ideas or had gone through a similar setup.

  • Implements interface method at the derived class

    Hi all.
    I have a class (Derived) that extends another class (Base).
    In the base class (Base) there is a method f() with its implementation.
    In the interface (C) there is also f() method exactlly like in the base class (Base).
    The derived class (Derived) is implements the interface (C).
    My question is:
    Do i have to implement the method f() in the derived class (Derived) ?

    My guess is that you probably have to, even if it's just to call the parent's method.
    This all sounds pretty sketchy. Why don't you just make the BASE class implement your interface?

  • What is the Use of Inner classes in Interface.

    Hi All,
    Most of us we know that We can define inner classes in the interface. Like
    public interface MyItf{
         Demo d = new Demo();     
         class Demo{
              Demo(){
              //some additional code here
    }Now I have following question in my mind:
    1. An Interface is pure abstract. Then why inner classes inside the interface?
    2. In what scenario, we can utilize these inner classes of interface?
    Plz Share your views on this...
    Thks for ur replies in advance.

    This we cando in defining Demo Class outside.That's no argument. You could write the programs in other languages, so why use Java? Just because you can use a top-level class instead, it's no argument against using an inner class. You also can make all attributes public... you don't o that either (I hope).
    Ok Also
    tell me how to pass an Object in inner class Demo. to
    the method of Interface.
    public abstract TheInterface.Demo doSomething(TheInterface.Demo d);
    Can u give some real time situation where this
    concept can be used.There are only very, very few. Just because it's possible, it doesn't mean it needs to be done or is done often.

Maybe you are looking for

  • Logical AND in MDX Reporting Services Parameter

    Hi, I would like to implement logical AND on a cube parameter. I have seen examples of hard-coded logical AND in MDX. (http://salvoz.com/blog/2013/12/24/mdx-implementing-logical-and-on-members-of-the-same-hierarchy/) But I'm not sure how to apply thi

  • Remote Control and Middle Tier

    We have a Middle Tier installed in a DMZ on a separate tree. Logins are ok, Applications are launched and installed, workstations are imported and inventoried however we cannot remote control the workstations. At the moment I am getting an 1858 Error

  • Warning in BCV

    When i open my reporting cockpit from any RPM object it gives me an error as follow. "Labels taken from field PROJ_DESCRIPTION are ambiguous" i am on PPM 5.0 Any clues how to fix this!! Thanks Edited by: sujata ramesh on May 6, 2011 12:34 AM

  • H.264 video wont play on iphone

    I went to the Apple store this weekend to check out the phone. I went to some video on the internet that I knew was H.264 encoded (for instance the movie at http://www.soothingenvironments.com/pacificrhythmsvideo.html) but it would not play on the ph

  • How to write a query?

    Hi all, Can you please explain me how to write a query joining tables in SAP ? Thanks, Avani.