Home » Java programming language

Abstract Classes in Java with Example

Learn: - In java programming what is the role of abstract class and how to implement it? This article contains the brief description about Abstract Classes with examples.
Submitted by Amit Shukla, on June 11, 2017

Use of abstract class

In programming there are some condition occurs in which user want to define a super class that declare the structure of the given abstraction without providing implementation of method. In that case the role of abstract class comes. Using this class one can create a super class that only define a generalized form that will be shared by all of his subclass, leaving it to each subclass to fill its detail.

According to dictionary abstraction is quality of dealing with ideas rather than events.

Similarly in object oriented programming, abstraction is process of hiding the implementation of any function from the user. Only the main features of functions are provided to the user. In other words user knows “what the object does” but don’t know “how it works”.

How to implement Abstract class?

An abstract class is declared by using abstract keyword. These classes cannot be instantiated, but they can be extended into sub classes or derived classes.

An abstract class cannot be directly instantiated using new operator, because an abstract class is not define.

Abstract classes are those classes which contain at least one abstract method. It means if any class contains abstract function then it should be declared as abstract class. That is an abstract class can contains both abstract and non abstract methods.

Properties of Abstract class

  1. Abstract class contains abstract methods.
  2. Abstract class cannot be instantiated.
  3. Abstract class can contain mixture of abstract and non abstract methods.
  4. To use abstract class one has to inherit it from another class.
  5. If program contains abstract method than it must be implements all the abstract method of abstract class.

Consider the program:

import java.util.*;
abstract class Vehical
{
	abstract void get();
	abstract void show();
}

class Car extends Vehical
{
	Scanner sc=new Scanner(System.in);
	private long cost;
	private String name;

	void get()
	{
		System.out.print("Enter the name of car : ");
		name=sc.nextLine();
		System.out.print("Enter the cost of car : ");
		cost=sc.nextLong();
	}

	void show()
	{
		System.out.println("Name of car is : "+name);
		System.out.println("Cost vo car is : "+cost);
	}
}

class Bike extends Vehical
{
	Scanner sc=new Scanner(System.in);
	private long cost;
	private String name;

	void get()
	{
		System.out.print("Enter the name of Bike : ");
		name=sc.nextLine();
		System.out.print("Enter the cost of Bike : ");
		cost=sc.nextLong();
	}

	void show()
	{
		System.out.println("Name of bike is : "+name);
		System.out.println("Cost of bike is : "+cost);
	}

}

class ExAbstract
{
	public static void main(String arg[])
	{
		Vehical V;
		Car C=new Car();
		V=C;
		V.get();
		V.show();
		Bike B=new Bike();
		V=B;
		V.get();
		V.show();

	}
}

Output

First Run:
Enter the name of car : Swift
Enter the cost of car : 500000
Name of car is : Swift
Cost vo car is : 500000
Enter the name of Bike : Pulser
Enter the cost of Bike : 75000
Name of bike is : Pulser
Cost of bike is : 75000


Second Run:
Enter the name of car : Hondacity
Enter the cost of car : 1000000
Name of car is : Hondacity
Cost vo car is : 1000000
Enter the name of Bike : Starcity
Enter the cost of Bike : 65000
Name of bike is : Starcity
Cost of bike is : 65000


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.