Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

October 30, 2015 Categories: Inheritance. 2 Comments on abstract Keyword

In this article we will look at abstract keyword in Java. We will learn what is the use of abstract keyword and how to use it in Java programs.

 

Uses of abstract keyword

Following are the uses of abstract keyword in Java:

  • Used to create abstract methods
  • Used to create abstract classes

 

Creating abstract methods

Sometimes while creating hierarchies, a method inside a super class might not be suitable to have any kind of implementation. Such methods can be declared as abstract using the abstract keyword. Syntax for creating an abstract method is as follows:

abstract return-type method-name(parameters-list);

As candidate example for abstract method let’s consider the Shape class from our previous article:

In the above example there is really no need to give implementation for the area() method. Area of shape doesn’t mean anything without telling what type of shape it is. So, we can declare area() method as abstract method using the abstract keyword as shown below:

Following are the rules associated with abstract methods:

  • Abstract methods doesn’t contain any body. It contains only method prototype.
  • Abstract methods can only be declared inside abstract classes.

 

Creating abstract classes

A class declared using the abstract keyword is known as an abstract class. The syntax for creating an abstract class is as shown below:

abstract class ClassName

{

//Members of the class

}

Following are the important points that should be remembered about abstract classes:

  • A class which contains at least one abstract method should be declared as abstract class.
  • An abstract class can contain both concrete (non-abstract) and abstract methods.
  • Abstract classes cannot be instantiated i.e, objects cannot be created for abstract classes.
  • All abstract methods in a base class must be overridden in the derived class.

Let’s look at an example which demonstrates the use of abstract classes:

Output of above Java code is:

Area of circle
Area of rectangle

Now, let’s look at another example:

The above Java code will not compile as the sub classes Cirlce and Rectangle doesn’t override the abstract peri() method.

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

2 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

any abstract class example help of subject no and detail

abstract class example is available (class Shape) on this page. An abstract class is just used to separate out the complex details of the concrete child classes.

Leave a Reply

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

Scroll Up