I am trying to understand access modifiers but i am a bit confused with how to use internal
.
I have class in a namespace that looks like this:
namespace Grids
{
public abstract class Grid
{
internal abstract Vector2Int ToGrid(float worldX, float worldY);
internal Vector2Int ToGrid(Vector3 worldPoint) => ToGrid(worldPoint.x, worldPoint.z);
internal Vector2Int ToGrid(Vector2 worldPoint) => ToGrid(worldPoint.x, worldPoint.y);
}
}
This is then implemented in an inherited class like so:
namespace Grids
{
public class RectGrid : Grid
{
public int Width;
public int Height;
public Grid(int w, int h)
{
Width = w;
Height = h;
}
internal override Vector2Int ToGrid(float worldX, float worldY)
{
int col = Mathf.FloorToInt(worldX / Width);
int row = Mathf.FloorToInt(worldY / Height);
return new Vector2Int(col, row);
}
}
}
So i now make a class to use this Grid thats not part of the namespace:
using Grids;
using UnityEngine;
public class MapGenerator : MonoBehaviour
{
private RectGrid _rectGrid;
void Awake() {
_rectGrid = new RectGrid(1,1);
_rectGrid.ToGrid(Vector3.zero); // why do i have access to this function
}
}
Yet for some reason i have access to the functions which are suppose to be internal:
Why is this ? I don't want to expose this function i want it only accessible to my Map
class which shares the same Grids
namespace and will control what i do expose. Yet my MapGenerator
class has access without even being part of the namespace?
Have i misunderstood how internal works here?