Interface versus abstract

Hello,
i want to support users with method calls that
uses interfaces like this
public void performOrder(Order order) {
// do something
So now i must define a good interface the user can implement and
use these methods.
In my special case the user must decide between three different order
and so i must do some implementation work in an abstract Class
public interface Order {
public String getName();
public int getType();
public String getParam(String key);
public Object getContent();
public abstract class AbstractOrder implements Order {
//implementing the other interface methods ....
public static final int TYPE_SIMPLE = 0;
public static final int TYPE_XML = 1;
public static final int TYPE_DATA = 2;
protected void setContent(Object cont) {
if (type == TYPE_SIMPLE)
if (cont instanceof String) this.cont = cont;
if (type == TYPE_XML)
if (cont instanceof ParseEvent[]) this.cont = cont;
if (type == TYPE_DATA)
if (cont instanceof byte[]) this.cont = cont;
At this point i dont know is it necessity to use still the interface ?
Or can i drop it and make method calls like this:
public void performOrder(AbstractOrder order) {
// do something
I have read about it and memory usage seems be the same, but i dont know
JavaClasses that uses abstract classes in this way.
Thank you and with best regards
Tim
P.S. sorry about my bad english :(

Hello,
i want to support users with method calls that
uses interfaces like this
public void performOrder(Order order) {
// do something
So now i must define a good interface the user can
implement and
use these methods.
In my special case the user must decide between three
different order
and so i must do some implementation work in an
abstract Class
public interface Order {
public String getName();
public int getType();
public String getParam(String key);
public Object getContent();
public abstract class AbstractOrder implements Order
//implementing the other interface methods ....
public static final int TYPE_SIMPLE = 0;
public static final int TYPE_XML = 1;
public static final int TYPE_DATA = 2;
protected void setContent(Object cont) {
if (type == TYPE_SIMPLE)
if (cont instanceof String) this.cont =
this.cont = cont;
if (type == TYPE_XML)
if (cont instanceof ParseEvent[])
rseEvent[]) this.cont = cont;
if (type == TYPE_DATA)
if (cont instanceof byte[]) this.cont =
this.cont = cont;
At this point i dont know is it necessity to use still
the interface ?
Or can i drop it and make method calls like this:
public void performOrder(AbstractOrder order) {
// do something
I have read about it and memory usage seems be the
same, but i dont know
JavaClasses that uses abstract classes in this way.
Thank you and with best regards
Tim
P.S. sorry about my bad english :(
Well you dont need the interface anymore.
You can leave the interface if you want, but only for
future design issues.
hopes this help :)
Ohad Pinchevsky

Similar Messages

  • Nested interfaces and abstract methods

    Consider the following design:
    interface ZIF_INTERFACE_A.
      methods METHOD_A.
    endinterface.
    interface ZIF_INTERFACE_B.
      interfaces ZIF_INTERFACE_A.
      methods METHOD_B.
    endinterface.
    class CLASS_A_B definition abstract.
    public section.
      interfaces ZIF_INTERFACE_B
        abstract methods METHOD_B.
      interfaces ZIF_INTERFACE_A
        abstract methods METHOD_A.
      methods METHOD_C.
    endclass.
    class CLASS_A_B implementation.
      method METHOD_C.
      endmethod.
    endclass.
    When trying to implement this design, the syntax check is giving me an error:
    The method "METHOD_A" was declared as not ABSTRACT in a previous INTERFACES statement.
    If I implement the design with Java (either using a public nested interface, or using interface inheritance) there is no problem declaring both the methods METHOD_A and METHOD_B as abstract in the CLASS_A_B class.
    So it seems to me this might be a bug in the ABAP Objects implementation. Or perhaps I am missing something else here? Does anyone have any suggestions?

    Hello,
    Nested interfaces
    When you include an IF in another IF both of them will exist on the same level, there is no concept of hierarchy. So when you add interface ZIF_B in the implementing class ZCL_A_B, the interface ZIF_A is also "included" in the relationship with the class ZCL_A_B implicitly.
    Just try to activate the code below & you'll get the error: "Implementation missing for method zif_interface_a~method_a":
    CLASS class_a_b DEFINITION ABSTRACT.
      PUBLIC SECTION.
    *    INTERFACES zif_interface_a
    *      ABSTRACT METHODS method_a.
        INTERFACES zif_interface_b
          ABSTRACT METHODS: method_b.
        METHODS method_c.
    ENDCLASS.                    "CLASS_A_B DEFINITION
    CLASS class_a_b DEFINITION ABSTRACT.
      PUBLIC SECTION.
        INTERFACES zif_interface_b
          ABSTRACT METHODS method_b.
        INTERFACES zif_interface_a
          ABSTRACT METHODS method_a.
        METHODS method_c.
    ENDCLASS.                    "CLASS_A_B DEFINITION
    So when the compiler is trying to compile your code it encounter implementation of ZIF_INTERFACE_B it finds that the "included interface" method ZIF_INTERFACE_Amethod_a is not abstract. So when you declare ZIF_INTERFACE_Amethod_a as "abstract" in the next line the compiler throws this error.
    So you need to define ZIF_INTERFACE_A~method_a as abstract, before defining ZIF_INTERFACE_B. Like this:
    CLASS class_a_b DEFINITION ABSTRACT.
      PUBLIC SECTION.
        INTERFACES zif_interface_a
          ABSTRACT METHODS method_a.
        INTERFACES zif_interface_b
          ABSTRACT METHODS method_b.
        METHODS method_c.
    ENDCLASS.                    "CLASS_A_B DEFINITION
    In this case the compiler treats method_a as abstract while checking ZIF_INTERFACE_B. Hence it doesn't throw any error.
    Hope i'm clear.
    BR,
    Suhas
    PS: Whenever i face any problem while creating local classes, i create a dummy class in the class builder to check

  • Interfaces vs abstract classes

    hi,
    i have some kind of general idea about interfaces and abstract classes.but what are the situations that are most suitable for use interfaces and abstract classes..if u have any idea pls help me
    thanx and best regards,
    Kelum

    BigDaddyLoveHandles wrote:
    Reason for using interfaces #17: dynamic proxies.
    import java.lang.reflect.*;
    interface Fooable {
    void f();
    void g(int x);
    class Foo implements Fooable {
    public void f(){ System.out.println("Foo.f()"); }
    public void g(int x){ System.out.format("Foo.g(%d)", x); }
    public class ProxyExample implements InvocationHandler {
    private Fooable target = new Foo();
    public static void main(String[] args) {
    Fooable fooable = (Fooable) Proxy.newProxyInstance(
    Fooable.class.getClassLoader(),
    new Class[] { Fooable.class },
    new ProxyExample());
    fooable.f();
    fooable.g(17);
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println("Invoking: " + method.getName());
    return method.invoke(target, args);
    <cat position_relative_to_pigeons="among">
    Although libraries like ASM or CGLIB allow you to dynamically proxy classes, too
    </cat>

  • 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.

  • Documentation formats for interfaces, classes, abstract classes

    I am writing docs and want to make sure it conforms to best practices. Here is what I am doing:
    interfaces = [italics, not bold]
    classes = [not italics, bold]
    abstract classes = [italics, bold]
    I am most unsure of abstract classes?
    That is important to me, so please help me get this straight. thanks.

    Everybody knows that interfaces are blue, classes are red, and abstract classes use jumpy text.
    (that was a joke by the way)
    To my mind, using bold for abstract classes would be misleading. Just on an intuitive level, bold seems more concrete. But that's just me. I think the best thing to do is just to decorate the names with "<<interface>>" or "<<abstract>>" so there's no confusion.

  • Interfaces and abstract classes

    can anyone pls tell me where exactly an abstract class is used and where exactly an interface is to be used.
    i think the main difference between an abstract class and an interface is that an interface will have all the abstract methods whereas an abstract class can have one or more abstract methods--- if i am not wrong.
    anything to add? -- pls let me know on this....
    and also how they might be useful for us....

    Inheritance is the most popular feature of the OOP. By organising classes into class hierachy it gives an extra dimension to the encapsulation of abstract data types becauase it enables classes to inherit attributes/methods from other classes.
    There are two complementary roles of inheritance in OOP application.
    1. Specialisation: Extending the functionality of an existing class.
    2. Generalisation: Sharing commonality between two or more classes.
    So adding functionality by extending what exists at each level to create more specialized classes. This would create a hierarchy and would be helpful even later on in maintenance.
    Now when to have an abstarct class and when an interface. An interface just defines the basic functionality to be implemented by all the subclasses, though the way they are implemented may be different. e.g an interfaca animal can have all those functionalities common to all animals in basics but different in implementation. e.g all animals walk however some walk on two legs and some on four. All animals can see but they might have a different colour spectrum visible. So all such methods general but common can be in an interface.
    Abstract classes come into play at a more defined level e.g in an abstract class human though walk would be the same so implemented there and then but talking can be left abstract since there may be different languages involved.
    For more details refer to a good OOP book.

  • 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
    ~

  • Interface and Abstraction

    hi,
    i want to know in which scenario of the project we can go for interface or abstraction, can anybody tell me the scenario with example

    http://faq.javaranch.com/view?InterfaceVsAbstractClass

  • 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.

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

  • Hi all,Interface vs Abstract class

    Hi, All When We are going to use interface or an Abstract class,Can tell exactly
    Thanks in advance

    Interface class is used to contain methods that is to be defined by children classes. And abstract class, even though you can have all methods being abstract, but normally I would use this class for situation such as implement methods that are general enough to be used by the children classes and left others being abstract to be implemented by children classes later. I always think this relationship in a simple way:
    Abstract Class: Animal
    Interface : Leg, Tail, Hair, Feather, Head...
    Children classes: Tiger, Bird, etc.
    Thus, for class Animal class you can have methods such as:
    Abstract: canRoar, canSing, canFly, ...
    Non-abstract: isSleeping, isAlive...
    More example:
    Tiger extends Animal implements Leg, Tail, Head, Hair
    Bird extends Animal implements Leg, Feather, Head
    I could be wrong, correct me if so. Cheers.

  • Dilemma: interface or abstract

    Hi, I'm facing a design dilemma :
    I have two classes:
    The first one aims to find an equation root using a bisection algorithm and the second one using a regular falsi algorithm
    public class BissectionRootFinder{
    //some variables and methods
    }and
    public class RegulaFalsiRootFinder{
    //some variables and methods
    }but both have some methods in commune,e.g.,
    public int getIterationCount() { //code}I can make it work creating a parent class
    public abstract RootFinder{
    //abstract methods and concrete methods
    }and making the other two extending this one, or I can make this RootFinder class an interface and make the others implement this.
    The question is what should be my criteria to choose which approach to use?
    thanks in advance,
    Manuel Leiria

    Hi, I'm facing a design dilemma :
    I have two classes:
    The first one aims to find an equation root using a
    bisection algorithm and the second one using a
    regular falsi algorithm
    public class BisectionRootFinder{
    //some variables and methods
    }and
    public class RegulaFalsiRootFinder{
    //some variables and methods
    There ought to be some more abstraction here somewhere (e.g. the function you pass in, the bounds on the independent variable, etc.)
    but both have some methods in common,e.g.,
    public int getIterationCount() { //code}
    More than this, right? Maybe the method that would take a Function object and bounds and return a List of roots?
    I can make it work creating a parent class
    code]
    public abstract RootFinder{
    //abstract methods and concrete methods
    This would assume that you have concrete default implementations that made sense for all subclasses.
    and making the other two extending this one, or I can
    make this RootFinder class an interface and make the
    others implement this.Why not do both? Create a RootFinder and an AbstractRootFinder that implemented the interface? That way clients that didn't want the default behavior wouldn't have to take it.
    The question is what should be my criteria to choose
    which approach to use?Do both. That's what java.util Collections do.
    %

  • Help with interface and abstract

    Hi all
    what is the difference between an interface and an abstract class??

    Suppose if you want to implement all the methods we usually use the interface. if you define a method in the interface you must provide the implementation for this interface in the implementation class.
    In case of abstract, we can have both the abstract and non -abstract methods. Means abstract class can have the methods with the implementations defined in that class and abstract methods which needs to be implemented for the sub classes.

  • 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.

  • Interface and abstract class

    why we have constructor for abstract class and not for interface,

    When you call a constructor of a class which is extended by abstract class. It runs first the abstract class constructor and then child class constructor and initialize the member variables.
    while interface has no variable all final data so interface is no need to implement a constructor.
    Regards,

Maybe you are looking for

  • Network Magic Pro 5.5 incorrectly identifies my HTC Android phone as a Win7 PC

    Your Network Magic Version installed: 5,5,9195,0-Pure0 The type of connection to the Internet, like DSL The Brand of Modem and its Model Number the router is connected to. SpeedTouch546T2 (set up as a bridge) The Brand, Model, Hardware Revision of yo

  • Problem in a Spark List with local images dragMoveEnabled in a AIR 2 application...

    Hey, I developpe an AIR 2 application which allows user to load local images in a list. My list is dragEnabled, dropEnabled and dragMoveEnabled to change the order of the images. But when I'm trying to move an itemRenderer, my application crash with

  • PS-CC 2014 (latest) - changing 32-bit depth file to 16-bit

    I opened a file, happened to be 32-bit depth (I don't have too many, not even sure how that one got to be 32-bit), and because a lot of filters don't work with that, I changed it to 16-bit, but when I did that, the HDR toning dialogue appeared, and I

  • Variable value not showing in sql developer  tool

    HI, I am using sql developer tool. when i take mouse in variable name at debugging its not showing variable value and instead showing variable name data type and owner. but in past i have used sql developer tool where when i take mouse on variable at

  • Final data from report painter

    hi experts, i dont hv knowledge on report painter but i need to grab the data from it urgently. how do i know what is the internal table that stores the final data result... pls advise.. thanks