C# Access
Write a class Lid with a private field int seal, a constructor Lid(int n) that sets seal = n;, and a public method int Read() that returns seal. In Pine, keep Main and build one lid with Lid L = new Lid(9);, then write L.Read(). Keep both types in the same teaching file so dotnet run stays simple. Run with dotnet run. The screen should show one line: 9. The field stays private to Lid. The public method is how Pine gets the number. Keep the starting public class named Pine.
Who can see a member
On the static page you called Cup.Mark() through a class name. Here the new words are private and public. A private field is visible inside its own class. Other classes in the same file — including Pine — cannot write L.seal when seal is private. A public method can still run from Pine and return that hidden value.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any Cup.Mark(); leftover if that static demo is still all you have in Main. Delete any Cup class leftover. Delete any Jar leftover. Delete any ReadLine line. The file you keep has no wait for typing.
Outside Pine’s braces — still in the same file — write public class Lid. Inside the braces declare private int seal;. Write the constructor Lid(int n) { seal = n; }. Then write public int Read() { return seal; }. There is no Main in Lid on this page. Save the file before you run.
The field needs private so Pine cannot touch seal by name. The method needs public so Pine can call Read. The return type is int because Read hands the stored number back to the caller. Keep return seal; inside the method body.
using System;
public class Lid
{
private int seal;
Lid(int n)
{
seal = n;
}
public int Read()
{
return seal;
}
}
Pine builds a lid and calls Read
Pine keeps public static void Main(). Inside Main, write two lines in order: Lid L = new Lid(9);, then Console.WriteLine(L.Read());. The constructor stores 9 in the private field. The call L.Read() returns that value. The write shows it. You are not writing Cup.Mark(); on this page. That was the static lesson.
using System;
public class Pine
{
public static void Main()
{
Lid L = new Lid(9);
Console.WriteLine(L.Read());
}
}
public class Lid
{
private int seal;
Lid(int n)
{
seal = n;
}
public int Read()
{
return seal;
}
}
Read the kept file from the top. using System; prepares short names. public class Pine is the starter. public static void Main() is where the runtime starts. The expression new Lid(9) builds a lid and stores 9 in seal. The call L.Read() enters the public method on that object. The body returns seal, so the value 9 comes back to Main. Then Main writes that return value and the screen shows 9.
Run it with dotnet run. The program writes one line and stops. It does not wait for you to type. If dotnet run waits, ReadLine is still in the file. Delete that line and keep new Lid(9) and Console.WriteLine(L.Read()); in Pine, with private int seal and public int Read() in Lid. The screen shows 9. If the screen shows 2, a leftover Cup tally demo may still be running. If the build fails on L.seal, you tried to read the private field from Main — switch to L.Read().
If you write Console.WriteLine(L.seal); while seal is private, the build refuses. That refusal is the lesson. Keep the public door: L.Read(). If you leave seal without private, default access in this simple shape can still compile from Pine in ways this page does not want to teach — put private on the field on purpose.
Compared with static Cup and with plain public fields
On the static page, Cup.tally was reachable through the class name after static work. This page hides seal from Pine and opens only Read. A leftover Cup.Mark(); Cup.Mark(); Console.WriteLine(Cup.tally); is a different lesson. For this page keep Lid and Pine and L.Read().
You could make every field public and write L.seal from Main. This lesson chooses private so the object owns the storage and Pine asks through a method. Writing Console.WriteLine(L); alone does not show a clean 9. Write Console.WriteLine(L.Read()); to see the returned number.
Two lids keep separate private seals
Each new Lid(...) gets its own private seal. You can write Lid a = new Lid(3); and Lid b = new Lid(8);, then write both a.Read() and b.Read(). The private keyword does not share one seal across objects. It only blocks other classes from naming the field.
using System;
public class Pine
{
public static void Main()
{
Lid a = new Lid(3);
Lid b = new Lid(8);
Console.WriteLine(a.Read());
Console.WriteLine(b.Read());
}
}
public class Lid
{
private int seal;
Lid(int n)
{
seal = n;
}
public int Read()
{
return seal;
}
}
Run again with dotnet run. The screen should show 3 on one line and 8 on the next. Each Read returned that object’s own private field. If both lines show the same number, check that you used two new Lid(...) calls with different arguments. If the screen still shows a single 9, you may still be on the one-lid demo — that is fine for the first checkpoint; use the two-lid file when you want the second check.
If the build complains about Read, confirm the method header includes public and int. If Main still calls Cup.Mark(), delete that static leftover so the access lesson can show lid numbers instead of a tally.
What this leaves for namespaces
This page uses private and public so one class can hide a field and still share a value through a method. A later page — namespaces — groups types under a name so larger programs stay organized. Stay with access here. Do not invent a namespace wrapper on this page unless you already know it; the lesson goal is private on the field and public on Read.
Do not nest Lid inside Pine’s braces. On this page the two classes sit as siblings in the file. If the build cannot find Lid while compiling Pine, check that public class Lid sits in the same file. If the screen shows 0, the constructor may be missing the store — keep Lid(int n) { seal = n; } and call Read after new.
You can declare private int seal;, write Lid(int n) { seal = n; } and public int Read() { return seal; }, and from Pine.Main run Lid L = new Lid(9);, then write L.Read(). Run with dotnet run. The screen shows 9. The private field stayed inside Lid. The public method handed the number to Pine. Think of private as a closed drawer and Read as the allowed way to look inside. Pine stays the door you open with dotnet run. The next page is namespaces, where types sit under a group name so bigger files stay clear.
Keep private on the field. Keep public on Read. Keep the call as L.Read() instead of L.seal. Those three habits are the checkpoint for this page before you move on. Leave leftover Cup.Mark() demos out of Main so the screen cannot show a tally by mistake.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top