c# - Related to the oops concepts -


i having basic question can annoy comes in mind when in started reading visual c# e-book.

they have mentioned that:: surprisingly, circle class of no practical use. default, when encapsulate methods , data inside class, class forms boundary outside world. fields (such radius) , methods (such area) defined in class can seen other methods inside class not outside world—they private class. so, although can create circle object in program, cannot access radius field or call area method, why class not of use—yet! however, can modify definition of field or method public

and circle class given

class circle {     int radius;      double area()     {         return math.pi * radius * radius;     } } 

so, private fields not accessible when tried in console project , running successfully. have main function in class , that's why can private fields accesed object of program class?

class program      {        int number;        static void main(string[] args)         {             program objprogram = new program();             objprogram.number = 10;             console.readline();          }    } 

you can access private fields within same class - any instance of class can access private fields of any other instance.

so can create program , access private fields within program.main.

if tried method in class, wouldn't work.

class program  {    int number;     static void main(string[] args)    {         program objprogram = new program();         objprogram.number = 10;                // works fine         console.readline();    } }  class someotherclass {    void somemethod()    {         program program = new program();         program.number = 10;                  // not compile!    } } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -