Java Abstract

Java Abstract


Abstraction In Java
  • Now we will understand what is Abstraction, and why it is used.
  • Abstraction is used to show the essential features without showing the background details.
  • We can achieve Abstraction in java by using the following two methods -
    • Abstract class - Using this we can achieve abstraction from 0% to 100%.
    • Interface - Using this way we can achieve 100% abstraction in java.
An essential element of object-oriented programming is abstraction. Humans manage complexity through abstraction. 
  • For example, people do not think of a car as a set of tens of thousands of individual parts. They think of it as a well-defined object with its own unique behavior.
  • Abstraction allows people to use a car to drive to the grocery store without being overwhelmed by the complexity of the parts that form the car. They can ignore the details of how the engine, transmission, and braking systems work. Instead, they are free to utilize the object as a whole.
Abstract class, and Abstract Methods
  • A class that contains one, or more abstract methods must declare with abstract keyword called abstract class.
  • A method that have no body in class in which it is declared must declare with abstract keyword, called abstract method.
  • We can not create object of abstact class.
  • An abstract class can contain data members, constructor, abstract method.
  • A abstract class is opposite to final class.
  • Child class must define all abstract method of 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.
Syntax -
abstract type name(parameter-list); 
As you can see, no method body is present.
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 a 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 .