The print statements prints the value of result on screen.Then method area() calculates result using values passed here as parameters and returns result to " result" variable.While generating object " ar" we are passing some values to constructor as 4 and 5,These values goes to AreaOfRhombus constructor definition and get assigned to " pDiagonal" and " qDiagonal" variable.We created an object of " AreaofRhombus" class.We created this class with main method.This class will execute and show the result on screen.In this class, ![]() The area() performs some calculations and returns the area.Constructor assigns the values of " p" and " q" to " pDiagnoal" and " qDiagona l" variables.The this keyword is used in reference to this class's variables.It is used to refer that variables which are following it belong to this particular class.Which means, this keyword is referring to AreaOfRhombus class. In the body of constructor, we are using this keyword.The parameterized constructor " AreaOfRhombus" is defined with two parameters " p" and " q".Write a program to caculate Area of Rhombus using parameterized constructor. This will illustrate the use of parameterized constructor. And param1, param2 are dummy names of parameters.You can give any name to them.In constructor body we can write statements to initialized variables of class. The dataype is the type of parameters like int,float,double,String etc. In the syntax, ConstructorName must be same as class name.In the parenthesis, define variables that are used by constructor. Public ConstructorName( dataype param1, datatype param2. A program that demonstrates parameterized constructors is given as follows. ![]() The parameterized constructors can be called implicitly or explicitly. ![]() Parameters are added to a parameterized constructor just like they are added to a normal function. In other words, we can initialize current class's variable from other class using parameterized constructor. The parameterized constructors can take arguments to initialize an object when it is created. This constructor set class variables to value of parameters passed from other class to it. The constructor which can contain parameters like normal method is known as Parameterized constructor.We can specify parameters in its definition within parenthesis (). In the last post, we discussed Default Constructor.This is very useful in making programs where we require to call other classes with some data.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |