Abstract classes in Java

By: Kamini Viewed: 159065 times  Printer Friendly Format    


There are situations in which you will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. That is, sometimes you will want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details. Such a class determines the nature of the methods that the subclasses must implement. One way this situation can occur is when a superclass is unable to create a meaningful implementation for a method. This is the case with the class Figure used in the preceding example. The definition of area( ) is simply a placeholder. It will not compute and display the area of any type of object.

As you will see as you create your own class libraries, it is not uncommon for a method to have no meaningful definition in the context of its superclass. You can handle this situation two ways. One way, as shown in the previous example, is to simply have it report a warning message. While this approach can be useful in certain situations—such as debugging—it is not usually appropriate. You may have methods which must be overridden by the subclass in order for the subclass to have any meaning. Consider the class Triangle. It has no meaning if area( ) is not defined. In this case, you want some way to ensure that a subclass does, indeed, override all necessary methods. Java's
solution to this problem is the abstract method.

You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. These methods are sometimes referred to as subclasser responsibility because they have no implementation specified in the superclass. Thus, a subclass must override them—it cannot simply use the version defined in the superclass. To declare an abstract method, use this general form:

abstract type name(parameter-list);

As you can see, no method body is present. Any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration. There can be no objects of an abstract class. That is, an abstract class cannot be directly instantiated with the new operator. Such objects would be useless, because an abstract class is not fully defined. Also, you cannot declare abstract constructors, or abstract static methods. Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared abstract.
Here is a simple example of a class with an abstract method, followed by a class which implements that method:

// A Simple demonstration of abstract.
abstract class A {
abstract void callme();
// concrete methods are still allowed in abstract classes
void callmetoo() {
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();
}
}

Notice that no objects of class A are declared in the program. As mentioned, it is not possible to instantiate an abstract class. One other point: class A implements a concrete method called callmetoo( ). This is perfectly acceptable. Abstract classes can include as much implementation as they see fit.

Although abstract classes cannot be used to instantiate objects, they can be used to create object references, because Java's approach to run-time polymorphism is implemented through the use of superclass references. Thus, it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object. You will see this feature put to use in the next example. Using an abstract class, you can improve the Figure class shown earlier. Since there is no meaningful concept of area for an undefined two-dimensional figure, the following version of the program declares area( ) as abstract inside Figure. This, of course, means that all classes derived from Figure must override area( ).

// 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
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
}
}

As the comment inside main( ) indicates, it is no longer possible to declare objects of type Figure, since it is now abstract. And, all subclasses of Figure must override area( ). To prove this to yourself, try creating a subclass that does not override area( ). You will receive a compile-time error.

Although it is not possible to create an object of type Figure, you can create a reference variable of type Figure. The variable figref is declared as a reference to Figure, which means that it can be used to refer to an object of any class derived from Figure. As explained, it is through superclass reference variables that overridden methods are resolved at run time.



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Awesome One....... Thanx...... m new to java...and
View Tutorial          By: jayanta at 2010-02-05 04:35:34

2. No use of this example.
I was searching oth

View Tutorial          By: Karan at 2010-04-10 05:41:47

3. thanks for giving the example of abstract class...
View Tutorial          By: mahesh at 2010-04-11 08:20:45

4. thanks for giving the example of abstract class...
View Tutorial          By: mahesh at 2010-04-11 08:21:25

5. Very good basic example. Thanks a lot.............
View Tutorial          By: satish at 2010-04-15 05:39:41

6. that was gud example to explain abstract. i have d
View Tutorial          By: zishan at 2010-05-20 00:21:46

7. Thanks a lot this thing was very importent for me!
View Tutorial          By: Andrew at 2010-05-23 01:47:04

8. The above example is help me that how to make abst
View Tutorial          By: LALIT PAL SINGH ShAKTAWAT at 2010-06-09 14:47:24

9. A very nice example for abstract classes, simple b
View Tutorial          By: Rahul at 2010-06-16 20:45:24

10. Good example of abstract class.
View Tutorial          By: Rathna Surapaneni at 2010-07-08 01:44:10

11. Good example.
View Tutorial          By: Rathna at 2010-07-08 01:47:34

12. Awesome, I am Impressed with the way the example h
View Tutorial          By: Darshan at 2010-07-09 01:34:22

13. yes it is very good example for Abstract classes a
View Tutorial          By: obulreddy.G at 2010-07-13 22:52:41

14. yes it is very good example for Abstract classes a
View Tutorial          By: obulreddy.G at 2010-07-13 22:53:11

15. yes it is very good example for Abstract classes a
View Tutorial          By: obulreddy.G at 2010-07-13 22:53:16

16. yes,it is a very nice example for abstract classe
View Tutorial          By: saranya at 2010-07-16 05:56:53

17. yes it is good example for abstract classes.Thanks
View Tutorial          By: Nilesh Chavan at 2010-07-17 02:24:42

18. Yeah..it's very good example....
View Tutorial          By: Packiyaraj.T at 2010-07-22 22:39:48

19. all the things are copied from java complete refre
View Tutorial          By: gaurav at 2010-07-28 11:56:08

20. i want different example


<

View Tutorial          By: santhosh at 2010-07-30 01:33:42

21. i want diff example one.....

plz
View Tutorial          By: Santhosh at 2010-07-30 01:35:22

22. very very thankssssssssssssssss
View Tutorial          By: gowtham at 2010-07-31 22:58:36

23. it is Marvelous example.
View Tutorial          By: princevijayakumar at 2010-08-04 07:56:33

24. Thanks for giving this basic example of Abstract c
View Tutorial          By: Yogendra Kirar at 2010-08-07 07:48:34

25. Very good explanation . I was very confuse but rea
View Tutorial          By: Shahabaj at 2010-08-10 08:58:39

26. it's very easy simple progremme in java
View Tutorial          By: sambasiva rao at 2010-09-24 23:53:00

27. Its Good Example to clear my all doubt .. thanx..
View Tutorial          By: zishan_shaikh at 2010-09-25 01:36:07

28. it's nice example of abstract class n methods to m
View Tutorial          By: Anil yadav at 2010-10-04 03:16:46

29. Its very good example but i want some lengthy prog
View Tutorial          By: Thamarai at 2010-10-09 03:00:25

30. Its very good example but i want some lengthy prog
View Tutorial          By: Thamarai at 2010-10-09 03:38:25

31. yes it is very gud but i have still a doubt that i
View Tutorial          By: Akheel at 2010-10-10 09:41:13

32. Why cant you try and write a book on Java
View Tutorial          By: Ajay Ivan Cordeiro at 2010-10-15 03:53:57

33. very good example....... thxxxxxxxx
View Tutorial          By: samy at 2010-10-27 03:28:32

34. This example is very useful to me,but i wnat more
View Tutorial          By: vaishali at 2010-12-06 08:20:18

35. thanku very much. it is a good examples for abstr
View Tutorial          By: rajesh at 2010-12-17 10:57:42

36. Thank you very much. Now I know how to make an abs
View Tutorial          By: Alex at 2011-01-17 03:35:22

37. Its really good to understand..thanks.
View Tutorial          By: Raghunandana at 2011-01-19 04:20:16

38. nice.......... but not clear.what is the use of ab
View Tutorial          By: rama krishna at 2011-02-01 06:03:35

39. its taken from complete reference by Schildt.....
View Tutorial          By: gipsy at 2011-02-04 22:37:00

40. i was looking for the real time applications of ab
View Tutorial          By: sravan kumar at 2011-03-08 05:39:13

41. realy it was a nice basic example...and better def
View Tutorial          By: amit kumar at 2011-03-14 08:11:31

42. thank you very much,it was really helpful
View Tutorial          By: kaustubh Damle at 2011-03-26 22:19:17

43. Thks a lot..... The way of giving explanation and
View Tutorial          By: Hari at 2011-04-08 02:58:06

44. Abstract class example is good, What is the differ
View Tutorial          By: megakumar at 2011-04-10 04:15:10

45. Nice Example .........than Q!!!
View Tutorial          By: Sreekar at 2011-06-29 15:31:50

46. neded defination in clear
View Tutorial          By: venkat at 2011-07-04 06:31:19

47. Abstract class is a class that can not be instanti
View Tutorial          By: Abstract at 2011-07-19 01:14:09

48. marvellous example
View Tutorial          By: kas at 2011-08-08 07:08:21

49. thanx it i very helpful 4 me as i m new with java
View Tutorial          By: akminder at 2011-08-09 05:09:19

50. this is not enough give a some thory or example
View Tutorial          By: rahul at 2011-08-16 07:04:39

51. copy of h.sheld
View Tutorial          By: vivek at 2011-08-31 13:12:36

52. This topic abstract class make me vry confused bt
View Tutorial          By: anu at 2011-09-02 08:07:36

53. thanx. for this tutorial. i was having some doubt
View Tutorial          By: prabhash at 2011-09-06 10:15:19

54. gud one
View Tutorial          By: bluedust at 2011-09-13 06:25:15

55. well done this will be a jackpot for learner
View Tutorial          By: Brijesh at 2011-09-16 11:28:16

56. this looks like taken from "the Complete refe
View Tutorial          By: viraj at 2011-09-18 17:33:14

57. Thanks for giving a simple but illustrative exampl
View Tutorial          By: prakash at 2011-09-21 07:22:28

58. Halaldi qaqa
View Tutorial          By: Etibar at 2011-09-23 08:52:30

59. atterporS
View Tutorial          By: atterporS at 2011-09-24 11:14:26

60. Awesome example dude. It really help 4 me to under
View Tutorial          By: Piyush Parmar at 2011-09-25 08:15:29

61. Hi All,
This example is very good,friend i

View Tutorial          By: Rohit Kumar Modi at 2011-09-26 19:39:43

62. MorrisGoto
View Tutorial          By: PriceDelw at 2011-09-27 14:26:33

63. This is a good site.THANKS A LOT.
View Tutorial          By: ASWANI at 2011-10-04 09:34:45

64. this exampless are copy and paste from Java comple
View Tutorial          By: prabhat at 2011-10-07 07:37:38

65. Nice tutorial... Even i was not sure what abstract
View Tutorial          By: Arun Abraham at 2011-10-13 05:33:28

66. it's awesome tutorial ..!! thank u very much
View Tutorial          By: Ankur at 2011-10-17 12:05:16

67. Thanks, yes this is the best example of abstract
View Tutorial          By: karuna at 2011-10-19 14:51:41

68. Here i posted a good example with spring. Here rea
View Tutorial          By: Vikash K Agarwal at 2011-11-02 12:02:50

69. thnks lot 4 helping me
View Tutorial          By: sandeep dudi at 2011-11-17 06:06:03

70. ok this is gud example to build an abstract class.
View Tutorial          By: ashish at 2011-12-01 06:22:32

71. thanks
View Tutorial          By: what is the use of abstract class and difference between abstract and interface brief explan and with example at 2011-12-02 09:01:08

72. keroltjejh
View Tutorial          By: keroltjejh at 2012-01-21 16:53:15

73. nice to see that type of tutorial..
View Tutorial          By: hulk at 2011-12-06 10:44:13

74. If one can't use English grammar correctly and wit
View Tutorial          By: Java Man at 2011-12-09 06:04:14

75. thanks for this example
View Tutorial          By: hai at 2011-12-16 05:36:14

76. what is the meaning of instantiated in abstract cl
View Tutorial          By: Manikandan MP at 2011-12-20 15:17:38

77. its very good example explains what is an abstarct
View Tutorial          By: sandeep at 2012-01-06 07:13:29

78. good start up fr java beginers
View Tutorial          By: sandeep Ch at 2012-01-06 07:16:34

79. imendklekr
View Tutorial          By: imendklekr at 2012-01-15 09:29:35

80. nice example for understanding abstract class,,,bu
View Tutorial          By: Aavez Sk at 2012-01-17 09:42:50

81. nice example for understanding abstract class,,,bu
View Tutorial          By: Aavez Sk at 2012-01-17 09:44:36

82. marionelrk
View Tutorial          By: marionelrk at 2012-01-18 02:33:22

83. i have cleared with my doubts about abstract class
View Tutorial          By: akash at 2012-01-31 05:05:03

84. its very good , good example for beginners
View Tutorial          By: Rohit Mumbai at 2012-02-03 06:59:13

85. really very good example .now i have cleared what
View Tutorial          By: pushpendra bansal gurjar at 2012-02-15 12:19:58

86. abstract class Explain the concept of abstract c
View Tutorial          By: rahul at 2012-02-23 08:30:47

87. Before i had lot of doubts in abstract class... no
View Tutorial          By: Mohandoss at 2012-04-02 05:08:43

88. abstract class A {
abstract void callme()

View Tutorial          By: satish at 2012-04-07 10:35:38

89. if a class is declared as abstract and it contains
View Tutorial          By: nagaraj at 2012-04-25 17:12:30

90. nicely explained :)
View Tutorial          By: ibtisam at 2012-04-26 10:31:58

91. Thnaks a lot. Deep in brief.
View Tutorial          By: Thanks at 2012-05-23 03:36:37

92. Good one...this will surely hlpe a lot...thanks aq
View Tutorial          By: zubair at 2012-07-03 06:51:00

93. Awesome Example plz I need some more example
View Tutorial          By: Amit at 2012-07-19 07:40:35

94. Really excellent. Covered everything in abstract b
View Tutorial          By: Ananthi R at 2012-07-23 14:08:47

95. Hi Zishan

this is useful for you,
View Tutorial          By: srini at 2012-08-03 14:42:58

96. Hello to Every one!! :-),HERE I'AM NOT WRITING A C
View Tutorial          By: Pulkit Malviya at 2012-08-26 14:56:18

97. we can create reference variable for an abstract c
View Tutorial          By: siva at 2012-08-30 06:03:50

98. thank u that is a simple and very understandable p
View Tutorial          By: narendra at 2012-09-10 06:48:28

99. thank u that is a simple and very understandable p
View Tutorial          By: narendra at 2012-09-10 06:51:16

100. not useful
bcz it is totally copy of java

View Tutorial          By: sonia at 2012-09-29 15:23:20

101. thanku this is very good example
View Tutorial          By: priyanka at 2012-10-02 13:08:49

102. Hi : Pulkit Malviya .I think that book has coding
View Tutorial          By: Anand at 2012-11-04 14:13:50

103. Interfaces contains all abstract methods(with out
View Tutorial          By: Mahesh at 2012-11-09 06:14:47

104. /* File name : Employee.java */
public abst

View Tutorial          By: Anonymous at 2012-11-16 12:23:17

105. Ditto copied from Herbert Schildt complete referen
View Tutorial          By: Girish at 2012-12-10 21:58:01

106. hi What is the meaning of this line ,
you c

View Tutorial          By: Anil Chalhotra at 2013-02-16 08:04:03

107. thanks for all samples..
View Tutorial          By: 珠洲市 at 2013-03-01 09:47:46

108. super......... thanks 4 ur great example...
View Tutorial          By: jithinraj at 2013-03-13 08:34:07

109. nice 1
View Tutorial          By: vivek at 2013-03-19 16:25:47

110. Which one is the inbuilt Abstract class in java
View Tutorial          By: vikas jain at 2013-04-16 07:25:33

111. Its just copied from Java Complete Reference book
View Tutorial          By: heart locker at 2013-04-19 13:53:06

112. Wonderful tutorial. Great example given.
To

View Tutorial          By: Hassam at 2013-06-05 12:31:51

113. A very good example to kickstart my preparation on
View Tutorial          By: Javaenthu at 2013-07-28 06:35:44

114. The above example for abstract class is very good.
View Tutorial          By: saafia at 2013-08-19 14:27:56

115. The above example for abstract class is very good.
View Tutorial          By: saafia at 2013-08-19 14:33:54

116. we can write an abstract class without any abstrac
View Tutorial          By: keerthi at 2013-08-20 16:31:38

117. Can you guys please help me with this question? I
View Tutorial          By: Christina Prinze at 2013-10-11 15:49:01

118. that's what I've been looking for!!!!!!!!!!!!!!!!!
View Tutorial          By: bibi at 2014-12-28 20:43:56