Abstract
Class in java
Definition:
- Java Abstract classes are used to declare common characteristics of subclasses.
- An abstract class cannot be instantiated.
- It can only be used as a superclass for other classes that extend the abstract class. Abstract classes are declared with the abstract keyword.
- Abstract classes are used to provide a template or design for concrete subclasses down the inheritance tree.
- Like any other class, an abstract class can contain fields that describe the characteristics and methods that describe the actions that a class can perform.
- An abstract class can include methods that contain no implementation. These are called abstract methods.
- The abstract method declaration must then end with a semicolon rather than a block. If a class has any abstract methods, whether declared or inherited, the entire class must be declared abstract.
- Abstract methods are used to provide a template for the classes that inherit the abstract methods.
- Abstract classes cannot be instantiated; they must be subclassed, and actual implementations must be provided for the abstract methods.
- Any implementation specified can, of course, be overridden by additional subclasses. An object must have an implementation for all of its methods. You need to create a subclass that provides an implementation for the abstract method.
A class abstract Vehicle
might be specified as abstract to represent the general abstraction of a
vehicle, as creating instances of the class would not be meaningful.
abstract class Vehicle {
int numofGears;
String color;
abstract boolean hasDiskBrake();
abstract int getNoofGears();
}
Example of a shape class as
an abstract class
abstract class Shape {
public String color;
public Shape() {
}
public void setColor(String c) {
color = c;
}
public String getColor() {
return color;
}
abstract public double area();
}
We can also implement the
generic shapes class as an abstract class so that we can draw lines, circles,
triangles etc. All shapes have some common fields and methods, but each can, of
course, add more fields and methods. The abstract class guarantees that each
shape will have the same set of basic properties. We declare this class
abstract because there is no such thing as a generic shape. There can only be
concrete shapes such as squares, circles, triangles etc.
public class Point extends
Shape {
static int x, y;
public Point() {
x = 0;
y = 0;
}
public double area() {
return 0;
}
public double perimeter() {
return 0;
}
public static void print() {
System.out.println("point: " + x +
"," + y);
}
public static void main(String args[]) {
Point p = new Point();
p.print();
}
}
Output
point: 0, 0
- Notice that, in order to create a Point object, its class cannot be abstract.
- This means that all of the abstract methods of the Shape class must be implemented by the Point class.
- The subclass must define an implementation for every abstract method of the abstract superclass, or the subclass itself will also be abstract.
- Similarly other shape objects can be created using the generic Shape Abstract class.
Disadvantage
A big Disadvantage of using
abstract classes is not able to use multiple inheritance. In the sense, when a
class extends an abstract class, it can’t extend any other class.
No comments:
Post a Comment