Abstraction is a foundational concept in programming, involving the process of distilling an object’s essential attributes and methods while concealing its internal complexities. This concept is effectively applied through the use of classes, which extend the principles of encapsulation. An illustrative example of this concept can be observed in Java, where it serves as an abstraction layer streamlining interactions with lower-level binary languages. These classes serve as custom data types, allowing for partial implementation while establishing guidelines for subclasses to complete the implementation. These classes, marked by the “abstract” keyword, can encompass a range of elements similar to regular classes, including constructors, blocks, variables, and methods. Moreover, they can incorporate abstract methods, enhancing their versatility and usefulness in object-oriented programming.
While exploring the concept of Java abstract class variables, it’s important to also delve into the broader topic of BiPredicate in Java, which plays a significant role in defining and manipulating abstract class variables within the language.
abstract class ClassName{
//members of abstract class
}
Through an abstract class, you can establish guidelines for subclasses to follow when implementing functionality by declaring abstract methods. This method is essentially a method declared without any specific implementation; it lacks code within its body and is terminated with a semicolon.
access_specifier abstarct returntype methodname (type1 pmtr1, type2 pmtr2, type3 pmtr3..);
Example:
abstract void calculate(int a, int b);
Numerous classes possess the capacity to make use of and extend an abstract class for their benefit. In order to harness its capabilities effectively, the implementing class must engage in inheritance by extending the abstract class. This inheritance process enables the implementing class to inherit both the structural framework and the blueprint outlined by the abstract class. Consequently, this ensures strict adherence to the specified guidelines and facilitates the utilization of the class’s functionalities by the implementing class.
class ImplementationClass extends Abstractclass {
// implementation for abstract class abstract methods
}
When a subclass extends an abstract class, it assumes a mandatory responsibility to override and furnish concrete implementations for all the abstract methods specified in its parent class. In cases where the subclass fails to execute these implementations, it must also be classified as an abstract class in its own regard. This requirement guarantees that the subclass either complies with the contractual obligations set by the abstract class or continues the abstraction paradigm by transforming into an abstract class in its own right.
abstract class A {
abstract int add(int a, int b);
abstract int subtract(int a, int b);
}
abstract class B { // abstract because does not implement subtract method
int add(int a, int b) {
return a + b;
}
}
class C extends A {
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
}
In Java, there exist two approaches to attain abstraction:
When an abstract class consists solely of abstract methods, it attains a state of absolute abstraction, reaching a 100% level of abstraction. Conversely, if this class incorporates concrete methods in addition to its abstract counterparts, it does not achieve complete abstraction. It is crucial to understand that the class has the capacity to encompass a combination of both methods. Nevertheless, even the presence of a single abstract method within a class necessitates it to be designated as abstract. Moreover, a class composed entirely of concrete methods can still be designated as abstract, often employed when there is no intention to create instances of that specific class.
abstract class A {
abstract void callme();
void callmetoo() {// concrete methods are still allowed in abstract classes
System.out.println("This is a concrete method.");
}
}
class B extends A {
void callme() {
System.out.println("B's implementation of callme.");
}
}
class AbstractDemo {
public static void main(String args[]) {
B b = new B();
b.callme();
b.callmetoo();
}
}
Output:
B’s implementation of callme.
This is a concrete method.
Abstract classes cannot be directly instantiated, but it is possible to declare a reference variable for them.
//Using abstract methods and classes.
abstract class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class AbstractAreas {
public static void main(String args[]) {
// Figure f = new Figure(10, 10); // illegal now
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref; // this is OK, no object is created; creating reference variable of abstract class
figref = r;// abstract class (super class) reference is assigned with subclass object
System.out.println("Area is " + figref.area());// since area() method is overridden area() method in
figref = t;
System.out.println("Area is " + figref.area());// area() method in Triangle is called
}
}
Java abstract class variables serve as an important tool in structuring robust and sustainable Java code. They offer a crucial framework for concrete classes and enhance code clarity while supporting code organization. Despite certain constraints, their capacity to facilitate code reuse and model theoretical situations makes them integral to Java programming.