Constructor in Abstract Class in Java10 Sept 2024 | 9 min read An Abstract class in Java is a class that cannot be instantiated directly. The goal of this is to act as a base class from which the other classes might inherited and extended. One of the important features that an abstract class has an ability to define the constructors, which are known as special methods and that are invoked when an object of a class is created. Rules to be followed while defining constructors in an abstract class:
Types of Constructors implemented using Abstract Class:There are three types of constructors are there they are:
1. Default Constructor: The constructor is automatically created via Java if no other constructor is defined in the class. It has no parameters and does not perform any moves aside from initializing default values for class fields. ALGORITHM: Step 1: Define an abstract class named "Shape". Step 2: Declare two integer variables "x" and "y" as protected. Step 3: Create a Shape class default constructor and set "x" and "y" to 0. Step 4: Now create a method "getArea()" it is a abstract method which is going to return a double value Step 5: Then create two non-abstract methods "printPosition()" and "setPosition(int x, int y)" which belongs to Shape class. Step 6: The setPosition method sets the values of x and y. Step 7: The printPosition method prints the values of x and y. Step 8: Define a Circle class that extends the Shape class. Step 9: Declare a double variable named "radius" as protected in the Circle class. Step 10: Define a constructor for the Circle class that accepts a double value for the radius. Step 11: Implement the getArea method for the Circle class which calculates the area of the circle. Step 12: Define a Square class that extends the Shape class. Step 13: Declare a double variable named "side" as protected in the Square class. Step 14: Define a constructor for the Square class that accepts a double value for the side. Step 15: Implement the getArea method for the Square class which calculates the area of the square. Step 16: Define a Main class. Step 17: Define the main function in the Main class. Step 18: Create a Circle object and a Square object. Step 19: Call the setPosition method for both the Circle and Square objects. Step 20: Call the getArea method for both the Circle and Square objects and print the results. Step 21: Call the printPosition method for both the Circle and Square objects and print the results. Implementation: Here is the implementation of above steps FileName: DefaultMain.java Output: Area of a circle is: 78.53981633974483 The Position:(2, 3) Area of a square is: 16.0 The Position:(5, 7) 2. Parameterized Constructor: When creating an object, this kind of constructor lets you pass arguments to it. When you wish to initialise the object with values, it is helpful. The parameterized constructor is defined with one or extra parameters, and whilst an object is created, the values passed to the constructor are used to initialize the corresponding fields of the item. ALGORITHM: Step 1: Define an abstract class Shape. Step 2: Add two protected instance variables of type int named x and y. Step 3: Create a parameterized constructor that initialises the instance variables x and y and accepts two parameters of type int, x and y. Step 4: Define an abstract class Shape. Step 5: Add two protected instance variables of type int named x and y. Step 6: Create a parameterized constructor that initialises the instance variables x and y and accepts two parameters of type int, x and y. Step 7: Define a class Circle that extends Shape. Step 8: Add a protected instance variable of type double named radius. Step 9: Define a parameterized constructor that takes three parameters of type int x, y, and double radius and initializes the x, y and radius instance variables using super() keyword. Step 10: Implement the abstract method getArea() by calculating the area of Circle. Step 11: Define a class Square that extends Shape. Step 12: Add a protected instance variable of type double named side. Step 13: Define a parameterized constructor that takes three parameters of type int x, y, and double side and initializes the x, y and side instance variables using super() keyword. Step 14: Implement the abstract method getArea() by calculating the area of Square. Step 15: Define a class Main. Step 16: Define a static method named main() which is the entry point of the program. Step 17: Create a Circle object using parameterized constructor. Step 18: Print the area and position of the Circle object using getArea() and printPosition() methods respectively. Step 19: Create a Square object using parameterized constructor. Step 20: Print the area and position of the Square object using getArea() and printPosition() methods respectively. Step 21: End of the program. Implementation: The implementation of the above steps mentioned below FileName: ParameterizedMain.java Output: Area of circle is: 78.53981633974483 The position: (2, 3) Area of square is:16.0 The position: (5, 7) 3. Copy constructor: copy constructor is used to create a new object with the equal values as an existing object (i.e., the item is created earlier than). It is useful while we need to create a new object that may be a replica of an object that is already present/existed. Copy constructor is defined with only one argument or one parameter that is an item of identical class. Then the constructor creates new object with same values as a parameter object. ALGORITHM: Step 1: Declare an abstract class with instance variables and default constructor. Step 2: Define a copy constructor with a parameter of the identical class type. Step 3: In the copy constructor, call the superclass copy constructor using the super keyword to copy the instance variables from the parameter object to the new object. Step 4: Assign the values of any extra instance variables within the subclass to the new item. Step 5: Implement the abstract method to calculate the area. Step 6: Define any other methods as necessary. Step 7: In the main function, create an object of the class. Step 8: Set the position and any other instance variables as necessary. Step 9: Create a new object the use of the copy constructor and passing the original item as a parameter. Step 10: Print the area and position of both the original and copied objects. Implementation: The implementation of above steps is given below FileName: CopyMain.java Output: Original Area of circle: 78.53981633974483 Position: (2, 3) Copied Area of circle: 78.53981633974483 Position: (2, 3) Original Area of square: 16.0 Position: (5, 7) Copied Area of square: 16.0 Position: (5, 7) |
Generally, all the users need to enter a username and password to log in to any application. Otherwise, the application page will not open. SAML stands for Security Assertion Markup Language. To understand what SAML is, we need to know what SSO is. SSO (Single Sign-on) Single sign-on...
17 min read
Problem Statement Finding the best route to draw a vertical line through a brick wall so that it intersects the minimum number of bricks is the foundation of the Minimum Number of Bricks that Can Be Intersected issue. A 2D list of numbers is used to represent...
4 min read
In Object-Oriented Programming, Array is a data structure that stores homogenous data in a linear way. The size of the array is fixed, i.e., once declared, the size of the array cannot be modified. In other words, array stores data of the same type (int, float, string,...
8 min read
In this tutorial, we are going to learn about the semiprimes numbers in Java. A n is said to be the semiprime number if the number n can be written as the product of two prime numbers. We will be exploring in this tutorial how to...
5 min read
When it comes to text formatting and manipulating strings in Java, there are certain characters that play a crucial role. One such character is the line feed character. In Java, the line feed character is represented by the escape sequence "\n". It may seem like a...
4 min read
In the world of programming, null values have long been a source of frustration, causing NullPointerExceptions that can crash applications and lead to unexpected behavior. To address this issue, Java introduced the Optional class in Java 8, providing a container type that either contains a non-null...
4 min read
The " ^ " symbol in Java represents the XOR logical operator, which performs a logical exclusive OR operation between two boolean values. If one of the operands is true and the other is false, this operator returns true; otherwise, it returns false. The XOR operator is...
3 min read
Java multicasting, also known as typecasting multiple times, refers to the process of applying several typecasting operations sequentially on a variable. This often occurs in scenarios where data types are incompatible but need to be transformed to make the code functional. Multicasting is especially useful in object-oriented...
4 min read
In software development, processing files is a frequent job that can become inefficient when managing several files or huge files. Multithreading is a crucial approach for speed optimization since it allows several threads to carry out work simultaneously. We shall examine multithreaded file processing in Java...
5 min read
In Java, System.out.print() and System.out.println() are two methods defined in the System class to send output to the console. They look and sound similar, but they behave differently in terms of cursor movement and formatting of the output. Java System.out.print() Method The System.out.print() method prints the specified...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India