0% found this document useful (0 votes)
25 views2 pages

Car

Access modifiers in programming define the accessibility of classes, methods, and properties. There are 4 access modifiers: public, private, protected, and internal. Public members are accessible everywhere, private members only within the same class, and protected/internal within derived classes or the same assembly. For example, a private field can only be accessed within its class, while a public field can be accessed by any other class.

Uploaded by

ivan Imbong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Car

Access modifiers in programming define the accessibility of classes, methods, and properties. There are 4 access modifiers: public, private, protected, and internal. Public members are accessible everywhere, private members only within the same class, and protected/internal within derived classes or the same assembly. For example, a private field can only be accessed within its class, while a public field can be accessed by any other class.

Uploaded by

ivan Imbong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Access Modifiers are keywords that define the accessibility of a member,

class or datatype in a program. These are mainly used to restrict unwanted data
manipulation by external programs or classes. There are 4 access modifiers
(public, protected, internal, private) which defines the 6 accessibility levels as
follows:
public,protected, internal, protected internal, private, private protected

Private
If you declare a field with a private access modifier, it can only be accessed
within the same class:

Access is only granted to the containing class. Any other class inside th
e current or another assembly is not granted access to these members

working!!
using System;
class Car

{
private string model = "Mustang";

static void Main(string[] args)


{
Car myObj = new Car();

Console.WriteLine("What type of car do you want?");


Console.WriteLine(myObj.model);
}
}

Fail!!

using System;
class Car
{
private string model = "Mustang";
}

class Program
{
static void Main(string[] args)
{
Car myObj = new Car();

Console.WriteLine("What type of car do you want?");


Console.WriteLine(myObj.model);
}
}

Public Modifier
If you declare a field with a public access modifier, it is accessible for all
classes:
Access is granted to the entire program. This means that another method or another
assembly
which contains the class reference can access these members or types. This access
modifier has
the most permissive access level in
comparison to all other access modifiers.

using System;
class Car
{
public string model = "Mustang";
}

class Program
{
static void Main(string[] args)
{
Car myObj = new Car();

Console.WriteLine("What type of car do you want?");


Console.WriteLine(myObj.model);
}
}

class Car
{
public string model = "Mustang";
public int number = 1;
}

class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Car myObj2 = new Car();

Console.WriteLine("What type of car do you want?");


Console.WriteLine(myObj.model);
Console.WriteLine(myObj2.number);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy