Abstract classes in Java
By: Kamini
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.
Archived Comments
1. that's what I've been looking for!!!!!!!!!!!!!!!!!!!!!!!!!! Thx
View Tutorial By: bibi at 2014-12-28 20:43:56
2. Can you guys please help me with this question? I would really really appreciate it..Thanks guys
View Tutorial By: Christina Prinze at 2013-10-11 15:49:01
3. we can write an abstract class without any abstract methods.its true.bt wats the use?
View Tutorial By: keerthi at 2013-08-20 16:31:38
4. The above example for abstract class is very good.sir i have a doubt,how develop a message abstract
View Tutorial By: saafia at 2013-08-19 14:33:54
5. The above example for abstract class is very good.sir i have a doubt,how develop a message abstract
View Tutorial By: saafia at 2013-08-19 14:27:56
6. A very good example to kickstart my preparation on abstract classes and methods
View Tutorial By: Javaenthu at 2013-07-28 06:35:44
7. Wonderful tutorial. Great example given.
Tomorrow is my exam, so it helped me a lot.
View Tutorial By: Hassam at 2013-06-05 12:31:51
8. Its just copied from Java Complete Reference book. I wanted something other than Complete Reference
View Tutorial By: heart locker at 2013-04-19 13:53:06
9. Which one is the inbuilt Abstract class in java
View Tutorial By: vikas jain at 2013-04-16 07:25:33
10. nice 1
View Tutorial By: vivek at 2013-03-19 16:25:47
11. super......... thanks 4 ur great example...
View Tutorial By: jithinraj at 2013-03-13 08:34:07
12. thanks for all samples..
View Tutorial By: ç æ´²å¸‚ at 2013-03-01 09:47:46
13. hi What is the meaning of this line ,
you cannot declare abstract constructors, or abstract s
View Tutorial By: Anil Chalhotra at 2013-02-16 08:04:03
14. Ditto copied from Herbert Schildt complete reference java
View Tutorial By: Girish at 2012-12-10 21:58:01
15. /* File name : Employee.java */
public abstract class Employee
{
 &
View Tutorial By: Anonymous at 2012-11-16 12:23:17
16. Interfaces contains all abstract methods(with out logic or body for method) it means every method o
View Tutorial By: Mahesh at 2012-11-09 06:14:47
17. Hi : Pulkit Malviya .I think that book has coding example also so once again refer that book clearly
View Tutorial By: Anand at 2012-11-04 14:13:50
18. thanku this is very good example
View Tutorial By: priyanka at 2012-10-02 13:08:49
19. not useful
bcz it is totally copy of java complete ref
so plz give diff example
View Tutorial By: sonia at 2012-09-29 15:23:20
20. thank u that is a simple and very understandable program......
View Tutorial By: narendra at 2012-09-10 06:51:16
21. thank u that is a simple and very understandable program......
View Tutorial By: narendra at 2012-09-10 06:48:28
22. we can create reference variable for an abstract class but we cant call a method in the subclass , w
View Tutorial By: siva at 2012-08-30 06:03:50
23. Hello to Every one!! :-),HERE I'AM NOT WRITING A CODE BECZ IT WILL TAKE A LOTS OF SPACE,SO I'AM JUS
View Tutorial By: Pulkit Malviya at 2012-08-26 14:56:18
24. Hi Zishan
this is useful for you,
Abstract classes cannot be instantia
View Tutorial By: srini at 2012-08-03 14:42:58
25. Really excellent. Covered everything in abstract but still simple. GREAT WORK
View Tutorial By: Ananthi R at 2012-07-23 14:08:47
26. Awesome Example plz I need some more example
View Tutorial By: Amit at 2012-07-19 07:40:35
27. Good one...this will surely hlpe a lot...thanks aqain...
View Tutorial By: zubair at 2012-07-03 06:51:00
28. Thnaks a lot. Deep in brief.
View Tutorial By: Thanks at 2012-05-23 03:36:37
29. nicely explained :)
View Tutorial By: ibtisam at 2012-04-26 10:31:58
30. if a class is declared as abstract and it contains two abstract methods those methods should be call
View Tutorial By: nagaraj at 2012-04-25 17:12:30
31. abstract class A {
abstract void callme();
void callmetoo() {
S
View Tutorial By: satish at 2012-04-07 10:35:38
32. Before i had lot of doubts in abstract class... now i am clear with in this... Thank you very much
View Tutorial By: Mohandoss at 2012-04-02 05:08:43
33. abstract class Explain the concept of abstract class and it?s use with a sample program. Java Ab
View Tutorial By: rahul at 2012-02-23 08:30:47
34. really very good example .now i have cleared what is abstract class.java-samples is extremely good s
View Tutorial By: pushpendra bansal gurjar at 2012-02-15 12:19:58
35. its very good , good example for beginners
View Tutorial By: Rohit Mumbai at 2012-02-03 06:59:13
36. i have cleared with my doubts about abstract class.....Dhanyawad..
View Tutorial By: akash at 2012-01-31 05:05:03
37. keroltjejh
View Tutorial By: keroltjejh at 2012-01-21 16:53:15
38. marionelrk
View Tutorial By: marionelrk at 2012-01-18 02:33:22
39. nice example for understanding abstract class,,,but its a basic one not covered details..anyway gud.
View Tutorial By: Aavez Sk at 2012-01-17 09:44:36
40. nice example for understanding abstract class,,,but its a basic one not covered details..anyway gud.
View Tutorial By: Aavez Sk at 2012-01-17 09:42:50
41. imendklekr
View Tutorial By: imendklekr at 2012-01-15 09:29:35
42. good start up fr java beginers
View Tutorial By: sandeep Ch at 2012-01-06 07:16:34
43. its very good example explains what is an abstarct class easily
View Tutorial By: sandeep at 2012-01-06 07:13:29
44. what is the meaning of instantiated in abstract class?
View Tutorial By: Manikandan MP at 2011-12-20 15:17:38
45. thanks for this example
View Tutorial By: hai at 2011-12-16 05:36:14
46. If one can't use English grammar correctly and with sense, how can one understand a programming lang
View Tutorial By: Java Man at 2011-12-09 06:04:14
47. nice to see that type of tutorial..
View Tutorial By: hulk at 2011-12-06 10:44:13
48. 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
49. ok this is gud example to build an abstract class. but i want know that what is the actual use of ab
View Tutorial By: ashish at 2011-12-01 06:22:32
50. thnks lot 4 helping me
View Tutorial By: sandeep dudi at 2011-11-17 06:06:03
51. Here i posted a good example with spring. Here really implementation of OOPS.
public abstract
View Tutorial By: Vikash K Agarwal at 2011-11-02 12:02:50
52. Thanks, yes this is the best example of abstract class.It easily understand
every one.
View Tutorial By: karuna at 2011-10-19 14:51:41
53. it's awesome tutorial ..!! thank u very much ..!
View Tutorial By: Ankur at 2011-10-17 12:05:16
54. Nice tutorial... Even i was not sure what abstract classes meant, thanks a lot.. Have got a good und
View Tutorial By: Arun Abraham at 2011-10-13 05:33:28
55. this exampless are copy and paste from Java complete reference book. uyou have another ?
View Tutorial By: prabhat at 2011-10-07 07:37:38
56. This is a good site.THANKS A LOT.
View Tutorial By: ASWANI at 2011-10-04 09:34:45
57. MorrisGoto
View Tutorial By: PriceDelw at 2011-09-27 14:26:33
58. Hi All,
This example is very good,friend i have opened one blog there we can discuss many thi
View Tutorial By: Rohit Kumar Modi at 2011-09-26 19:39:43
59. Awesome example dude. It really help 4 me to understand the concept properly.
:) Thanks a lot
View Tutorial By: Piyush Parmar at 2011-09-25 08:15:29
60. atterporS
View Tutorial By: atterporS at 2011-09-24 11:14:26
61. Halaldi qaqa
View Tutorial By: Etibar at 2011-09-23 08:52:30
62. Thanks for giving a simple but illustrative example
View Tutorial By: prakash at 2011-09-21 07:22:28
63. this looks like taken from "the Complete reference java" book by herbert schildt.....
View Tutorial By: viraj at 2011-09-18 17:33:14
64. well done this will be a jackpot for learner
View Tutorial By: Brijesh at 2011-09-16 11:28:16
65. gud one
View Tutorial By: bluedust at 2011-09-13 06:25:15
66. thanx. for this tutorial. i was having some doubt on abstract class. now, its clear.
View Tutorial By: prabhash at 2011-09-06 10:15:19
67. This topic abstract class make me vry confused bt nw m satisfied........thnx............
View Tutorial By: anu at 2011-09-02 08:07:36
68. copy of h.sheld
View Tutorial By: vivek at 2011-08-31 13:12:36
69. this is not enough give a some thory or example
View Tutorial By: rahul at 2011-08-16 07:04:39
70. thanx it i very helpful 4 me as i m new with java
View Tutorial By: akminder at 2011-08-09 05:09:19
71. marvellous example
View Tutorial By: kas at 2011-08-08 07:08:21
72. Abstract class is a class that can not be instantiated, it exists extensively for inheritance and it
View Tutorial By: Abstract at 2011-07-19 01:14:09
73. neded defination in clear
View Tutorial By: venkat at 2011-07-04 06:31:19
74. Nice Example .........than Q!!!
View Tutorial By: Sreekar at 2011-06-29 15:31:50
75. Abstract class example is good, What is the difference between abstract and interface . And give exa
View Tutorial By: megakumar at 2011-04-10 04:15:10
76. Thks a lot..... The way of giving explanation and exmples are hossom to see and learn...for beginner
View Tutorial By: Hari at 2011-04-08 02:58:06
77. thank you very much,it was really helpful
View Tutorial By: kaustubh Damle at 2011-03-26 22:19:17
78. realy it was a nice basic example...and better definition...my problem has solved after reading this
View Tutorial By: amit kumar at 2011-03-14 08:11:31
79. i was looking for the real time applications of abstract class
View Tutorial By: sravan kumar at 2011-03-08 05:39:13
80. its taken from complete reference by Schildt..... give some genuine example....
View Tutorial By: gipsy at 2011-02-04 22:37:00
81. nice.......... but not clear.what is the use of abstract????????????
View Tutorial By: rama krishna at 2011-02-01 06:03:35
82. Its really good to understand..thanks.
View Tutorial By: Raghunandana at 2011-01-19 04:20:16
83. Thank you very much. Now I know how to make an abstract class
View Tutorial By: Alex at 2011-01-17 03:35:22
84. thanku very much. it is a good examples for abstract class
View Tutorial By: rajesh at 2010-12-17 10:57:42
85. This example is very useful to me,but i wnat more examples to clarify my doubts.plz send to my mail
View Tutorial By: vaishali at 2010-12-06 08:20:18
86. very good example....... thxxxxxxxx
View Tutorial By: samy at 2010-10-27 03:28:32
87. Why cant you try and write a book on Java
View Tutorial By: Ajay Ivan Cordeiro at 2010-10-15 03:53:57
88. yes it is very gud but i have still a doubt that is can we caal a derived class method by using obje
View Tutorial By: Akheel at 2010-10-10 09:41:13
89. Its very good example but i want some lengthy program
View Tutorial By: Thamarai at 2010-10-09 03:38:25
90. Its very good example but i want some lengthy program
View Tutorial By: Thamarai at 2010-10-09 03:00:25
91. it's nice example of abstract class n methods to memorize about
abstracts keyword nice..
View Tutorial By: Anil yadav at 2010-10-04 03:16:46
92. Its Good Example to clear my all doubt .. thanx..
View Tutorial By: zishan_shaikh at 2010-09-25 01:36:07
93. it's very easy simple progremme in java
View Tutorial By: sambasiva rao at 2010-09-24 23:53:00
94. Very good explanation . I was very confuse but read this & my doubts are cleared . Thank you
View Tutorial By: Shahabaj at 2010-08-10 08:58:39
95. Thanks for giving this basic example of Abstract class. It will very help full in my next interview.
View Tutorial By: Yogendra Kirar at 2010-08-07 07:48:34
96. it is Marvelous example.
View Tutorial By: princevijayakumar at 2010-08-04 07:56:33
97. very very thankssssssssssssssss
View Tutorial By: gowtham at 2010-07-31 22:58:36
98. i want diff example one.....
plz
give me
View Tutorial By: Santhosh at 2010-07-30 01:35:22
99. i want different example
plz
give me
View Tutorial By: santhosh at 2010-07-30 01:33:42
100. all the things are copied from java complete refrence
View Tutorial By: gaurav at 2010-07-28 11:56:08
101. Yeah..it's very good example....
View Tutorial By: Packiyaraj.T at 2010-07-22 22:39:48
102. yes it is good example for abstract classes.Thanks a lot this thing was very importent for me!
View Tutorial By: Nilesh Chavan at 2010-07-17 02:24:42
103. yes,it is a very nice example for abstract classes and its methods.
View Tutorial By: saranya at 2010-07-16 05:56:53
104. yes it is very good example for Abstract classes and methods
View Tutorial By: obulreddy.G at 2010-07-13 22:53:16
105. yes it is very good example for Abstract classes and methods
View Tutorial By: obulreddy.G at 2010-07-13 22:53:11
106. yes it is very good example for Abstract classes and methods
View Tutorial By: obulreddy.G at 2010-07-13 22:52:41
107. Awesome, I am Impressed with the way the example has been given here. It is such a great experience
View Tutorial By: Darshan at 2010-07-09 01:34:22
108. Good example.
View Tutorial By: Rathna at 2010-07-08 01:47:34
109. Good example of abstract class.
View Tutorial By: Rathna Surapaneni at 2010-07-08 01:44:10
110. A very nice example for abstract classes, simple but effective
View Tutorial By: Rahul at 2010-06-16 20:45:24
111. The above example is help me that how to make abstract class and method. It is good example for begi
View Tutorial By: LALIT PAL SINGH ShAKTAWAT at 2010-06-09 14:47:24
112. Thanks a lot this thing was very importent for me!
[email protected]
View Tutorial By: Andrew at 2010-05-23 01:47:04
113. that was gud example to explain abstract. i have doubt.....
here is one program which has 1 a
View Tutorial By: zishan at 2010-05-20 00:21:46
114. Very good basic example. Thanks a lot..................
View Tutorial By: satish at 2010-04-15 05:39:41
115. thanks for giving the example of abstract class...........
View Tutorial By: mahesh at 2010-04-11 08:21:25
116. thanks for giving the example of abstract class...........
View Tutorial By: mahesh at 2010-04-11 08:20:45
117. No use of this example.
I was searching other than java complete reference.
and these
View Tutorial By: Karan at 2010-04-10 05:41:47
118. Awesome One....... Thanx...... m new to java...and its an awesome example to start up.
View Tutorial By: jayanta at 2010-02-05 04:35:34
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program