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();
}

Similar Messages

  • Can interface extend abstract class?

    Can interface extend abstract class?
    I tried to make a interface extend an abstract class but i got an error stating:
    interface expected here.
    Can anyone help me ?

    > ok, but can an interface implement an abstract class?
    No. An interface provides no implementation whatsoever. An abstract class can implement an interface, but not the other way around.
    http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
    ~

  • 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;
    }

  • Interface extending another interface

    Hi, i seem to have trouble finding on the internet what happends when one interface extends another interface. Can you explain it here or give me some link where i can find such information. Thanks

    have you tried to create on yet?  sometimes the best way to learn is just to do it.
    the truth is, extension of an interface is no different than the extension of a regular class.  when you finally implement your subClassed interface, you will just have to remember to add all unImplemented methods of both interfaces.  generally for soemthing like this its best to just implement multiple interfaces so that you will always know what you need to implement before run time otherwise you are bound to receive a lot of errors for not having implemented those methods.

  • Class implementing its own inner interface?

    Hi
    If I try to compile the following I get a "cyclic inheritance involving Data_set" compiler error on line 1:
        public class Data_set implements Data_set.Row
            public interface Row
                String column(final int a_index);
            public String column(final int a_index)
                return "something";
            public Row row(final int a_index)
                return new Row_impl();
            private class Row_impl
                implements Row
                public String column(final int a_index)
                    return "something else";
        }Is there any good logical reason why this should be disallowed, given that the interface is necessarily static and putting it inside the class seems like just a namespace issue that would involve no cyclic dependencies?
    Thanks
    Colin

    colin_chambers wrote:
    The Row component here only has meaning with respect to the Data_set container, so I do think that it makes sense to scope the Row name inside Data_set I respectfully disagree. Row is much more independent of a set of Rows than a DataSet is from Rows. A row can live on its own, a data set cannot. Your domain modeling is iffy at best.
    I would find it extra syntactic clutter to extract the row explicitly in these cases.You should never resort to a poor design to avoid "syntactic clutter", and it's quite likely that a proper design will reduce the clutter. My guess is that you've got a bad design through and through, and should consider maybe an inversion of control? Maybe what you're really looking for is a [Visitor pattern|http://en.wikipedia.org/wiki/Visitor_pattern]?
    public interface RowVisitor {
       void visitRow(Row row);
    public interface RowVisitable {
       void acceptRowVisitor(RowVisitor visitor);
    public class Row extends RowVisitable {
       public void acceptRowVisitor(RowVisitor visitor) {
          visitor.visitRow(this);
       //...other stuff
    public class DataSet implements RowVisitable {
       private final Collection<RowVisitable> children;
       public void acceptRowVisitor(RowVisitor visitor) {
          for ( RowVisitable row : children ) {
             row.acceptRowVisitor(visitor);
    DataSet set;
    RowVisitor printVisitor = new RowVisitor() {
       public void visitRow(Row row) {
          System.out.println(row);
    set.acceptRowVisitor(printVisitor);Another option would be an Iterator pattern.

  • Interface extends another interface

    Hi,
    Java has the concept of inheritance for both classes and interfaces.
    For classes inheritance is restricted to single inheritance. For multiple
    inheritance the class should implement an interface.
    But what about interfaces? Is multiple inheritance defined for interfaces?
    If so, is there any documentation on this? Are there OO-design-considerations
    that argue against multiple inheritance for interfaces?
    Thanks for your remark or pointing to literature,
    Sponiza

    You should try it and see. But, not to spoil the surprise, yes Java allows an interface to extend multiple interfaces, and yes, like most everything else there are design considerations to doing this. You might wade through the long discussion here:
    http://forum.java.sun.com/thread.jsp?thread=479908&forum=4&message=2241590
    and hit google or search these fora
    Good Luck
    Lee

  • Class constructor that implements an interface returns an  "interface", why

    Hi,
    I am studying some code that I need to understand well. This code works, I just don't understand the following:
    A class was defined extending an interface as so:
    public class GeometricShape implements Area {
    // constructor
    public GeometricShape() {
    System.out.println('bla);
    In another file, GeometricShape class was instantiated as follows:
    public class ExampleUse {
    Area g = new GeometricShape();
    My qustion is, why does the code above expects "new GeometricShape()" constructor to return an interface of type Area?
    Can someone explain?
    thanks

    Can someone explain?When a class implements an interface, or extends another class, or when a interface extends another interface, it means that anywhere an instance of the parent class or interface is expected, the child can be used.
    Wherever a Mammal is expected, you can provide a Dog or Cat or Whale or Human or NakedMoleRat. Each of those is a mammal.
    If you say "give me some food," and you don't specify anything else, the person you're talking to can hand you a hamburger or an apple or a bowl of rice. Any of those will meet the requirements you put forth.
    This is how the OO "is-a" relationship maps to Java.

  • Who can tell the difference of the abstract interface class?

    someone ask me that he had a abstarct interface class, such as :
    public abstract interface Book{
    I was comfused, who can tell me why use it?

    http://java.sun.com/docs/books/tutorial/java/interpack/interfaceDef.html
    Scroll down to the bottom.
    Drake

  • Interfaces that extend other interfaces

    I am trying to get a handle on the concept of interfaces extending other interfaces, and I just want to bounce my thoughts off the board and see where it goes. The Sun tutorial doesn't provide a lot of detail that I can see.
    Suppose we have interface A which has 3 methods (signatures), and interface B which extends interface A and adds two methods of its own. Suppose also we have a class C which can implement A or B or both.
    1. Having B extend A provides a heirarchy of types, with A above B
    2. If class C implements interface B, then the extends relationship B has with A forces C to provide implementations for the methods of A as well as B. On this point I am not certain.
    3. Having B extend A allows us to essentially add methods to the interface defined by A without forcing those changes on owners of the classes that already extend A.
    Sound OK? Anywone have anything to add?
    Edited by: Fguy on Aug 27, 2009 7:07 PM

    jverd wrote:
    Fguy wrote:
    >
    3. Having B extend A allows us to essentially add methods to the interface defined by A without forcing those >>changes on owners of the classes that already extend A.Huh? Not sure what you're saying, here, but extending A means we're defining a type that's a specialized or >enhanced type of A. Implementors can implement A if all they need is a run of the mill A, or B if they need the >specialized/enhanced version.Thanks.
    What I meant was that class C implements interface A, and interface B is not a part of the picture yet. Then, by creating a new interface B that extends A, rather than just adding the new methods to A, then you are not forced to add new method implementation to class C right away.That would be one situation in which one might do that. Of course, it's also not uncommon to design the two distinct interfaces from the start
    Ok I'll pick up on that, the situation that led to this thread was me trying to understand why the List interface extends the Collection interface, and why the ArrayList class implements both interfaces. And I eventually decided that designing kind of an arrangement makes sense from the start as you say, because there are probably reasons why you'd want that type heirarchy, or higher level abstraction. with different levels of functionality in the respective implementations. I can't really think of any other reason.

  • Error while extending two interfaces.

    I am using Weblogic Integration 8.5. When an interface extends two interfaces. Out of which one has a clone method declared. <br>
    This IDE is giving error as <br>
    ERROR: Sample.java:3: This type inherits two versions of method java.lang.Object clone(), one from java.lang.Object and another from com.ParentOne, that have conflicting access restrictions. <br>
    <b>Following are code snippets.</b><br>
    public interface Sample extends ParentOne, ParentTwo {} <br>
    public interface ParentOne { <br>
    public Object clone() throws CloneNotSupportedException; <br>} <br>public interface ParentTwo {} <br>
    <b>Same is working fine in other IDE with the bea JDK as well as sun's jdk. </b>
    <br>
    Can anyone help on this? Many Thanks.

    I am using Weblogic Integration 8.5. When an interface extends two interfaces. Out of which one has a clone method declared. <br>
    This IDE is giving error as <br>
    ERROR: Sample.java:3: This type inherits two versions of method java.lang.Object clone(), one from java.lang.Object and another from com.ParentOne, that have conflicting access restrictions. <br>
    <b>Following are code snippets.</b><br>
    public interface Sample extends ParentOne, ParentTwo {} <br>
    public interface ParentOne { <br>
    public Object clone() throws CloneNotSupportedException; <br>} <br>public interface ParentTwo {} <br>
    <b>Same is working fine in other IDE with the bea JDK as well as sun's jdk. </b>
    <br>
    Can anyone help on this? Many Thanks.

  • Regarding Abstract Interface

    Hello,
    What makes the difference between the Normal Interfaces and Abstract Interfaces. Is there any technical differences between these two interfaces.
    Thank you

    Hi,
    You use a message interface to describe a platform-independent or programming-language-independent interface, which you want to use to exchange messages between application components using SAP Exchange Infrastructure.
    When you create a message interface you define the communication parameters by using the attributes Mode and Category as Sync/Async, Inbound/Outbound or Abstract.
    Message interfaces of this category can perform the role of an inbound or outbound interface within integration processes, depending on whether it is used to send or receive a message. For this reason, no direction is specified during definition. In integration processes, you can use the same abstract interface to receive and send a message. Abstract message interfaces generally receive the message from an outbound interface of a sender system and send it to an inbound interface of a receiver system, thus performing a complementary role.
    BPM can interact and deal with only Abstract Interfaces. And so, if you have a transformation step inside the BPM , the source and target interface will have to be abstract interfaces.
    These characteristics determine the direction of an interface:
    ·        An outbound interface sends a request message that is only used to make a communication party aware of data sent, without waiting for a response message. In the case of the latter, we also refer to publishing interfaces.
    ·        An inbound interface receives a request message that you reply to with a direct response message or whose data you can process in the system without a response.
    Thanks
    Swarup

  • Ok, easy question...About implements and extends

    Im sure this will be simple to all of you, but I am still new to the Java language. I want to know the difference between using "extends" and "implements" in the way that its used in these examples:
    With the "extends" keyword...
    class PrimeThread extends Thread {
             long minPrime;
             PrimeThread(long minPrime) {
                 this.minPrime = minPrime;
             public void run() {
                 // compute primes larger than minPrime
         }And then with the "implements" keyword...
    Using the interface "Runnable"
    class PrimeRun implements Runnable {
             long minPrime;
             PrimeRun(long minPrime) {
                 this.minPrime = minPrime;
             public void run() {
                 // compute primes larger than minPrime
         }Can someone please explain when you would want to use the "extends" version and when you would want to use the "implements" version? Sorry for practically insulting your intelligence...I'm just trying to learn this language without a book as an experiment to see (how much/how fast) I can learn purely using the web resources...Thanks to anyone who wishes to explain this to me.

    That's pretty much it.
    An interface may extend zero or more other interfaces.
    A class (except Object) always extends exactly one other class--either the class explicitly named with "extends", or Object if "extends" is not present.
    A class may implement zero or more interfaces.
    "Extends" implies a parent-child relationship. Interfaces extend other interfaces, and classes extend other classes.
    "Implements" implies a contract stating that the class will provide implementations for all the methods declared in the interface (or the class will be declared abstract). If a class implements A, and A extends B, then that class must implement all methods declared in A and all methods declared in B.
    Does this help?

  • Generic interface in abstract super class

    hello java folks!
    i have a weird problem with a generics implementation of an interface which is implemented in an abstract class.
    if i extend from this abstract class and try to override the method i get this compiler error:
    cannot directly invoke abstract method...
    but in my abstract super class this method is not implemented as abstract!
    do i have an error in my understanding how to work with generics or is this a bug in javac?
    (note: the message is trown by the eclipse ide, but i think it has someting to do with javac...)
    thanks for every hint!
    greetings daniel
    examples:
    public interface MyInterface <T extends Object> {
       public String testMe(T t);
    public abstract class AbstractSuperClass<T extends AbstractSuperClass> implements MyInterface<T> {
       public String testMe(T o) {
          // do something with o...
          // now we have a String str
          return str;
    public final class SubClass extends AbstractSuperClass<SubClass> {
       @Override
       public String testMe(SubClass o)
          return super.testMe(o);
    }

    Hi Wachtda,
    Firstly, T extends Object is redundant as all classes implicitly extend the Object class.
    Therefore :
    public interface MyInterface <T> {
       public String testMe(T t);
    }Secondly, abstract classes may have both abstract and non-abstract instance methods. Also, two methods, one abstract and one non-abstract, must have a different signature.
    The following example will give a compile error because the methods share the same signature :
    abstract class Test {
         public void sayHello() {
              System.out.println("Hello");
         abstract public void sayHello();
    }Therefore, to make an interface method as abstract would simply block the possibility of implementing it.
    BTW, you can do this :
    abstract class Test {
         public void sayHello() {
              System.out.println("Hello");
         abstract public void sayHello(String name);
    }Finally, there's no bug in javac.

  • JSF annotations on interface or abstract class

    Hello,
    Can we use JSF 2 annotations like @ManagedBeans or @SessionScoped for interfaces and abstract classes? I mean, will it works if I implement an interface which declares some annotation(s) and the managed bean class itself doesn't have annotations?
    Thanks a lot!
    Oleg.

    Okey, an example:
    @ManagedBean
    public abstract class MyAbstractClass {
    public class MyClass extends MyAbstractClass {
    Does it work if I access MyClass by ValueExpression #{myAbstractClass} ? Will be MyClass automatically annotated with @ManagedBean?
    Best regards.
    Oleg.

  • Interface and Abstract class difference

    An interface can be used in such a way that we don't know the class of object assigned to a reference of that interface type until runtime. Can we use the abstract class in a similar way too?
    The difference between an abstract class and interface can be listed as
    1. Interface can not have implementation of any method
    2. The usage of interface and class is one other difference
    3. What other differences we have?

    Yes an abstract class can be used in a similar way. The main issue with an abstract class is that you extend it and you can only extend one class so that can be a huge limitation that an interface does not give you.
    Here's another one that is often overlooked: use both.
    public abstract class SomeBaseClass implements Runnable {
      public abstract void someAbstractMethod();
      public void someMethodWithADefaultImplementation(){
        System.out.println("Hello!");
    }Any class that extends SomeBaseClass (and is not abstract) will need to implement the run() method of the Runnable interface.

Maybe you are looking for