Abstract method and class

I'm a beginner in Java and just learn about abstract method and class.
However, i am wondering what is the point of using abstract method/class?
Because when I delete the abstract method and change the class name to public class XXXX( changed from "abstract class XXXX), my program still runs well, nothing goes different.
Is it because I haven't encountered any situation that abstract method is necessary or ?
Thanks!

Yes - you probably haven't encountered a situation where you need an abstract.
Abstract classes are not designed to do anything on their own. They are designed to provide a template for other classes to extend by inheritance. What you have build sounds like a concrete class - one which you are creating instances of. Abstract classes are not designed to be ever instantiated in their pure form - they act like a partial building block, which you will complete in a class which extends the abstract.
An example might be a button class, which provides some core functionality (like rollover, rollout etc) but has an empty action method which has to be overwritten by a relevant subclass like 'StartButton'. In general, abstract classes may not be the right answer, and many people would argue that it is better to use an interface, which can be implemented instead of extended, meaning that you can ADD instead of REPLACING.
Not sure if that helps.. there are whole chapters in books on this kind of thing, so it's hard to explain in a couple of paragraphs. Do some google searches to find out more about how they work.

Similar Messages

  • Smart forms - Identifying the calling method and class

    Hi Experts,
    Is there a way to identify the method and class names that called a smart form..
    Any inputs or pointers in this regard woul;d be of great help.
    Regards,
    Kris.

    Hi Kartik,
    The procedure you suggested is good to identify the class by manual intervention.
    But I need the class/mehtod name in the program as the logic of the code depends on which class called the smart form.
    Any idea how this can be read?
    Regards,
    Kris.

  • Abstract Methods and Override Don't work For SubPackages

    I have an abstract class called "A" in a package called test.
    package test;
    public abstract class  A
        int x;
        public A (int x)
            this.x = x;
        abstract void testMethod();
    }And I have a sub class of "A" called "SubA" in a sub package of "test" called "test.subPkg":
    package test.subPkg;
    import test.A;
    public class SubA extends A {
        public SubA(int x)
            super(x);
        @Override
        void testMethod()
            throw new UnsupportedOperationException("Not supported yet.");
    }Now when I try to compile, it gives me the error:
    /home/eric/NetBeansProjects/drum-tabber4/trunk/src/test/subPkg/SubA.java:6: test.subPkg.SubA is not abstract and does not override abstract method testMethod() in test.A
    public class SubA extends A {
    /home/eric/NetBeansProjects/drum-tabber4/trunk/src/test/subPkg/SubA.java:14: method does not override or implement a method from a supertype
    If I put SubA in the above package "test" instead of "test.subPkg", it compiles fine. But I don't want to do that because I have a lot of sub classes that are organized differently in separate sub packages.
    If I make A an interface, it compiles fine. But I don't want to do that either because I want A to implement the constructor.
    Anyone have any idea of how to fix this issue?

    Now when I try to compile, it gives me the error:
    /home/eric/NetBeansProjects/drum-tabber4/trunk/src/test/subPkg/SubA.java:6: test.subPkg.SubA is not abstract and does not override abstract method testMethod() in test.A
    public class SubA extends A {
    /home/eric/NetBeansProjects/drum-tabber4/trunk/src/test/subPkg/SubA.java:14: method does not override or implement a method from a supertype
    If I put SubA in the above package "test" instead of "test.subPkg", it compiles fine. But I don't want to do that because I have a lot of sub classes that are organized differently in separate sub packages.Thats because method testMethod() in class A has a default access modifier. Meaning, that testMethod() will only be visible within the same package and not outside of package test. Expand the access modifier of testMethod() in class A, either make it protected or public, like this...
    protected abstract void testMethod();
    OR
    public abstract void testMethod();

  • Abstract methods and subclasses

    I have a class, DfContainer that has a method whose return type is of an abstract class, DfObject.
    There are several classes that extend the abstract class. Each of them has methods that are unique to them.
    But in order to be able to execute the methods using the object returned through the method in DfContainer, I need to define each method as abstact in DfObject.
    Now say, I need a method called getNames() in the class DfNames that extends DfObject, and I need a method called getRanks() in DfOrganization that also extends DfObject.
    I would define those two methods as abstract in DfObject. But, I would need to add getRanks() to DfNames, and add getNames() to DfOrganization, even if I don't need them there.
    That's what I'm doing right now. If I don't cast the object returned from the method in DfContainer to a subtype of DfObject, is this the correct approach?
    Thanks,
    Moshe

    Why don't you define an empty method body in the abstract class, that does not have to be overridden in the subclass.
    An abstract class can contain fully implemented methods. You can then override them or not as you choose. You don't have to define your individual methods as abstract.
    Look at the way the various listeners and adapters are done in AWT. You have a listener that's an interface, then a dummy class that provides do-nothing implementations for all methods in the interface, so when you want to override one method you only need to override that specific method, not all the methods in the interface. You should be able to do something similar.

  • How to make a field manadatory within ALV Grid using methods and classes

    Hi,
    I am using ALV Grid using set_table_for_first_display
    inside my dialog programming. I have a field called project number inside my grid which has to be made as mandatory field.
    I have defined a method called catch_data_changed inside my class lcl_event_receiver. This method captures the changes made to one of the fields inside my ALV grid and displays all the default values of the other fields from the grid.
    Now, i have to make project number which is one of my fields inside my ALV grid as mandatory. At the field catalog level i did not find any such option for making a field as required field.
    Is there any other way, i can accomplish this within the ALV grid?

    from my understanding from ur question, i understood that u want the editable field inside alvgrid to be mandatory.
    i dont know anthing in fieldcat, but u can try the following logic.
    FORM DATA_CHANGED  USING P_ER_DATA_CHANGED TYPE REF TO
    CL_ALV_CHANGED_DATA_PROTOCOL .
      DATA: L_VALUE TYPE LVC_VALUE,
        ls_mod_cell type lvc_s_modi.
      READ TABLE P_ER_DATA_CHANGED->MT_MOD_CELLS INTO LS_MOD_CELL.
    if sy-subrc = 0.
        CALL METHOD P_ER_DATA_CHANGED->GET_CELL_VALUE
          EXPORTING
            I_ROW_ID    = LS_MOD_CELL-row_id
            I_FIELDNAME = LS_MOD_CELL-fieldname
          IMPORTING
            E_VALUE     = L_VALUE.
    IF LS_MOD_CELL-FIELDNAME <> 'fieldname what u want'
    MESSAGE 'ENTER VALUE INTO (fieldname u want) ' TYPE 'I'.
    ENDIF.
    ELSE.
    MESSAGE 'ENTER VALUE INTO (fieldname u want) ' TYPE 'I'.
    ENDIF.

  • What are abstract classes/methods and what are they for?

    Hi,
    I've just heard about abstract classes and methods and I'm just wondering what exactly they're used for, and why are they there for the Graphics class for example?
    Cheers.

    raggy wrote:
    bastones_ wrote:
    Hi,
    I've just heard about abstract classes and methods and I'm just wondering what exactly they're used for, and why are they there for the Graphics class for example?
    Cheers.Hey bro, I'll try to solve your problemYou have to know two important concepts for this part. 1 is Abstract classes and the other is Interface classes. Depends on the nature of the project, you need to set certain level of standards and rules that the other developers must follow. This is where Abstract classes and Interface classes come into picture.
    Abstract classes are usually used on small time projects, where it can have code implementation like general classes and also declare Abstract methods (empty methods that require implementation from the sub-classes).Wrong, they are used equally among big and small projects alike.
    Here are the rules of an Abstract class and method:
    1. Abstract classes cannot be instantiatedRight.
    2. Abstract class can extend an abstract class and implement several interface classesRight, but the same is true for non-abstract classes, so nothing special here.
    3. Abstract class cannot extend a general class or an interfaceWrong. Abstract classes can extend non-abstract ones. Best example: Object is non-abstract. How would you write an abstract class that doesn't extend Object (directly or indirectly)?
    4. If a class contains Abstract method, the class has to be declared Abstract classRight.
    5. An Abstract class may or may not contain an Abstract methodRight, and an important point to realize. A class need not have abstract methods to be an abstract class, although usually it will.
    6. Abstract method should not have any code implementations, the sub-classes must override it (sub-class must give the code implementations). An abstract method must not have any implementation code code. It's more than a suggestion.
    7. If a sub-class of an Abstract class does not override the Abstract methods of its super-class, than the sub-class should be declared Abstract also.This follows from point 4.
    9. Abstract classes can only be declared with public and default access modifiers.That's the same for abstract and non-abstract classes.

  • Non-abstract methods in a Abstract class

    Abstract Class can contain Non-abstract methods.
    and Abstract Classes are not instantiable as well
    So,
    What is the purpose of Non-abstract methods in a Abstract class.
    since we can't create objects and use it
    so these non-abstract methods are only available to subclasses.
    (if the subclass is not marked as abstract)
    is that the advantage that has.(availability in subclass)
    ??

    For example, the AbstractCollection class (in
    java.util) provides an implementation for many of the
    methods defined in the Collection interface.
    Subclasses only have to implement a few more methods
    to fulfill the Collection contract. Subclasses may
    also choose to override the AbstractCollection
    functionality if - for example - they know how to
    provide an optimized implementation based on
    characteristics of the actual subclass.Another example is the abstract class MouseAdapter that implements MouseListener, MouseWheelListener, MouseMotionListener, and that you can use instead of these interfaces when you want to react to one or two types of events only.
    Quoting the javadocs: "If you implement the MouseListener, MouseMotionListener interface, you have to define all of the methods in it. This abstract class defines null methods for them all, so you can only have to define methods for events you care about."

  • Class-data versus data and methods versus class-methods in OO ABAP

    Hi
    I was going thorugh following OO ABAP code.
    CLASS vessel DEFINITION.
      PUBLIC SECTION.
        METHODS: constructor,
                 drive IMPORTING speed_up TYPE i,
                 get_id RETURNING value(id) TYPE i.
        CLASS-METHODS: start,
                       objects,
                       inheritance,
                       interfaces,
                       events.
      PROTECTED SECTION.
        DATA: speed TYPE i,
              max_speed TYPE i VALUE 100.
      PRIVATE SECTION.
        CLASS-DATA object_count TYPE i.
        DATA id TYPE i.
    ENDCLASS.
    Whats is difference between methods and class-methods ?
    What is the difference between data and class-data ?

    Hi Rajesh,
    There are two types of componenets in a class
    1)Static components
    2) Instance components
    Instance components exist for every instance of the class, while static exist only once for any number of instances of the class.
    Components of the class are methods, attributes, events etc.
    static attributes are represented by CLASS-DATA and instance attributes are represented by DATA.
    static methods hence are done by CLASS-METHODS and can access only static attributes.
    Instance methods are done by METHODS and can access any attribute.
    For eg: supposing that in a class, there is a static attribute. Suppose after one instance is created, we are setting this static attribute value as 10. Now we are creating another instance of the same class. Now when you try to display the value of this attribute, it will be 10.ie. it needs to be initialized once and can be shared between instances.
    Just go through this document..You will get nice info from this.
    http://esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt
    http://esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf
    If you want to go deeper, like object persistence and all, just refer this document.
    http://esnips.com/doc/92be4457-1b6e-4061-92e5-8e4b3a6e3239/Object-Oriented-ABAP.ppt
    Regards,
    SP.

  • About abstract interface and it's method

    Hi !!
    HttpSession session = request.getSession();
    getSession() is the Method of HttpSession and it is the abstract interface..
    right ??
    so how can we call the abstract method and ..
    here... request is the object of HttpServletRequest which is abstract interface so how can we make an Object of this abstract interface...
    I m very confused Please solve my Querry ASAP

    As far as I can tell, these three threads are all the same person asking the same question.
    http://forum.java.sun.com/thread.jspa?threadID=5188609
    http://forum.java.sun.com/thread.jspa?threadID=5188638
    http://forum.java.sun.com/thread.jspa?threadID=5188603
    How obnoxious.

  • Static abstract methods - how can I avoid them?

    Hi everybody,
    just found out that java doesn't allow static abstract methods nor static methods in interfaces. Although I understand the reasons, I can't think of how to change my design to gain the required behavior without the need of static abstract methods.
    Here's what I want to do and how my thoughts lead to static abstract methods:
    ClassA provides access to native code (c-dll, using jna). The path to the dll can be set programmatically. Here's a draft of the class:
    public ClassA {
       private static String path;
       public static void setRealtivePath(String path) {
          //check if path exists and is not null -> get absolute path
          setPath(path);
       public static void setPath(String absolutePath) {
          this.path = path;
      //code to provide access to native lib
    }There is some more classes which provide access to different dlls (therefore the code for accessing the dlls differs from ClassA). Although this works, the setRelativePath method has to be implemented in each class. I thought it would be nice to put this method into a abstract super class that looks like this:
    public abstract class superClass {
       public static void setRelativePath(String path) {
          //check if path exists and is not null -> get absolute path
          setPath(path);
       //force inherting class to implement a static method
       public static abstract void setPath(String absolutePath);
    }thus simplifying ClassA and it's look-a-likes:
    public ClassA {
       private static String path;
       @Override
       public static void setPath(String absolutePath) {
          this.path = path;
      //code to provide access to native lib
    }Since static abstract methods (and overriding static methods) is not allowed, this doesn't work.
    I hope someone can catch my idea ;). Any suggestions how to do this in a nice clean way?
    Thanks in advance,
    Martin
    Edited by: morty2 on Jul 22, 2009 2:57 AM

    First of all, thanks a lot for your answer.
    YoungWinston wrote:
    Actually, you can "override" static methods (in that you can write the same method for both a subclass and a superclass, providing the superclass method isn't final); it's just that it doesn't work polymorphically. The "overriding" method masks the overridden one and the determination of what gets called is entirely down to the declared type.Yes, I know that. There's one problem: Your suggestion means that I simply have to drop the abstract modifier in the super classes setPath method. However, since the super class calls the setPath method (and not the inherting classes!) it will always be the super classes' method being called.
    YoungWinston wrote:
    Why are you so concerned with making everything static? Seems to me that the simplest solution would be to make all the contents instance-based.I want ClassA and it's look-a-likes to be set up properly at application start up, be accessible quite anytime and easily, they don't do anything themselves except for setting the path and calling into the native library which does the math. (Compareable to how you call e.g. Math.cos()). Therefore I don't think that an instance-based solution would be a better approach.
    YoungWinston wrote:
    Furthermore, you could make the class immutable; which might be a good thing - I'm not sure I'd want someone being able to change the pathname after I've >set up a class like this.Thanks for that!
    PS: As mentioned in my first post, I do have a working solution. However, I'm really corious about finding a nicer and cleaner way to do it!
    Martin

  • Newbie question about abstract methods

    hey, I'm working on a web application that I was given and I'm a little confused about
    some of the code in some of the classes. These are some methods in this abstract class. I don't understand
    how this post method works if the method it's calling is declared abstract. Could someone please tell me how this works?
        public final Representation post(Representation entity, Variant variant) throws ResourceException {
            prePostAuthorization(entity);
            if (!authorizeGet()) {
                return doUnauthenticatedGet(variant);
            } else {
                return doAuthenticatedPost(entity, variant);
    protected abstract boolean authorizeGet();Thanks
    Edited by: saru88 on Aug 10, 2010 8:09 PM

    Abstract Methods specify the requirements, but to Implement the functionality later.
    So with abstract methods or classes it is possible to seperate the design from the implementation in a software project.
    Abstract methods are always used together with extended classes, so I am pretty sure that you are using another class.
    Btw: Please post the Code Keyword in these brackets:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

  • Using an abstract method to assign an annotion property

    Hi Guys
    I have an annotation for specifiying that a method requires a transation. It's very simple, and looks like this:
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface TransationRequired
        PersistenceUnitDescriptor persistenceUnitDescriptor();
    }PersistenceUnitDescriptor is a simple enum, and I use Aspect J to weave code around methods annotated with this annotation.
    When I annotate a method like this
    @TransationRequired(persistenceUnitDescriptor = PersistenceUnitDescriptor.XXX)
         public Boolean func(String xx)
             //TODO: Something
         }It works fine, but when I annotate like this :
    @TransationRequired(persistenceUnitDescriptor = getPersistenceUnit())
         public Boolean func(String xx)
             //TODO: Something
         }the apect code is never run.
    getPersistenceUnit() is an abstract method and it is the only thing which is different between the two cases.
    I'm not sure if this is beacuse of the annotation or because the weaving, but I thought I would ask in the annotation categroy just in case.
    Anyone got an idea as to what may be causing this?
    Thanks in advance,
    Vackar

    VackarAfzal wrote:
    OK, Thanks for the tip. Pity that it has to be constant, in my opinion annotations would be a lot more powerful if you could dynamically change attributes. That really would make them more than just an alternative to annoying config files. But perhaps there are some technical details that I'm not aware of which makes this idea seem silly.Annotations were designed as a compile-time construct so the values within need to be compile time constants. At runtime, dynamic proxies are used to construct objects whose methods will return the values in question. You are free to construct your own objects implementing the interface of an annotation type at runtime that return values determined more dynamically.

  • What is the difference between instance variable and class variable?

    i've looked it up on a few pages and i'm still struggling a bit maybe one of you guys could "dumb" it down for me or give and example of how their uses differ.
    thanks a lot

    Instance is variable, class is an object.What? "Instance" doesn't necessarily refer to variables, and although it's true that instances of Class are objects, it's not clear if that's what you meant, or how it's relevant.
    if you declare one instance in a class that instance
    should be sharing within that class only.Sharing with what? Non-static fields are not shared at all. Sharing which instance? What are you talking about?
    if you declare one class object it will share
    anywhere in the current program.Err...you can create global variables in Java, more or less, by making them static fields. If that's what you meant. It's not a very good practice though.
    Static fields, methods, and classes are not necessarily object types. One can have a static primitive field.

  • Override Abstract Methods in NetBeans 6.1

    I just upgraded to NetBeans 6.1 and I can't figure out how to automatically override abstract methods... I I know in older versions you could select a menu option and select abstract methods and NetBeans would put them all in for you...

    I'm sorry... I admit that I only looked quickly for a NetABeans forum and didn't find one... What is it under so I know for the future...

  • How to get a list of callinf mathoda and classes

    I am going to analyse and improve an existing sofware.
    Is there a tool to list methods and classes that are called when an activity on this software is begun?
    I have source doce but I do not have UML for this project
    and rational or other commercial products either.
    best regards
    Mangi

    A profiler?

Maybe you are looking for

  • "You will not be able to publish your resource plan..."

    Hi all, Project Server 2010 with SP1 and Feb '12 CU.  We've started experimenting with resource planning and have found a situation for which I have no explanation.  For some projects, the resource plan functions work just fine.  But we have a few wh

  • Windows 7 64bit and Indesign CS6 AAMEE install fails

    Hello, i have a windows 7 64bit OS and a server 2008 r2 environemnt. i am trying to deploy indesign CS6 via a batch script that works well on a 32bit windows XP and 7 machine but not on the 64 bit 7 and 2008. the script is simply msiexec /i path of M

  • Bug in Illustrator CS6 pattern options palet

    I found out that it was not possible to create a patern swatch with a single copy of 1x1. The preview window kept showing at least a 3x3 version and if you drag the swatch from your swatches pallet it copies 3x3 swatch. Does any one else experienced

  • How to connect R3 to Portal

    Hi all, Can any one tell me how to connect R3 to Portal step by step ? Please... I am new to Portal environment.. Regards, Murugan Arumugam.

  • Ghost email addresses........

    We have an iPhone 4 and its used by a few people and we put in a mail account and check and delete it at times, but with my primary account I go to type a name in the To: box for an email and all kinds of names and addresses come up. Many of those na