
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Parameterized Constructor in an Abstract Class in Java
A common question in Java OOPs is whether abstract classes can have parameterized constructors. Yes, we can define a parameterized constructor in an abstract class.
What is a Parameterized Constructor in Java
A parameterized constructor is a special type of class constructor that accepts parameters/arguments when creating an object. Unlike a default (no-arg) constructor, it allows you to initialize an object with specific values at the time of creation.
Syntax
The Following is the syntax:
public class ClassName { private dataType field1; private dataType field2; // Parameterized constructor public ClassName(dataType param1, dataType param2) { this.field1 = param1; this.field2 = param2; } }
Abstract Class Constructor Conditions
Conditions for defining a parameterized constructor in an abstract class:
- We need to make sure that the class which is extending an abstract class have a constructor and it can call the superclass parameterized constructor.
- We can call the superclass parameterized constructor in a subclass by using super() call.
- If we are not placing super() call in the subclass constructor, a compile-time error will occur.
Example of Parameterized Constructor in an Abstract Class
Below is an example of a parameterized constructor in an abstract class in Java:
abstract class AbstractClassTest { AbstractClassTest(int a) { // Parameterized Constructor System.out.println("Parameterized Constructor of an abstract class a="+ a); } } public class Test extends AbstractClassTest { Test() { super(20); System.out.println("Test Class Constructor"); } public static void main(String[] args) { Test obj = new Test(); } }
Output
Parameterized Constructor of an abstract class a=20 Test Class Constructor
Practical Use Case with Abstract Method
In this example, the abstract class Shape has a constructor that takes a color parameter. This enforces that any subclass, like Circle, must call super(color) to properly initialize the inherited property. The abstract method area() is implemented in the subclass.
abstract class Shape { private String color; public Shape(String color) { this.color = color; System.out.println("Shape constructor called. Color: " + color); } public abstract double area(); } class Circle extends Shape { private double radius; public Circle(String color, double radius) { super(color); this.radius = radius; } @Override public double area() { return Math.PI * radius * radius; } } public class Main { public static void main(String[] args) { Circle circle = new Circle("Red", 5.0); System.out.println("Area: " + circle.area()); } }
Output
Shape constructor called. Color: Red Area: 78.53981633974483
Advertisements