Constructors are special methods named after the class and without a return type, and are used to construct objects. Constructors, like methods, can take input parameters. Constructors are used to initialize objects. Abstract classes can have constructors also.
Constructors are different from methods:
Types of constructor :
This Java program demonstrates the usage of constructors in a class to initialize the instance variables of an object when it is created.
//Constructor in Java class RectangleShape { int length, width; public RectangleShape() { System.out.println("Constructor Called"); length=2; width=10; } int area() { int a = length * width; return a; } } public class constructor { public static void main(String args[]) { RectangleShape o1 = new RectangleShape(); System.out.println("Area of Rectangle : " + o1.area()); } }
Constructor Called Area of Rectangle : 20To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions