Java abstract Keyword

Java Abstract Keyword


Abstract Keyword In Java
An abstract method is created by specifying the abstract type modifier. An abstract method contains nobody and therefore, is not implemented by the superclass.
In other words, we can say abstract keyword works as a  non-access modifier.
We can make class, and method abstracts by putting abstract keywords before them -
  • A class that contains one, or more abstract methods must declare with an abstract keyword called an abstract class.
  • A method that has nobody in class in which it is declared must declare with an abstract keyword, called the abstract method.
  • We can not create objects of an abstract class.
  • An abstract class can contain data members, constructors, abstract methods.
  • An abstract class is opposite to a final class.
  • Child class must define all abstract methods of the super class.
  • If you inherit from an abstract class and you want to make objects of the new type, you must provide method definitions for all the abstract methods in the base class. If you don’t (and you may choose not to), then the derived class is also abstract, and the compiler will force you to qualify that class with the abstract keyword. 
  • It’s possible to make a class abstract without including any abstract methods. This is useful when you’ve got a class in which it doesn’t make sense to have any abstract methods, and yet you want to prevent any instances of that class.
Example - 
// A Simple demonstration of abstract.
abstract class A {
abstract void callme();
// concrete methods are still allowed in abstract classes
void callmetoo() {
System.out.println("This is a concrete method.");
}
}
class B extends A {
void callme() {
System.out.println("B's implementation of callme.");
}
}
class Main {
public static void main(String args[]) {
B b = new B();
b.callme();
b.callmetoo();
}
}
 
Notice that no objects of class A are declared in the program. As mentioned, it is not possible to instantiate an abstract class. Another thing is that class A implements another method called callmetoo( ). This is perfectly acceptable.
Example 2 - 
// Using abstract methods and classes.
abstract class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class AbstractAreas {
public static void main(String args[]) {
// Figure f = new Figure(10, 10); // illegal now
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref; // this is OK, no object is created
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
}
}
As the comment inside main( ) indicates, it is no longer possible to declare objects of type Figure, since it is now abstract. And, all subclasses of Figure must override area( ).To prove this to yourself, try creating a subclass that does not override area( ). a compile-time error will occur.