Abstract methods
Abstract methods is a method only with its declaration, without implementation details. In C++, these are known as "pure virtual functions". In JAVA, to specify an abstract method, "abstract" keyword is added to the method signature. Declaration of an abstract class always follows by the semicolon, not with the pair of curly brackets as in non-abstract methods.
Examples for abstract methods :
//abstract method
public abstract void abstractMethod() ;
//abstract method with parameters
public abstract int abstractMethodWithParam(int a, String s);
Abstract class
Partially implemented classes are known as Abstract classes.These classes may contain abstract methods and non-abstract methods. (Hence implementation is not complete.)
Features of an abstract class
- If a class has any abstract methods, it should be declared as an abstract class. (However abstract classes can be declared without any abstract methods )
- Class variables, constructors and non - abstract methods (methods with implementation details) may included in the abstract class.
- Can't create instances from an abstract class. (can't be instantiated.) but sub classes can be created by extending an abstract class.
- "extend" key word is used to create sub classes from an abstract class.
- All abstract methods in an abstract class should implement in the sub class. Otherwise it also should be declared as an abstract class.(Because if all abstract methods are not implemented, some abstract method remain in the sub class. Hence it is also an abstract class!)
UML representation of abstract methods and abstract classes
In UML diagrams, abstract classes and abstract methods are represented in italic letters.
Example for abstract class :
Here is an simple example for an abstract class.
//Abstract class
//This class has one abstract method and one non-abstract method.
abstract class AbstractClass {
//abstract method
public abstract void abstractMethod();
//non-abstract method
public void normalMethod(){
System.out.println("Inside the non-abstract (normal) method.");
}
}
Following class is created by extending above abstract class. This class should implements all abstract method in the abstract class. Additionally it may contain its own variables, constructors and methods. As usual this class inherit all variables and methods from the parent class (which is the abstract class)
//Class extends the abstract class.
class MyClass extends AbstractClass {
//overriding the abstract method
@Override
public void abstractMethod() {
System.out.println("Inside the abstract method implementation.");
}
//method belongs to the MyClass class.
public void myMethod() {
System.out.println("Inside the sub class method.");
}
}
Following class is used to run the programme.
public class AbstractExample {
public static void main(String[] args) {
//instance of the MyClass
MyClass obj = new MyClass();
//calling non-abstract method of the abstract class
System.out.println("\tCalling non-abstract method =>");
obj.normalMethod();
//calling implementation of the abstract method
System.out.println("\tCalling abstract method implementation =>");
obj.abstractMethod();
//calling method belongs to the sub class (MyClass)
System.out.println("\tCalling method belongs to the sub class =>");
obj.myMethod();
}
}
Output of the programme :
Calling non-abstract method =>
Inside the non-abstract (normal) method.
Calling abstract method implementation =>
Inside the abstract method implementation.
Calling method belongs to the sub class =>
Inside the sub class method.
- Suppose that a class is reused in several occasions. It has methods common to the all occasions along side with methods specific to the each occurrence. In this case, we can declare an abstract class which contains common methods and derive sub classes from it with specific methods. So the sub class has specific methods defined within itself and common methods inherited from the parent abstract class.
- If a class is partially implemented and willing to complete implementation in future, we can declare it as an abstract class to prevent instantiation which may be problematic.
Download source file:
No comments:
Post a Comment