Java Abstract class
What is a Java Abstract class?
Abstract class represents common pattern or skeleton of similar classes which will extend an abstract class and implement all abstract methods of the abstract class.
An abstract method is a method that is declared but does not have an implementation (without braces, and followed by a semicolon).
You should use abstract methods to specify what methods (behavior) subclasses MUST have but it is NOT SAME for all subclasses (If you have one same method implementation for all subclasses, then you should create a normal, non-abstract method in the parent class WITH implementation, and all subclasses will inherit this method as is).
Class that extends an abstract class must implement all abstract methods, or be an abstract class.
It may contain variables, implemented methods and methods declarations – abstract methods.
Abstract class CANNOT be instantiated (you can create an object of the class that does not have ANY abstract method).
Abstract class CAN extend a class and CAN implement a Java interface.
Java Abstract class example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
/**
* Java abstract class example
* Abstract class represents skeleton of the classes
* which will extend this abstract class
*/
/**
* Vehicle abstract class is a general class of all vehicles
* It contains methods and attributes common to all vehicles
* but you can't create an instance of the Vehicle class
* Vehicle class is used as a basic structure for all specific vehicles (car,bus...)
*/
abstract class Vehicle {
//subclasses will inherit an attribute
int maxSpeed;
//subclasses must implement this method
//(otherwise they have to be declared as abstract classes)
abstract void showMaxSpeed();
//subclass will have this method (through inheritance) as is (default implementation)
//or they may implement their own implementation (override a method)
int getWheelsNumber() {
return 4;
}
}
/**Car IS-A Vehicle*/
class Car extends Vehicle {
public Car() {
maxSpeed = 180;
}
public void showMaxSpeed() {
System.out.println("Car max speed: " + maxSpeed + " km/h");
}
//Car class will inherit getWheelsNumber() method from the parent class
//there is no need to override this method because default implementation
//is appropriate for Car class - 4 wheels
}
/**Bus IS-A Vehicle*/
class Bus extends Vehicle {
public Bus() {
maxSpeed = 100;
}
public void showMaxSpeed() {
System.out.println("Bus max speed: " + maxSpeed + " km/h");
}
//Bus class will override this method because the default implementation
//is not appropriate for Bus class
public int getWheelsNumber() {
return 6;
}
}
/**Truck IS-A Vehicle*/
class Truck extends Vehicle {
public Truck() {
maxSpeed = 80;
}
public void showMaxSpeed() {
System.out.println("Truck max speed: " + maxSpeed + " km/h");
}
//Truck class will override this method because the default implementation
//is not appropriate for Bus class
public int getWheelsNumber() {
return 10;
}
}
public class AbstractClassExample {
public static void main(String[] args) {
Vehicle car = new Car();
Vehicle bus = new Bus();
Vehicle truck = new Truck();
car.showMaxSpeed();
bus.showMaxSpeed();
truck.showMaxSpeed();
System.out.println("Wheels number-car:" + car.getWheelsNumber() +
", bus:" + bus.getWheelsNumber() + ", truck:" + truck.getWheelsNumber());
}
}
|
J.W. PRODUCTION
REFERENCE: http://www.javacodeexamples.com/java-abstract-class-example/