Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Inheritance. 2 Comments on abstract Keyword
0
(0)

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:


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


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

 

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

class Shape
{
	void area()
	{
		System.out.println("Area of shape");
	}
}

 

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:

abstract class Shape
{
	abstract void area();
}

 

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:

abstract class Shape
{
	abstract void area();
}

class Circle extends Shape
{
	void area()
	{
		System.out.println("Area of circle");
	}
}

class Rectangle extends Shape
{
	void area()
	{
		System.out.println("Area of rectangle");
	}
}

public class Driver
{
	public static void main(String[] args) 
	{
		Shape s;
		s = new Circle();
		s.area();
		s = new Rectangle();
		s.area();
	}
}

 

Output of above Java code is:

 

Area of circle
Area of rectangle

 

Now, let’s look at another example:

abstract class Shape
{
	abstract void area();
	abstract void peri();
}

class Circle extends Shape
{
	void area()
	{
		System.out.println("Area of circle");
	}
}

class Rectangle extends Shape
{
	void area()
	{
		System.out.println("Area of rectangle");
	}
}

public class Driver
{
	public static void main(String[] args) 
	{
		Shape s;
		s = new Circle();
		s.area();
		s = new Rectangle();
		s.area();
	}
}

 

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

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

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.

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.

any abstract class example help of subject no and detail

Leave a Reply

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