Home > Programming, QA, Test and Automation Stuffs > Abstract Methods and Classes

Abstract Methods and Classes

Hello guys,

I will continue with more technical posts today… 🙂

What is ? 

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void moveTo(double deltaX, double deltaY);

If a class includes abstract methods, then the class itself must be declared abstract, as in:

public abstract class GraphicObject {
   // declare fields
   // declare nonabstract methods
   abstract void draw();
}

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

An Abstract Class Example

In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for example: position, orientation, line color, fill color) and behaviors (for example: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects (for example: position, fill color, and moveTo). Others require different implementations (for example, resize or draw). All GraphicObjects must be able to draw or resize themselves; they just differ in how they do it. This is a perfect situation for an abstract superclass. You can take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object (for example, GraphicObject) as shown in the following figure.

Classes Rectangle, Line, Bezier, and Circle Inherit from GraphicObject  

Classes Rectangle, Line, Bezier, and Circle Inherit from GraphicObject

First, you declare an abstract class, GraphicObject, to provide member variables and methods that are wholly shared by all subclasses, such as the current position and the moveTo method. GraphicObject also declares abstract methods for methods, such as draw or resize, that need to be implemented by all subclasses but must be implemented in different ways. The GraphicObject class can look something like this:

abstract class GraphicObject {
    int x, y;
    ...
    void moveTo(int newX, int newY) {
        ...
    }
    abstract void draw();
    abstract void resize();
}

Each nonabstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw and resize methods:

class Circle extends GraphicObject {
    void draw() {
        ...
    }
    void resize() {
        ...
    }
}
class Rectangle extends GraphicObject {
    void draw() {
        ...
    }
    void resize() {
        ...
    }
}

Class Members

An abstract class may have static fields and static methods. You can use these static members with a class reference (for example,AbstractClass.staticMethod()) as you would with any other class.

When ?

Abstract Classes is to create templates for future classes and offer the added benefit of letting you define functionality that your child classes can utilize later. You can’t provide an implementation for an Interface.

Why ?

Abstract Classes are a good fit if you want to provide implementation details to your children but don’t want to allow an instance of your class to be directly instantiated (which allows you to partially define a class). If you want to simply define a contract for Objects to follow, then use an Interface.

 
Abstract methods are useful ?

Abstract methods are useful in the same way that defining methods in an Interface is useful. It’s a way for the designer of the Abstract class to say “any child of mine MUST implement this method”.

 

Any question or comment, just write below ! Thank you again ! 

Bye 🙂

 

Font: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

http://stackoverflow.com/questions/3344816/when-and-why-to-use-abstract-classes-methods

Advertisements
  1. Joe
    July 4, 2014 at 9:15 pm

    Good article! This not only comes in handy when building a framework (i.e. my_test extends base_test to make use of methods that all tests can access or helper methods that log in or register a user as a step in another test) but when digging through the code you are examining.

  2. zonutszonuts
    July 6, 2014 at 6:13 pm

    This approach may be what I need to solve an automated regression testing problem I’ve been wrestling with for a while – thank you!

    • July 6, 2014 at 10:19 pm

      What is the problem ? I can use as example here on blog !

  3. zonutszonuts
    July 6, 2014 at 6:15 pm

    This approach may be able to help me resolve a problem I’ve been wrestling with in an automated regression test library I’ve been building – thank you!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: