Abstract class in java

Abstract class in real world:

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.

Abstract class in java:

Abstract class is a way of implementing 0 to 100% abstraction. A class declared with abstract keyword is known as an abstract class. An abstract class may or may not contain abstract method. Abstract classes cannot be instantiated.

Syntax:

abstract class className {

   // declare fields

   // declare abstract/non-abstract methods

}

Abstract method:

A method with no implementation i.e. without braces and followed by a semicolon.

Syntax:

abstract return_type methodName();

Example:

/**
 * This program is used to show simple use of abstract class.
 * @author W3spoint
 */
abstract class GraphicObjects{
                //abstract method declaration
                abstract void showShape();
}
 
class Circle extends GraphicObjects{
                /**
                 * This is the overridden method, provide implementation
                 * of abstract method according to your need.
                 * @author W3spoint
                 */
                void showShape() {
                      System.out.println("Object type is Circle.");
                }     
}
 
class Rectangle extends GraphicObjects{
                /**
                 * This is the overridden method, provide implementation
                 * of abstract method according to your need.
                 * @author W3spoint
                 */
                void showShape() {
                     System.out.println("Object type is Rectangle.");
               }  
}
 
class Triangle extends GraphicObjects{
                /**
                 * This is the overridden method, provide implementation
                 * of abstract method according to your need.
                 * @author W3spoint
                 */
                void showShape() {
                     System.out.println("Object type is Triangle.");
                }             
 
}
 
public class AbstractClassExample1 {
                public static void main(String args[]){
                       //GraphicObjects is the super class
                       //hence it's reference can contain subclass object.
                       GraphicObjects obj = new Circle();
                       obj.showShape();
                       obj = new Rectangle();
                       obj.showShape();
                       obj = new Triangle();
                       obj.showShape();
                }  
}

Output:

Object type is Circle.
Object type is Rectangle.
Object type is Triangle.

Download this example.

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:

abstract class GraphicObjects{//no error
                //non-abstract method declaration
                void showShape(){
                                System.out.println("Print object shape.");
                }
}
//error here class must be abstract if it 
//have one or more abstract methods.
class GraphicObjects{
                //abstract method declaration
                abstract void showShape(){
}

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:

/**
 * 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.
 * @author W3spoint
 */
abstract class GraphicObjects{
                //abstract method declaration
                abstract void showShape();
}
 
class Circle extends GraphicObjects{
                /**
                 * This is the overridden method, provide implementation
                 * of abstract method according to your need.
                 * @author W3spoint
                 */
                void showShape() {
                     System.out.println("Object type is Circle.");
                }             
}
 
class Rectangle extends GraphicObjects{
          //error here, Rectangle class have to provide implementation
          //of all abstract methods of extended abstract class.
}
 
abstract class Triangle extends GraphicObjects{
                //no error here, because Triangle class is declared
                //as an abstract class
}
 
public class AbstractClassExample2 {
                public static void main(String args[]){
                       //GraphicObjects is the super class
                       //hence it's reference can contain subclass object.
                       GraphicObjects obj = new Circle();
                       obj.showShape();
                }    
}

Output:

Error.

Download this example.

An abstract class can have both static and non-static data members and methods like any other java class.

Example:

/**
 * 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.
 * @author W3spoint
 */
abstract class GraphicObjects{
                //non static data member
                int var1 = 50;              
 
                //static data member
                static String str1 = "www.w3spoint.com"; 
 
                //abstract method declaration
                abstract void showShape();
 
                //non abstract, non static method
                void area(int area){
                     System.out.println("Area = " + area);
                }
 
                //non abstract, static method
                static void displayGraphicObjects(){
                        System.out.println("Graphic objects.");
                }
}
 
class Circle extends GraphicObjects{
                /**
                 * This is the overridden method, provide implementation
                 * of abstract method according to your need.
                 * @author W3spoint
                 */
                void showShape() {
                     System.out.println("Object type is Circle.");
                     System.out.println("Non static variable = " + var1);
                }
}
 
public class AbstractClassExample3 {
                public static void main(String args[]){
                     //GraphicObjects is the super class
                     //hence it's reference can contain subclass object.
                     GraphicObjects obj = new Circle();
                     obj.showShape();
                     obj.area(250);                               
 
                     //call static method and variable with class name.
                     GraphicObjects.displayGraphicObjects();
                     System.out.println("static variable = "
                      + GraphicObjects.str1);
                }           
}

Output:

Area = 250
Graphic objects.
static variable = www.w3spoint.com

Download this example.

Why abstract class is used:

Abstract class in java is used to implement 0 to 100% abstraction.
Note: Abstract class provide 0 to 100% abstraction because it may contain no abstract method or it may contain some of its methods as abstract methods or it may contain all its methods as abstract methods.

Can abstract class have constructors in Java?

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. 

Can abstract class be final in Java?

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.
 
Next Topic: Interface in java with example.
Previous Topic: How to read input from command line in Java using Scanner.

Java interview questions on interface and abstract class

 

Content Protection by DMCA.com
Please Share