Overidding a method defined in a Final class via an interface.

Hi,
Is it possible to override methods of a final class
which has implemented methods of an interface.
interface A{
     void method1();     
public final class FinalityA implements A{
     public void method1(){
          // Code
}Now,I would like to override the functionality of
method1() defined in class FinalityA.
I cannot subclass FinalityA as this class is final.
// Invalid.
class Myclass extends FinalityA{
}Is it possible to override the functionality defined in method1()
Please suggest

You can do:
public class MyClass implements A {
   private final FinalityA _wrapee;
   public MyClass(FinalityA wrapped) {
       _wrapee = wrapped;
  public void method1() {
       .. do some special processing
      _wrapee.method1();
       .. do some more stuff
        }If you want to get really clever you can do stuff with java.lang.reflect.Proxy that allows you to automatically pass on calls to interface methods (this kind of thing is increasingly used for stuff like transaction management).
What you can't do is to fiddle with the code of the method if called from a reference to FinalityA rather than a reference to the interface. The whole point of final classes and methods is to prevent that.

Similar Messages

  • A question about the getProperty method defined in the Security class

    Hello Everyone!
    I would like to ask a question about the getProperty method defined in the
    Security class.
    public static String getProperty(String key) Do you know how can I exract the list of all possible keys?.
    Thanks in advance,

    I found the answer, in fact the keys are defined in the java.security file.

  • Final class CL_PT_EMPLOYEE. Why?

    Today I tried to make a local class as a sub class to CL_PT_EMPLOYEE and found out that this class was defined as a final class.
    Does any one have an idea why the class CL_PT_EMPLOYEE has been defined as a final class. Is that to stop any re-use of SAP code. Any one from SAP?
    (P.S. Please dont over exert yourself in explaining the meaning of a final class to me)

    Hello Ahsan,
    Sorry if I wasn't clear, this is an example of sarcasm. The use of the word "apparently" should have given it away, and particularly the exaggeration "untold harm" was supposed to convey the impression that I do not believe this for one minute.
    This is just what I was told by OSS, it was about two years ago and there were several conversations back and forth, some by telephone. Basically, by allowing us to create a subclass we would be able to redefine a superclass method and... well, I am not quite sure what the risk is to be honest, but SAP seemed to think this was a dangerous tool to let loose upon us customers. It was the type of response that is under discussion in this thread:
    A (disbelieving) question regarding the OSS
    As others have a really suggested, composition is the answer and usually a better technique.
    The only time when you should need to create a subtype of a standard SAP class is if you have to use polymorphism to feed your own version of an SAP class to something else that expects a particular class as input. This is the <sarcasm>"risky" part by which we could damage the system according to OSS</sarcasm>, yet is quite commonly used in workflow. In the BOR world they even provided a neat feature called delegation (basically a reverse inheritance) to accomplish this.
    Cheers,
    Mike
    Edited by: Mike Pokraka on Sep 2, 2008 5:59 PM - clarification

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

  • In which case we need a class with all methods defined as abstract ?

    In which case we need : A class with all methods defined as abstract (or should we go for Interface always in this case)

    The concept of interface and abstract class overlaps sometime, but we can have the following thumb rule to use them for our specific need:
    1) Interface: This should be use for code structuring. They standardize your application by providing a specific contract within and outside. If you think abstract class also provide structure, then reconsider as it limits the structure re-usability when there are many super-classes to inherit from. Java allow multiple inheritance by interface and not by Abstract class.
    2) Abstract Class: This should be use for code-reusability. Interface doesn't have any code so can't be used for code-reusability.
    Actually we can use both to provide the best.Taking a refernce to J2EE framework, the "Servlet" is an interface. It laids down the contract/structure for any code to be a servlet.
    But the "GenericServlet" class is an abstract class which provides implementation of some of the methods defined in the "Servlet" interface and leave some method abstract.
    Thus we see that the "Servlet" interface standardise the structure and the "GenericServlet" abstract class gives code re-usability.
    Original Question:
    In which case we need a class with all methods defined as abstract ?To this question, as all methods are abstarct we don't have option for code-reusability. so why not add standard structure concept to your application. So if there is not any restriction on making the field variable(if any) as final, we should go with the interface concept.

  • How do I access classes and methods defined in a wsdl file

    I have been provided a wsdl file I need to find out how do I access classes and methods defined in a wsdl file directly instead of doing a wsdl2java...

    Several comments :
    1- is there any reason to have blank chars inserted after the path ? Seems that you already have a problem there. If possible, try to solve the problem at the source
    2- the end of line char is usually CR (Carriage Return, aka ASCII char 13 = $0D = Control-D). But LF (Line Feed = 10 = $0A = control-A) is also used (platform dependent). In LV, you can use the "Concatenate strings" function to add/insert control chars (found in the String Control Palette). However, this will not solve your problem of unwanted added blank chars at the end of your string.
    3- you can use the Trim white space.vi (in the "Additionnal string functions" sub-palette) to remove ALL the spaces in your string
    4- you can build your own "end space remover" function. :
    reverse the string, wire to a "Match pattern" function, use " +" (space + "+") to search for any number of spaces, reverse again the "after substring".
    5- there is no point 5 :-)
    You may find interesting description of ASCII chars here
    Chilly Charly    (aka CC)
             E-List Master - Kudos glutton - Press the yellow button on the left...        

  • Final method and final class

    What is final method and final class in abap objects.

    ejp wrote:
    Since that doesn't work--or would overyy complex to implement... would be impossible to implement. Once the method-local copy goes out of existence, ipso facto it can never be changed. This is the whole point.I consider it impossible too, but I'm not a language/compiler/runtime expert, so I allowed for the possibility that there could be some way to do it--e.g. local variables that are references in inner classes live on the heap instead of the stack, or something. My point isn't that it's possible, just that if it were somehow to be done, it would by ugly, so we may as well consider it impossible--it just ain't gonna happen.
    we go with the logic, "Okay, we need to copies, but keeping them in sync is a nightmareNo, it is +meaningless.+No, it's not meaningless. If we have the two copies, and they're not final, then after the inner object is created, the method can continue running, and either the method or the inner object could modify its copy. As far as our code knows, it's the same variable, so there'd have to be some way to keep them in sync. That's either impossible or undesirably complex--like the above, it doesn't matter which--so it's final in order to get past the issue of keeping the copies in sync.

  • How do I redefine a Method of a Final Class?

    Hi,
    Is it possible to redefine a method of a final class and if so, can someone please give me a brief example of that technique?
    Thank you very much!
    Andy

    Hi,
    Please find the example.
    Program Description :      Interface I1 contains two methods : M1 and M2.
    I1 is included and incorporated in class : C1 with M2 as a final method. Both the methods are implemented in class C1.
    Class C2 is a subclass of class C1. It redefines method : I1M1 and re-implements it, but it does not do that for I1M2 as that is declared as final method.
    In the START-OF-SELECTION block, object OREF1 is created from class C1 and OREF2 from class C2 and both the methods M1 and M2 are called using both the objects.
    Example:
    report ytest .
    interface i1 .
      methods : m1 ,
                m2 .
    endinterface.
    class c1 definition.
      public section.
       interfaces : I1 final methods m2 .
    endclass.
    class c1 implementation.
      method i1~m1.
       write:/5 'I am m1 in c1'.
      endmethod.
      method i1~m2.
       write:/5 'I am m2 in c1'.
      endmethod.
    endclass.
    class c2 definition inheriting from c1.
      public section.
       methods : i1~m1 redefinition .
    endclass.
    class c2 implementation.
      method : i1~m1.
       write:/5 'I am m1 in c2'.
      endmethod.
    endclass.
    start-of-selection.
      data : oref1 type ref to c1,
             oref2 type ref to c2 .
       create object : oref1 , oref2.
       call method  : oref1->i1~m1 , u201C Output : I am m1 in c1
                      oref2->i1~m1 , u201C Output : I am m1 in c2
                      oref1->i1~m2 , u201C Output : I am m2 in c1
                      oref2->i1~m2 . u201C Output : I am m2 in c1
    Output :      I am m1 in c1 
    I am m1 in c2 
    I am m2 in c1 
    I am m2 in c1 
    Thanks,
    Anitha

  • Protected method in Final Class

    Hi all
    Can we have protected method in Final class ?
    If so how is it possible ?
    Vicky

    Final classes cannot have subclasses and hence no need of protected methods.
    No comments on possibility.
    Check this link from SAP help to know more :
    http://help.sap.com/saphelp_nw70/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htm
    Hope this helps you.
    Edited by: Harsh Bhalla on Dec 19, 2009 3:35 PM

  • Final class: methods/fields automatically final?

    As a final class cannot be subclassed, methods/fields cannot be overwritten. But is there a performance/security difference between
    public final class A {
        public static final String S = "S";
        public final static do() {}
    }and
    public final class A {
        public static String S = "S";
        public static do() {}
    }?

    As a final class cannot be subclassed, methods/fields
    cannot be overwritten. But is there a
    performance/security difference betweenAll methods of a final class are implicitly final.
    Fields are not (you could demonstrate that very easily by altering the value of the field S in your example).

  • Does somebody know,what diffrence between overide a method or to hide?

    Hi All!
    Does somebody know,what diffrence between overide a method or to hide?
    Thanks.

    Static methods are class methods. This means you can
    access them without having any existing instances of
    the class (className.methodName()).True. Further, I would argue that the language designers made a mistake in allowing static methods to be accessed via variables, since it only seems to add confusion.
    A static method (or any other method in fact) can be
    overriden by a sub class as long as it's not declared
    final in the superclass.Not true. A static method can be hidden by a subclass;Only instance methods can be overridden. If you disagree with this, please take time to read the link in one of my earlier posts, and look at the examples here:
    http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#39617
    The difference between overriding and hiding can be demonstrated by the code below:
    class Superclass {
      public void instanceMethod() {
        System.out.println("Superclass.instanceMethod");
      public static void classMethod() {
        System.out.println("Superclass.classMethod");
    class Subclass extends Superclass {
      public void instanceMethod() {
        System.out.println("Subclass.instanceMethod");
      public static void classMethod() {
        System.out.println("Subclass.classMethod");
    public class MainClass {
      public static void main(String[] argv) {
        Superclass x = new Subclass();
        x.instanceMethod();
        x.classMethod();
    }The output is:
    Subclass.instanceMethod
    Superclass.classMethodThe instance method has been overriden and the class method has been hidden. The difference is:
    When you override a method, you are effectively saying "use this implementation instead of the one in the superclass", and client code has no way of calling the superclass' implementation. The implementation has been replaced.
    When you hide a method, the original implementation from the superclass can still be called by client code, via the superclass' class name, or a variable whose compile-time type is that of the superclass.

  • Final classes

    as far as optimization goes, if i define a class as final, do i need to define my methods as final as well?
    does the compiler treat all my methods as final if i define my class as final?
    thanks in advance

    shaun - if I can't create a subclass of a class then I
    can't instantiate the object anyways...so the terms
    are kind of interchangable. :)
    ShaunWhat? Of course you can still instantiate a final class - you just can't subclass it. The two terms are not "kind of interchangeable".

  • Java.lang.VerifyError: Cannot inherit from final class

    Hi i am trying to call a crystal report from jsp but i am getting this error
    error:-
    org.apache.jasper.JasperException: Cannot inherit from final class
         org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
         org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
         org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
         org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    root cause
    javax.servlet.ServletException: Cannot inherit from final class
         org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:841)
         org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:774)
         org.apache.jsp._1_jsp._jspService(_1_jsp.java:95)
         org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
         org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
         org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
         org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    root cause
    java.lang.VerifyError: Cannot inherit from final class
         java.lang.ClassLoader.defineClass1(Native Method)
         java.lang.ClassLoader.defineClass(Unknown Source)
         java.security.SecureClassLoader.defineClass(Unknown Source)
         org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1853)
         org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:875)
         org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1330)
         org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1209)
         java.lang.ClassLoader.loadClassInternal(Unknown Source)
         com.crystaldecisions.reports.sdk.ReportClientDocument.open(Unknown Source)
         org.apache.jsp._1_jsp._jspService(_1_jsp.java:64)
         org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
         org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
         org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
         org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    my jsp is
    <%@ page import="com.crystaldecisions.report.web.viewer.*" %>
    <%@ page import="com.crystaldecisions.sdk.occa.report.data.*"  %>
    <%@ page import="com.crystaldecisions.reports.sdk.ReportClientDocument" %>
    <%
        Object reportSource = session.getAttribute("reportSource");
        if (reportSource == null)
           String report = "report.rpt";
           ReportClientDocument reportClientDoc = new ReportClientDocument();
           reportClientDoc.open( "report.rpt", 0);
           reportSource = reportClientDoc.getReportSource();
           session.setAttribute("reportSource", reportSource);
        CrystalReportViewer viewer = new CrystalReportViewer();
        viewer.setReportSource(reportSource);
           viewer.setEnableLogonPrompt(false); 
        viewer.setOwnPage(true);
        viewer.processHttpRequest(request, response, getServletConfig().getServletContext(), null);
    %>
    and in my WEB-INF/lib i included jar files namely
    Concurrent.jar,CrystalCharting.jar,CrystalCommon.jar,CrystalContentModels.jar,CrystalExporters.jar,CrystalExportingBase.jar,CrystalFormulas.jar,CrystalQueryEngine.jar,CrystalReportEngine.jar,CrystalReportingCommon.jar,icu4j.jar,jrcerom.jar,keycodeDecoder.jar,log4j.jar,MetafileRenderer.jar,rasapp.jar,rascore.jar,rpoifs.jar,serialization.jar,URIUtil.jar,xml-apis.jar,xbean.jar,xercesImpl.jar,webreporting.jar,webreporting-jsf.jar
    please help me

    You can't inherit from a final class. 
    So - are you trying to inherit from a final class?
    Sincerely,
    Ted Ueda

  • Cannot use user classes in webservices interface of appmodule custom method

    Hi,
    I need to expose an application module's custom method that uses user defined classes as parameters and return types as a web service interface. I have not been able to do that. Below are more details.
    In particular, I have an app module that has a client interface with a custom method that takes and returns a serializable Emp object (user defined classes/objects). I need to expose this app module as a web service through the use of a service interface.
    When i run the "Create Service Interface" wizard, under service custom methods (Create Service Interface Step 2 of 4 wizard), i don't see my custom method under "Available."
    In addition, the JDev message log says: Business Components: Method "yourCustomMethod(Emp):Emp" cannot be exposed via service interface because the return type "com.mypackage.Emp" is not supported.
    From what i understand by reading the docs below, it seems that in an application module's service interface (Webservice) you only can use simple data type or java.util.list or AttributeList (which is a wrapper for any viewRow). In other words, i cannot use my user defined object in the custom method parameters and return types.
    This seems to be an artificial limitation of webservice creation through the use of app module because one can use complex/user defined objects in methods of a java class and expose them through a webservice.
    I would appreciate if of the tooling PMs can make a well thought out comment.
    11.2.3 What You May Need to Know About Method Signatures on the Service Interface
    http://docs.oracle.com/cd/E12839_01/web.1111/b31974/bcextservices.htm#CJAEHFJD
    9.8.4 What You May Need to Know About Method Signatures on the Client Interface
    http://docs.oracle.com/cd/E12839_01/web.1111/b31974/bcservices.htm#CHDFHGII
    Thanks,
    AJ
    ps: I am using Build JDEVADF_11.1.1.5.0_GENERIC_110409.0025.6013.

    Hi,
    question 2 doesn't apply as the client interface is for Java access to public methods. For the Service Interface, the document lists the supported data types you can use in arguments and return values. To expose a custom methods, what about using a POJO that accesses ADF BC (using create root application module) and then expose your POJO as a JAWS service. This will allow you to expose the data queried from ADF BC through your custom classes.
    Frank

  • Abstract class & final class

    Which is more correct... Or which is the more preferred way.
    abstract class A
      final method1(){}
      final method2(){}
      final method30(){}
    }or
    final class A
      private A(){}
      method1(){}
      method2(){}
      method3(){}
    }My understanding is that both classes cannot be instantiated. The first one requires writing 'final' for EACH method. The second one involves writing a private constructor.

    It depends on what you are trying to do. If you are trying to make a class that can be sub-classed but has some methods that cannot be overriden, then "abstract class A" with final methods is the way to go. If you want a class that cannot be sub-classed or instantiated then "final class A" with private constructor is the way to go.
    classes cannot be instantiatedOnly true for "final class A" because you made the only constructor private. Not true for the abstract one.// you forgot return values for the methods
    abstract class A
      final static /*void*/ method1(){}  // package private
      final static /*void*/ method2(){}  // package private
      final static /*void*/ method30(){} // package private
    // this would work
    A a = new A(){};
    // if I am in the same package as A, then this would work
    a.method1();
    // or this
    public class B extends A
      public B(String whatever)
        // Although, I cannot override the super methods
        // becuase they are all declared as final
        // I can only invoke them if I am in the same package.  You
        // declared them as package private instead of class "private"
    }Using final as a class modifier disables the ability to sub-class it but does not disable the ability to create an object of that class. You must make a private constructer. If the only constructor is "private" then you can't subclass or instantiate, so making the class final is uneeded.

Maybe you are looking for