Computer Science, asked by waleedkhanpti70, 1 month ago

what is the main difference between constructor of a class and functions we use in same class.

AND write one simple program where constructor is called and execute of functions from the object of a class.​

Answers

Answered by dreamrob
1

Answer:

Constructor:

  • Constructor name and the class name should be same.
  • Constructor do not have a return type.
  • Constructors are automatically called when we create the object of the class.

Function:

  • Function name and class name should not be same.
  • Function have a return type.
  • Functions are forcefully called.

Program in Java:

import java.util.*;

public class MyClass

{

   double r, A;

   MyClass(double rr)

   {

       r = rr;

   }

   void area()

   {

       A = 3.14 * r * r;

       System.out.print("Area of the Circle : " + A);

   }

   public static void main(String args[])

   {

       Scanner Sc = new Scanner(System.in);

       MyClass obj = new MyClass(10);

       obj.area();

   }

}

Similar questions