Why are we using Abstract class?

Why are we using Abstract class? What is specify use of Abstract class?

The way I understand it....and I may be wrong because
I am very new....is that by making the class abstract
you will add abstract methods to it. Those abstract
methods MUST be defined in in any subclass that
inherits from the abstract class thereby making the
abstract a template for subclasses.
If your animal class is abstract then you would need
an abstract method (Which is just siganture, no body)
of say "numberOfLegs". Then in your dog and cat
classes which extend animal you must define a
"numberOfLegs" method with appropriate code.it isn't mandatory for an abstract class to have abstract methods. if a class does have abstract methods, the class must be abstract. but the following is perfectly legal
public abstract class NoAbstractMethods {
  public void doStuff() {
     // do stuff
}a subclass of an abstract class can also be abstract, and as such need not implement any additional methods

Similar Messages

  • Why use abstract classes?

    Why should I use abstract classes instead of a regular class with empty method bodies? Just better design? Is there some logical or performance based reason?

    Why should I use abstract classes instead of aregular
    class with empty method bodies? Just better design?Is
    there some logical or performance based reason?Because it describes what you're doing.
    If you define a "regular" class with empty message
    bodies, everyone who looks at it will say "WTF is he
    trying to do" or, if they're charitable, "Look,
    Harvey, someone who's trying to make Java look just
    like C++!".
    sigh Maybe answers like THIS are why people keep asking the same question.
    Here's a couple things an abstract class does that a "regular" class with empty methods bodies doesn't:
    1) An abstract class cannot be instantiated.
    2) An abstract class forces it's abstract methods to be implemented.
    If you were to extend a non-abstract class with empty method bodies, you wouldn't have to override the methods... you could just leave them empty. An abstract class forces it.
    There's a lot more reasons... those are a couple obvious ones.

  • What is the significance of Marker interface? Why are we using, even though

    What is the significance of Marker interface? Why are we using, even though it has no method?

    Well, what's the significance of an interface? They can define a set of methods a class may implement but the class could equally well implement these methods without the interface so why having interfaces at all?
    The answer is that the most important aspect of an interface is that it constitutes a type (you can declare variables of it). And it's a type regardless of how many methods it defines, including none. So the reason for having a marker interface is that you're interested solely in the type aspect of interfaces.

  • When to use abstract classes instead of interfaces with extension methods in C#?

    "Abstract class" and "interface" are similar concepts, with interface being the more abstract of the two. One differentiating factor is that abstract classes provide method implementations for derived classes when needed. In C#, however,
    this differentiating factor has been reduced by the recent introduction of extension methods, which enable implementations to be provided for interface methods. Another differentiating factor is that a class can inherit only one abstract class (i.e., there
    is no multiple inheritance), but it can implement multiple interfaces. This makes interfaces less restrictive and more flexible. So, in C#, when should we use abstract classes
    instead of interfaces with extension methods?
    A notable example of the interface + extension method model is LINQ, where query functionality is provided for any type that implements IEnumerable via
    a multitude of extension methods.

    Hi
    Well I believe Interfaces have more uses in software design. You could decouple your component implementing against interfaces so that
    you have more flexibility on changing your code with less risk. Like Inversion of Control patterns where you can use interfaces and then when you decide you can change the concrete implementation that you want to use. Or other uses for interfaces is you could
    use Interceptors using interfaces (Unity
    Interceptor) to do different things where not all of these is feasible or at least as straightforward using abstract classes.
    Regards
    Aram

  • When to use abstract class compared to interface

    hi
    can some one plase advise me when to use abstract class compared to interface?
    Example will be appreciated...

    So an abstract class can carry implementation. This can be used to formulate a rule of thumb as to when to use it over an interface.
    If you have a so called type specialization relationship between the subtypes and the supertype then they're likely to benefit from shared implementation provided by the supertype, so use class (abstract or concrete) extension in this case. Type specialization is when the supertype represents a general concept like Fruit and the subtypes are specialized forms of that like Apple and Banana.
    Another common kind of relationship is called type expansion. In this case the subtypes are unlikely to have any use of implementation provided by the supertype, so use interface implementation. Type expansion is when the supertype represents a specific character the subtypes take on. For example Apple and Bicycle ure unrelated in the type specialization sense but still can share a common character like Comparable. The subtypes have been expanded to include the supertype character, namely the ability to be compared.

  • Most commenly used abstract class in java.

    hi all,
    can anyone please tell me, what is most commonly used abstract class in java. this question was asked in interview.

    I hate interviewers when they ask about specifics of some classes.The fact that you hate it doesn't automatically follow that it's a bad question from the perspective of the person conducting the interviewer. You don't know what the real question is.
    Bad "real" question: Do you know the statistics of Java usage off the top of your head?
    Good "real" question: Do you know that Object isn't Abstract and can you name a few Abstract classes off the cuff?
    Even then Programmers are so literal minded - the real question may be "Talk about what you know a bit."

  • Using Abstract Class Methods

    I just want to know that in how many ways can we use the non abstract methods of the class without extending the class(dont want to implement all abstract methods of the parent class).One way is that if the methods are static then we can use CLASSNAME.METHODNAME().Is their any other way of doing it

    none at all. not a single one. by definition, a non-static method has to be invoked on a particular instance of a class, and an abstract class cannot be instantiated. somebody is probably going to say you can do this:
    abstract class AbstractClass {
      abstract void minceAbout();
      void talkRubbish() {
         // do stuff
    new AbstractClass() {
        void minceAbout() {}
    }.talkRubbish();but that is extending the AbstractClass, no matter what their argument. why d'you ask, anyway? why not implement the methods? sounds like a design smell

  • EJB question: How to use abstract class in writing a session bean?

    I had written an abstract class which implements the session bean as follow:
    public abstract class LoggingSessionBean implements SessionBean {
    protected SessionContext ctx;
    protected abstract Object editRecord(Object obj) throws Exception;
    public LoggingSessionBean()
    super();
    private final String getBeforeUpdateImage(Object obj) throws Exception {
    // implement the details of extracting the backup image ...
    public void setSessionContext(SessionContext ctx)
    this.ctx = ctx;
    private final void writeThisImageToDatabase(String aStr) {
    // connect to database to write the record ...
    public final Object update(final Object obj) {
    try {
    final String aStr = getBeforeUpdateImage(obj);
    writeThisImageToDatabase(aStr);
    editRecord(obj);
    } catch (Exception e) {
    ctx.setRollbackOnly();
    This abstract class is to write the backup image to the database so that other session beans extending it only need to implement the details in editRecord(Object Obj) and they do not need to take care of the operation of making the backup image.
    However, some several questions for me are:
    1. If I write a class ScheduleSessionBean extending the above abstract class and the according 2 interfaces ScheduleSession and ScheduleSessionHome for this session bean (void update(Object obj); defined in ScheduleSession), do I still need to write the interfaces for LoggingSession and LoggingSessionHome?
    2. If I wrote the interface LoggingSession extending EJBObject where it defined the abstract methods "void update(Object obj);" and "void setSessionContext(SessionContext ctx);", that this meant I needed to write the ScheduleSession to implement the Logging Session?
    3. I used OC4J 9.0.4. How can I define the ejb-jar.xml in this case?

    Hi Maggie,
    1. do I still need to write
    the interfaces for LoggingSession and
    LoggingSessionHome?"LoggingSessionBean" can't be a session bean, because it's an abstract class. Therefore there's no point in thinking about the 'home' and 'remote' interfaces.
    2. this
    meant I needed to write the ScheduleSession to
    implement the Logging Session?Again, not really a question worth considering, since "LoggingSessionBean" can't be an EJB.
    3. I used OC4J 9.0.4. How can I define the
    ejb-jar.xml in this case?Same as you define it for any version of OC4J and for any EJB container, for that matter, since the "ejb-jar.xml" file is defined by the EJB specification.
    Let me suggest that you create a "Logging" class as a regular java class, and give your "ScheduleSessionBean" a member that is an instance of the "Logging" class.
    Alternatively, the "ScheduleSessionBean" can extend the "Logging" class and implement the "SessionBean" interface.
    Good Luck,
    Avi.

  • OC4J 9.0.4 migrate to 10.1.3.1 JSP fails using abstract class for ResultSet

    Consider the following JSP code snipet:
    <%
    com.class.SQLDataSource detail = ((com.class.SQLDataSource)request.getAttribute("AcctList"));
    %>
    <%while detail.next()) {detail.getRow();%>
    <tr ...>
    <td ...><%= detail.getString("ACCT") %></td>
    </tr>
    <%}%>
    blah, blah, blah
    class snipet looks like this:
    public abstarct class SQLDataSource extends serializable {
    private ResultSet resultSet = null;
    private PreparesStatement stmt = null;
    public boolean next() throws SQLException {
    boolean result = getResultSet().next();
    return (result);
    public String getString(String columnName) throws SQLExcpetion {
    return getResultSet().getString(columnName);
    public void execute() throws SQLException {
    -- checks stmt
    -- if null generates resultSet from "AcctList.sql" query
    -- if not null, re-executes as-is stmt for a query only
    protected ResultSet getResultSet() throws SQLException {
    execute(); // see above
    return resultSet;
    The problem:
    <%= detail.getString("ACCT") %> generates a NullPointerException
    Yet, if I add debug statements in the SQLDataSouce class, they conclusively show that resultSet has 1902 rows and I can diplay the contents on these rows (on the console, of course, the JSP still generates a NullPointerException).
    If I "hardcode" the creation of a ResultSet in the JSP, the pages displays properly (not the desired method).
    PLEASE TAKE NOTE: This JSP/Class combination works PERFECTLY in OC4J 9.0.4 using JDeveloper 9.0.5.2. When I deploy the **EXACT SAME** JSP/Class combination in JDeveloper/OC4J 10.1.3.1, I receive the NullPointerException.
    So what this feels like to me (and my VERY limited J2EE experinece) is that the abstract class SQLDataSource has somehow lost/closed/dropped/corrupted resultSet when "returning" to the JSP for display of the contents of resultSet. This is, of course, a laymans explanation of the experienced effect of running this code.
    It makes me wonder if under 10.1.3.1 "something more" must be done so that the (abstract) SQLDataSource class can operate in the same way it did under 9.0.4.
    Last note: this NullPointerException happens whether I deploy to standalone OC4J 10.1.3.1 or if I execute my application withing JDevloper's Embedded OC4J instance.
    Any Assistance on this MOST aggrivating problem would be greatly appreciated.
    Others have yet to solve it in similar posts of mine. Hopefully this more definitive description will help YOU be my personal HERO.
    Ed.

    repost to pique interest

  • When should I use abstract classes and when should I use interfaces?

    Can any body tell me in which scenario we use /we go for Interface and which scenario we go for abstract class, because as per my knowledge what ever thing we can do by using Interface that thing can also done through abstract class i mean to say that the
    behavior of the two class.
    And other thing i also want to know that which concept comes first into the programming abstract class or Interface.
    S.K Nayak

    The main differences between an abstract class and an interface:
    Abstract
    An abstract class can contain actual working code (default functionality), and can have either virtual or abstract method.
    An abstract class must be sub-classed and only the sub-classes can be instantiated. Abstract methods must be implemented in the sub-class. Virtual methods may be overridden in the sub-class (although virtual methods typically contain code, you still may
    need/want to override them). A good use for an abstract class is if you want to implement the majority of the functionality that a class will need, but individual sub-classes may need slightly different additional functioality.
    Interface
    An interface only contains the method signatures (method name and parameters), there is no code and it is not a class.
    An interface must be implemented by a class. An interface is not a class and so it cannot be sub-classed. It can only be implemented by a class. When a class implements an interface, it must have code in it for each method in the interface's definition.
    I have a blog post about interfaces:
    http://geek-goddess-bonnie.blogspot.com/2010/06/program-to-interface.html
    (sorry, I have no blog posts specific to abstract classes)
    ~~Bonnie DeWitt [C# MVP]
    http://geek-goddess-bonnie.blogspot.com

  • Why JSP not use new class that I compile again?

    I use bean class with JSP. But When I add some code and compile new bean. JSP not use new been class it use old class.
    I try to set file server.xml and set <DefaultContext reloadable="true"/> and restart Tomcat. But it not work?
    any help please?

    I run JSP in http://localhost:8080/stringbean.jsp
    is context name mean localhost
    in c:\tomcat\work has following directory
    standalone (level1 dir)
    localhost (level2 dir)
    - (level3 dir)
    examples (level3 dir)
    manager (level3 dir)
    tomcat-doc (level3 dir)
    webdav (level3 dir)

  • FILLING an area  without using SHAPE class

    I am trying to implement the fill operation in a drawing application.
    but I am not using the SHAPE class and also I do not know the co-ordinates.
    I have just a very faint idea of implementing it, but am not able to path myself out.
    If any appropriate method strikes to any of being, please pass on that to me kindly.
    Thanks
    Rgds
    Ashwin

    I think doing the fill operation in the paint() method is a very bad idea... how will yoou keep track of the fill action for the next time the paint() wil be called ?
    In fact, you have two solutions:
    1) Keep track in an array (or vectors or array list) of every object you create in your drawing tool. For example:
    Object 1: Rectangle, bounds=[10,5,100,200], color=blue, fill=false
    Object 2: Circle, bounds=[80,45,30,30], color=red, fill=false
    Object 3: Shape, bounds=[10,5,50,60], color=green, fill=true
    In this case, your paint() method only has to loop thru the objects and do the paintings accordingly.
    2) Only keep track of the graphic area. The paint() method first has to recover the previous graphic area, then do the new paint action, and finally backup the graphic area. For example:
       // Call the superclass painting
       super.paintComponent(g);
       // Paint the previous graphic area
       if (oldGraphics != null) g.drawImage(previousGraphics);
       // Paint the new object
       doPaintAction(g);
       // backup the graphic area
       // NOTE: You'll have to find a way to get an image
       // of the graphic area!
       previousGraphics = getGraphicImage(g);Hope this helped,
    Regards

  • Why are videos using space on my phone when there are no videos on my phone?

    When I sync my phone is shows that quite a bit of space is being used by video, but there shouldn't be any videos.  How do I free up that space that is supposedly being used by "video". Thanks.

    ansbrad wrote:
    My phone keeps giving me a notification that my storage is almost full.  It looks like the photos & camera are taking up most of the space, however I only have 99 photos on the phone and photo streaming is turned off, but it still has 3.9KB used space under shared photo stream.  How do I delete this?
    1 gigabyte = 1024 megabytes.  1 megabyte = 1024 kilobytes.
    1 gigabyte = 1048576 kilobytes.
    3.9 KB is 0.38% of a MB, and 0.00037% of a GB
    If your iPhone is a 8GB model, then you are literally worrying about 0.000046% of the device's total space.
    Or, to look at it another way, an 8GB iPhone has 8,388,608 kilobytes of total space.  Are you really concerned about less than 4 KB out of 8.3 Million?
    Or... Let's say that you had $83,886.08 in your bank account.  Would you worry about trying to find the nickel that rolled under the fridge?

  • Why got Nullpointer use getJarPath.class.getResource(".").getPath() ?

    hi,
    I have a question about the following program:
    public class getJarPath {
    public getJarPath() {
    public static void main(String[] arg) {
    System.out.println("java.net.URl->" + getJarPath.class.getResource(
    "getJarPath.class"));
    System.out.println("getPath->" + getJarPath.class.getResource(
    "getJarPath.class").getPath());
    System.out.println("getFile->" + getJarPath.class.getResource(
    "getJarPath.class").getFile());
    System.out.println("getPath with . ->"+getJarPath.class.getResource(".").getPath()); // NullPointer occured
    1. compile getJarPath.java
    D:\>D:\j2sdk1.4.2_04\bin\javac getJarPath.java
    2. run this test program with "java getJarPath"
    D:\>D:\j2sdk1.4.2_04\bin\java getJarPath
    java.net.URl->file:/D:/getJarPath.class
    getPath->/D:/getJarPath.class
    getFile->/D:/getJarPath.class
    getPath with . ->/C:/Program%20Files/exceed.nt/
    3. D:\>D:\j2sdk1.4.2_04\bin\jar cvf getJarPath.jar getJarPath.class
    4. D:\>D:\j2sdk1.4.2_04\bin\java -cp d:\getJarPath.jar getJarPath
    java.net.URl->jar:file:/D:/getJarPath.jar!/getJarPath.class
    getPath->file:/D:/getJarPath.jar!/getJarPath.class
    getFile->file:/D:/getJarPath.jar!/getJarPath.class
    Exception in thread "main" java.lang.NullPointerException
    at getJarPath.main(getJarPath.java:15)
    question:
    in step 2. why the path "." is "/C:/Program%20Files/exceed.nt/" ?
    in step 4. why NullPointerException occured?
    thank a lot
    BR

    Your output seems to indicate that all lines executed successfully, yet you say there's a NullPointerException. Which is it?

  • Droid Turbo why are you using so much data?

    I have yet to get a response from Verizon on why my droid turbo is using so much data.  It is apparent their is some flaw when my phone shows I have used 100 mb so far this billing period and Verizon wireless shows I have use 1.17 gb of data.  I have checked all of my apps to restrict background data and yet it is using data like it is going out of style.  If somebody has a solution please let me know.

    I had the same problem. After checking with other Turbo users on another board it turns out that even when you sign into wifi Turbo was using data. I believe this issue was supposed to be fixed in the last update, but I'm not sure it has been. I called Verizon today to get a refund for the $55 in overage fees I got in my first month with the Turbo. Verizon gave it to me, but in the course of the conversation the tech support guy asked me to turn on airplane mode and he sent me a text, which came through fine. He seemed fairly shocked about that, but still said that "we can't be sure what the source of the problem is, it could be your router." My router works fine and we have no connectivity issues with all the other devices in the house including the other cellphone, and it would have nothing to do with texts coming through. So, that's my experience with this issue, but at least I got a refund for last month's overages.

Maybe you are looking for

  • Error in Transfer posting for movement type 303

    Hi Sir, I am getting following error while doing Transfer posting for movement type 303 thru BAPI_GOODSMVT_CREATE Error in Function: Order    not found or not permitted for Goods Movement. I am passiing all mandatory parameter for it as per BAPI Docu

  • Why I should enter my credit card details if I just want to get free application?

    Starting from last month I am unable to download any free application which contains in app purchase service. Why I should enter my credit card details if I just want to get free application? I am close to go to Android

  • Removing 3D Dock Reflection and/or Changing 2D Dock Background Color

    Has anyone figured out a way to do either (or both) of the following: -remove reflections from the 3D dock -adjust the dark gray color of the 2D dock ? This is the last step I need to make the Dock visually tolerable for me. It would seem that both a

  • Qosmio G10: DVD-R and DVD+RW are CDs when inserted

    When viewing my empty DVD drive via Windows Explorer, it displays "DVD-RAM Drive". When I put a blank DVD-R or DVD+RW (brand new Phillips) into the drive, the drive description immediately changes to "CD Drive", and I only get audio options. I have n

  • Item Selection in contract

    Dear All, As i have done item selection in VA01 ,but i want to call item selection in VA41 screen, but there will be no tab call additional functional in EDIT , so pls help me on this, is there config require for that? Regrads, Pranav