27.abstract Class in Java
27.abstract Class in Java
Vishnu
Let us take an example of graphic objects. Different graphic objects are there such as
circle, rectangle, triangle etc. They all have state defined by their positions, colour etc.
and behaviour defined by draw, resize, calculate size etc. As all these object types has
common things but with different implementations. We can take advantage of common
things with different implementations and put these common things in an abstract class
(say GraphicObjects) then extends this class in subclasses to provide specific
implementations.
Syntax:
// declare fields
Abstract method:
Syntax:
Example:
package com.sst;
void showShape() {
System.out.println("Object type is Triangle.");
}
2
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
27.Abstract Class By Mr. Vishnu
Output:
If a class have one abstract method it must be an abstract class but vice versa is
not true i.e. it is not necessary that an abstract class have an abstract method.
Example:
If a class extends abstract class than either it has to provide implementation of all
abstract methods or declare this class as abstract class.
Example:
package com.vishnu;
/**
* This program is used to show that a class either have to provide
* implementation of all abstract methods of extended abstract class or declare
* abstract itself.
*/
abstract class GraphicObjects {
// abstract method declaration
3
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
27.Abstract Class By Mr. Vishnu
Output:
Error
An abstract class can have both static and non-static data members and methods
like any other java class.
Example:
package com.vishnu;
/**
4
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
27.Abstract Class By Mr. Vishnu
* This program is used to show that abstract class can have both static
* and non-static data members and methods like any other java class.
*/
abstract class GraphicObjects{
//non static data member
int var1 = 50;
5
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
27.Abstract Class By Mr. Vishnu
Output:
Area = 250
Graphic objects.
Yes, abstract class have constructors in java. But it is not used to instantiate abstract
class. It is used in constructor chaining or to initialize abstract class common variables.
No, abstract class can’t be final in Java because abstract classes are used only by
extending and if they made final they can’t extended.
6
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.