Abstraction in java
Abstraction can be defined as the process of hiding the unwanted details and exposing only the essential features of a particular object or concept. The concept of abstraction is used by classes and lists of attributes are defined in them like cost, size and weight, and methods that operate on their attributes. Abstraction is also achieved through composition.
For example, a class Car would be made up of an Engine, Gearbox, Steering objects, and many more components. To build the Car class, one does not need to know how the different components work internally, but only how to interface with them, i.e., send messages to them, receive messages from them, and perhaps make the different objects composing the class interact with each other.
What is abstract class in Java
An abstract class is something which is incomplete and you can not create instance of abstract class. If you want to use it you need to make it complete or concrete by extending it. A class is called concrete if it does not contain any abstract method and implements all abstract method inherited from abstract class or interface it has implemented or extended. By the way Java has concept of abstract classes, abstract method but a variable can not be abstract in Java.
Ads by Google


Abstract Class Example in Java
Example:
abstract class Shape{
abstract void draw();
}
class Rectangle extends Shape{
void draw(){
System.out.println("Drawing Rectangle");
}
}
class Traingle extends Shape{
void draw(){
System.out.println("Drawing Traingle");
}
}
class AbstractDemo{
public static void main(String args[]){
Shape s1=new Rectangle();
s1.draw();
s1=new Traingle();
s1.draw();
}
}
Output
Drawing Rectangle
Drawing Traingle.
Abstract Method
Abstract methods are those which need to be implemented in subclass / child class. Abstract methods are only defined in superclass / parent class but with no body.
Syntax of Abstract Method
abstract class clsName
{
// Variable declaration

// Constructor

// Method
abstract rtype mthName(params);
}
clsName is a valid identifier in java. It is a class name.
abstract is a keyword to define that method an abstract method.
rtype is return type of a method.
mthName is a method name and valid java identifier.
Example of an Abstract Method
Example
abstract class Shape
{
public static float pi = 3.142;
protected float height;
protected float width;

abstract float area();
}

class Square extends Shape
{
Square(float h, float w)
{
height = h;
width = w;
}

float area()
{
return height * width;
}

}

class Rectangle extends Shape
{
Rectangle(float h, float w)
{
height = h;
width = w;
}

float area()
{
return height * width;
}

}

class Circle extends Shape
{
float radius;

Circle(float r)
{
radius = r;
}

float area()
{
return Shape.pi * radius *radius;
}

}

class AbstractMethodDemo
{
public static void main(String args[])
{
Square sObj = new Square(5,5);
Rectangle rObj = new Rectangle(5,7);
Circle cObj = new Circle(2);

System.out.println("Area of square : " + sObj.area());
System.out.println("Area of rectangle : " + rObj.area());
System.out.println("Area of circle : " + cObj.area());
}
}
Output
Area of square : 25
Area of rectangle : 35
Area of circle : 12.57
Previous - Java Polymorphism                                                  Next - Java Encapsulation