Introduction to Abstract Classes
An essential element of object-oriented programming is abstraction. Humans manage complexity through abstraction. For example, people do not think of a car as a set of tens of thousands of individual parts. They think of it as a well-defined object with its own unique behavior. This abstraction allows people to use a car to drive to the grocery store without being overwhelmed by the complexity of the parts that form the car. They can ignore the details of how the engine, transmission, and braking systems work. Instead they are free to utilize the object as a whole.
In constructing a hierarchy of relationships among various classes, it is sometimes beneficial to include a class, normally at the top of the hierarchy, whose methods cannot be instantiated. The class acts as a blueprint for all subclasses, and as such it can be extended to suit different classes within the taxonomy.
Suppose we were modeling the behavior of animals, by creating a class hierarchy that started with a base class called Animal. Animals are capable of doing different things like flying, digging and walking, but there are some common operations as well like eating and sleeping. Some common operations are performed by all animals, but in a different way as well. When an operation is performed in a different way, it is a good candidate for an abstract method (forcing subclasses to provide a custom implementation).
Abstract class cannot be instantiated. The abstract class is a generalized superclass extended by subclasses and has atleast one abstract behavior and may contain behaviors with body.
An abstract class can have methods with implementation and without implementation. The method without implementation is called abstract method. An abstract method is defined by the method’s signature and has no method body. An abstract class is a class that contains at least one abstract method. As a result, an abstract class cannot be instantiated since there would be no means of implementing the abstract method(s) within the class. When a class contains at least one abstract method, the class is automatically taken to be abstract. However, not all the methods of an abstract class need be abstract.
A subclass of an abstract class may be instantiated, provided that the abstract methods of the abstract class are overridden and implemented in the subclass. If all the abstract methods are not implemented, the subclass must also be abstract.
Syntax
abstract class MyClass {
//Method with implementation (Non-abstract method)
public void show() {
// do something
statement(s);
}
//Abstract method
public abstract int sum();
}
Example
1. /**
2. * @(#)AbstractDemo.java
3. *
4. *
5. * @author
6. * @version 1.00 2009/11/20
7. */
8.
9. abstract class Animal {
10.
11. public Animal (String name) {
12.
13. System.out.println("The Animal is " + name);
14.
15. }
16.
17. public void eat(String food) {
18.
19. System.out.println("Eats " + food);
20. }
21.
22. public void sleep(int hours) {
23.
24. System.out.println("Sleeps for " + hours + " hours");
25.
26. }
27.
28. //Abstract method
29. public abstract void makeNoise();
30.
31. }
32.
33. class Dog extends Animal {
34.
35. public Dog() {
36.
37. super("Dog");
38.
39. }
40.
41.
42. //Override abstract method of super class
43. public void makeNoise() {
45.
46. System.out.println ("Noise is Bark! Bark!");
47.
48. }
49.
50. }
51.
52. class Cat extends Animal {
53.
54. public Cat() {
55.
56. super("Cat");
57.
58. }
59.
60. public void makeNoise() {
61.
62. System.out.println ("Noise is Moo! Moo!");
63.
64. }
65.
66. }
67.
68. public class AbstractDemo {
69.
70. /**
71. * Creates a new instance of AbstractDemo.
72. */
73. public AbstractDemo() {
74. }
75.
76. /**
77. * @param args the command line arguments
78. */
79.
80. public static void main(String[] args) {
81.
82. // TODO code application logic here
83.
84. Dog dog = new Dog();
85.
86. dog.eat("Mutton");
87. dog.sleep(2);
88. dog.makeNoise();
89.
90. Cat cat = new Cat();
91.
92. cat.eat("Chiken");
93. cat.sleep(1);
94. cat.makeNoise();
95.
96.
97.
98. }
99. }
Output
The Animal is Dog
Eats Mutton
Sleeps for 2 hours
Noise is Bark! Bark!
The Animal is Cat
Eats Chiken
Sleeps for 1 hours
Noise is Moo! Moo!
