what is abstract class in java?

Abstract class in java is a conditioned class that we cannot use to create an object. To access it you need to inherit from another class. Abstraction 0Process of hiding certain details and showing required information to the user. The class that contains an abstract method or non-abstract method, such a class known as an Abstract class. A class made abstract by putting the keyword abstract in front of the class keyword in the first line of a class definition.

Example:

abstract class shape 
{
  abstract double area();//abstract method
  abstract double circumference(); 
} 

Abstract classes are the same as normal classes with fields and methods. But you cannot create objects of abstract classes by using a new operator. However, you can declare a variable. Such a variable can used to refer to an instance of any of the subclasses of the abstract superclass. If you want to represent an object of the abstract superclass Shape using the following statement. 

Shape a =new Shape(9,6);

It shows a compile-time error.
Abstract classes are useful when you require to create a generic type that used as a superclass for two subclasses. But the superclass itself does not represent an actual object.

Here are some points to remember for the java abstract class:

  • An abstract class must declare with an abstract keyword.
  • It can have abstract and non-abstract methods.
  • An abstract cannot be instantiated.
  • It can have constructors and static methods also.
  • It can have final methods with will force the subclass not to change the body of the method.
  • The abstract method cannot be private. Since the private methods cannot inherit and therefore we cannot redefine the classes.
  • The abstract class has no use, no purpose unless it extends.

The following program demonstrates the use of abstract class.

abstract class Shape
{
 abstract void area();
abstract  void circumference();
}
class Rectangle extends Shape
{
 private double length,breadth;
 Rectangle (double x, double y) 
 {
 length=x; breadth=y;
}
public void area()
{System.out.printIn("Area of Rectangle is = " + (length*breadth))};
public void circumference(){System.out.printIn("Circumference of Rectangle is =" +2*(length+breadth));}
}
class Circle extends Shape 
{
 private double radius;
Circle(double r)
{
radius = x;
}
public void area()
{
 System.out.printIn("Area of Circle is ="+(Math.PI*radius));
}
public void circumference()
{
 System.out.printIn("Circumference of Circle is=" + 2*Math.PI*radius);
}
} 
class AbstractsDemo
{
  public static void main(String[] args)
{
 Shape a;// shape class reference variable
 rectangle r = new Rectangle(10,20);
 a = r;// Assign Rectangle reference to shape reference
 a.area();
 a.circumference();
 Circle c = new Circle(5);
 a = c;// Assign circle reference to shape reference
 a.area() ;
 a.circumference();
}
}

Output:

Area of Rectangle is = 200.0
Circumference of Rectangle is = 60.0
Area of Circle is = 78.53981633974483
Circumference of Circle is = 31.41592653589793

Recommended Posts:

Difference between the Java and JavaScript

What is JavaScript?

How are java objects stored in memory?

Compilation and Execution of a java program

References:

www.javatpoint.com

learn programming in java/ Anurag Gupta /book

Leave a Reply

Your email address will not be published. Required fields are marked *