BANGALORE, INDIA: In linguistics abstraction means a concept or an idea. It is a way of generalizing a thought process that can be later applied to wider applications.
In computer science, abstraction helps the program designers to segregate the concepts from its instances of implementation. Abstraction is an important paradigm of the object-oriented programming which let us focus on the features provided by a system rather than its underlying implementation details.
While developing a system using an object oriented programming languages - abstraction focuses on writing the classes or interfaces those identifying and describing the type of functionality provided by that system. This helps the developers to further write the applications based on this abstraction rather than on its implementation details.
The different OOPS languages inherent abstraction by means of incorporated keywords and declarative statements such as — virtual in C++ or abstract in Java
Usage — How to use abstract in Java
In Java ‘abstract’ keyword is used to write a class that declares the generic structure which must later be implemented by its subclasses. The abstract keyword can be used in different ways for writing java classes to implement abstraction. We can have:
1.An abstract class with one or more abstract method(s)
2.An abstract class without any abstract method
1.An abstract class with one or more abstract method(s)
Usually designing any system, there are requirements to write a class declaring the generalized form of a functionality that must be implemented by all of its sub-classes. The solution here is to define an abstraction - in the form of an abstract class. The functionality which must be implemented by subclasses should be defined in the form of abstract methods in this abstract class.
Syntax details
We can have one or more method(s) declared as abstract in a class. Subsequently, this class having abstract methods must be declared as abstract itself.
abstract method is a method which may or may not have implementation in base class but it is compulsory to have an implementation in concrete derived classes.
abstract class is the one which may have abstract methods defined into it. An abstract class only represents an abstraction - which can not be instantiated.
A method can be declared as abstract by writing abstract keyword as
Abstract method with no implementation in base class
// Case 1. abstract method with no implementation
abstract return-type method-name (parameters);
Abstract method with implementation in the base class
An abstract method may have definition in the base class if required. But, yes it is still compulsory to provide the implementation of this method into the derived classes.
// Case 2. abstract method with implementation
abstract return-type method-name (parameters) {
…………
}
Hence, the complete class may look like as:
abstract class MyClass {
// Case 1. abstract methods with no definition
abstract return-type method-name (parameters);
…………….
// Case 2. abstract methods with definition
abstract return-type method-name (parameters) {
…………
}
// other method — non-abstract methods
return-type method-name (parameters) {
…………
}
…………….
}
2.Abstract class without any abstract method
It is not compulsory to have an abstract method to make a class an abstract class. We can declare 0class as abstract even if it does not have any abstract method. At times, we have a class with all its methods having implementation. Still we do not want the class to be instantiated. In such case we can easily declare the class as an abstract class. Declaring the class as abstract restricts the creation of the objects of that type
Syntax — Simply use the abstract keyword before class keyword as:
abstract class MyClass {
// all non-abstract methods
…………….
}
Here, MyClass is an abstract class. That means,
“A class declared as abstract can not be instantiated i.e. we can not create an object of class declared as an abstract class”
Compiler will give an error if we try to create an object of an abstract class. Yes, we can declare a variable of abstract class type but we can not cerate an object using ‘new’. The following code will generate a compiler error
………..
MyClass obj1; // Ok code
MyClass obj2 = new MyClass (); // Error code!!
……….
(The author is lecturer at Amity University)