Hey Guys, I am going to explain you a very basic and an important oops concept - Abstraction.
Abstraction in simple way or Abstraction in easy way can be said as Hiding . I have explained the whole abstraction concept with the help of abstract classes and abstraction examples.

Abstract classes -

What are the Abstract classes meant for?

The answer to my question is abstract classes are meant for 'abstracting' means if some classes are having common behavior, instead of writing every time the same thing in each class, write that in one class and ask the other classes to use it (by making the classes as sub classes to the abstract class). this is nothing but inheritance.

In simple words I would say - A class must be declared abstract when it has one or more abstract methods. So a class having no method can be declared as abstract or not ?? Think Think about it

or simply try in eclipse.

A method is declared abstract when it has a method heading, but no body – which means that an abstract method has no implementation code inside curly braces like normal methods do.

A non-abstract class is called a concrete class.

Now, the question arises - When to use abstract methods in java?

I would like to explain this by illustrating an example here -

/* the GeometricalFigure class must be declared as abstract

   because it contains an abstract method  */

public abstract class GeometricalFigure


        /* because this is an abstract method so the

       body will be blank  */
        public abstract float getArea();

public class Circle extends GeometricalFigure
        private float radius;
        public float getArea()
                return (3.14 * (radius * 2));

public class Rectangle extends GeometricalFigure
        private float length, width;
public float getArea(GeometricalFigure other)
                return length * width;

But one question you all would be thinking is as to why did I declare the getArea method to be abstract in the GeometricalFigure class? Well, what does the getArea method do? It returns the area of a specific shape. But, because the GeometricalFigure class isn’t a specific shape (like a Circle or a Rectangle), there’s really no definition we can give the getArea method inside the GeometricalFigure class. That’s why we declare the method and the GeometricalFigure class to be abstract. Any classes that derive from the GeometricalFigure class basically has 2 options: 1. The derived class must provide a definition for the getArea method OR 2. The derived class must be declared abstract itself.

In a nutshell, An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
With abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.

Important points you all need to make a note of -

  • An abstract class can also have constructors and instance variables as well. It can have static or final instance variables as well.
  • An abstract class can also have constructors and instance variables as well. It can have static or final instance variables as well.

I would want like to give  an illustration on the concept of abstract classes and methods with the help of a basic self explanatory example ---->

You want to use an AbstractClass when although your sub-types have to implement some specific logic, you still have logic common to all sub-types like in this case:

public abstract class GeometricalShape
     private String name;
     private int  noOfEdges;
     protected GeometricalShape(name,noOfEdges)

**abstract methods
     public abstract GetArea();
      public abstract GetPerimeter();

**Concrete methods
      public GetName()
return name;
public int GetNoOfEdges()
return noOfEdges;


In this example each geometric shape will have to implement specific logic regarding Area and Perimeter like before, but now all sub-types will share common methods for retrieving the name and no. of edges of the geometrical, which would be redundant to be defined in each and every sub-class.

Post a Comment