What is java abstract class

Java abstract class introduction

Java abstract class is a class that cannot be instantiated.

We may say that this type of java class is useless because we cannot create instances from a java abstract class.

But, the concept of abstract class is very useful in object oriented programming.

We covered also other OOP concepts like the encapsulation,the polymorphism and the abstraction.

An abstract class is manned to be a parent class. It is created to be used for inheritance as a base class.

An abstract class is declared like this:

Abstract class A {}

This type of classes may have methods and attributes that are inherited by child classes:

Abstract class A {
type1 attr1;
type2 attr2;
public type3 f(){
}
public abstract Type1 g();
}

A java abstract class can have fully implemented methods or methods that have no implementations.

A method declared with its signature and have no implementation is called an abstract method.

In the example above, the method g() is an abstract method.

It has the abstract keyword in its signature.

The method f() is not an abstract method because we opened and closed the curl braces even if its implementation is empty.

We can declare a variable of type A but we cannot instantiate an object of type A.

A variable1; //OK

But, we cannot create an object of type A.

A variable2 = new A(); //compiler error

Let’s define a child class B that inherits from class A and implements the abstract method g():

class B extends A {
public Type1 g(){
}
}

In the class B, the method g() inherited from class A is no longer abstract. So, the class B hasn’t any abstract methods.

Therefore, it is not abstract and we can instantiate it.

This line of code is correct:

B variable3 = new B ();

Or using the polymorphism principle, we can do like this:

A variable4 = new B (); //OK

Java abstract class characteristics

1)     A java class is abstract if it contains one or many abstract methods. The abstract keyword in the class declaration is not mandatory.

class A {
public abstract Type1 g();
}

2)     An abstract method must be public

3)     In an abstract method, the names of formal arguments of the method are mandatory.

class A {
public abstract Type1 g(int); //compiler error
}

4)     A child class that inherits from an abstract class must define all the abstract methods of its parent class in order to be not abstract.

5)     If a child class that inherits from an abstract class doesn’t implement one abstract method, it is abstract and it cannot be instanciated.

6)     Abstract classes cannot have abstract constructors

Java abstract class use cases

      Abstract class in java is important in the Object oriented design.

     We can use it like this:

  • We put the common state of child classes inside the parent abstract class
  • Implement the methods that have the same implementation for all child classes
  • Keep the methods that are specific to each child class as abstracts. In this way, the child class is responsible for providing its own implementation for its parent‘s abstract methods. Otherwise, it will be abstract. It will override its parent class‘s abstract classes.

Java abstract class example

In this example, we will create an abstract class called Shape that has two child classes.

Those child classes are: Circle and Square.

The code of the abstract class Shape is the following:

public abstract class Shape {
 
       protected String name;
      
       public Shape() {
             this.name ="shape";
       }
            
       public Shape(String name) {
             this.name = name;
       }
 
       public String getName() {
             return name;
       }
 
       public void setName(String name) {
             this.name = name;
       }
 
 
       protected void display(){
             System.out.println("My name is "+ name);
       }
      
       public abstract double area();
}

The source code of the Square class is the following:

public class Square extends Shape{
 
       private double side;
            
       public Square() {
             super("square");
             this.side = 0;
       }
      
       public Square(double side, String name) {
             super(name);
             this.side = side;
       }
      
       @Override
       public double area() {
             return side * side;
       }
 
       public double getSide() {
             return side;
       }
 
       public void setSide(double side) {
             this.side = side;
       }
 
}

The code of the circle class is the following:

public class Circle extends Shape {
 
       private double radius;
      
      
       public Circle() {
             super("circle");
             this.radius = 0;
       }
      
       public Circle(double radius, String name) {
             super(name);
             this.radius = radius;
       }
      
       @Override
       public double area(){
            
             return 3.14 * radius * radius;        
       }
      
      
       public double getRadius() {
             return radius;
       }
 
       public void setRadius(float radius) {
             this.radius = radius;
       }
 
}

The Shape abstract class defines:

A common attribute for all its child classes: the name attribute

A common method for all its child classes: the radius method

Then, each one from the child classes will give its own implementation to the area method.

Also, the display method is defined once in the parent class and is reused by its child classes.

This is what we call code reuse.

This is an important concept in Object oriented design that is offered by inheritance.

This is the code of the main class that puts everything in action:

public class
TestShapes {
public static void main(String[] args) {
Shape sq = new Square(1.5,"sq");
System.out.println("The area of sq = "+ sq.area());
sq.display();
Shape cir = new Circle(2.3, "cir");
System.out.println("The area of cir = "+ cir.area());
cir.display();
Square sq1 = new Square(6.7,"sq1");
System.out.println("The area of sq1 = "+ sq1.area());
sq1.display();
            
Shape cir1 = new Circle(7.5, "cir1"); System.out.println("The area of cir1 = "+ cir1.area()); cir1.display();
}
}

Java abstract class : conclusion

This java abstract class tutorial arrives at its end.

We have explained the abstract class concept with many examples.

If this java tutorial was helpful for you, please check our core java tutorial.

You can like our how to program facebook page for more tutorials or follow our how to program Google plus page.

Post a Comment

Previous Post Next Post