Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP Access Modifiers: Compile-time or Run-time

I've heard that Access Modifiers Public, Private and Protected are just some Compiler Stuff, and they're not actually exists in the compiled Binary Code.

Now I'm wondering how much it's correct? And if it's correct, does it mean that Encapsulation is not exist in the binary code at run-time? So, if you modify the binary to access a Private method illegally, in theory, there isn't anything to check your rights, neither any OOP mechanism nor Operating System, right?

I've also tagged the question for both C++ and Java. I'm aware of the difference between them, just curious to see how different they handle Access Modifiers.

like image 848
Mahdi Avatar asked Mar 22 '13 06:03

Mahdi


People also ask

Is run time the same as compile time?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.

What is run time and compile time exception?

A compile-time error generally refers to the errors that correspond to the semantics or syntax. A runtime error refers to the error that we encounter during the code execution during runtime.

Which is faster runtime or compile time?

Basically if your compiler can work out what you mean or what a value is "at compile time" it can hardcode this into the runtime code. Obviously if your runtime code has to do a calculation every time it will run slower, so if you can determine something at compile time it is much better.

Is dynamic runtime or compile time?

Dynamic variableIt skips compile-time type checking. Instead, it checks the type at the run time & then assigns the right-hand side type to the left-hand side variable. Dynamic variable does not need to be initialized at the time of declaration because they are going to be resolved at runtime.


1 Answers

Access modifiers are solely a compile time mechanism in C++. In Java however, they are enforced at runtime too, because Java also has a runtime typesystem, and it can dynamically (at runtime) create classes. So it needs to enforce access at runtime too for types it doesn't know about at compile time.

Why use access modifiers?

The sole purpose of access modifiers is to enforce design.

Say you have a class A that you implemented

class A
{
public:
  void DoSomething()
  {
    // use private member mPrivMember to do something
  }
private:
  int mPrivMember;
}

And some code using class A:

A a_obj;

The above code can call a_obj.DoSomething(), however they don't have direct access to mPrivMember, so a.mPrivMember written outside class A will not compile.

Now, why would you want some members accessible to outside code and some not? Well, here is why: at the moment, the method DoSomething() uses mPrivMember to actually do stuff. But after some time, you might decide you want to refactor the code in DoSomething, to improve it. You found a different way to do something that doesn't involve using mPrivMember anymore. So you delete mPrivMember and reimplement DoSomething some other way.

Now, if there was code outside your class using mPrivMember, that code wouldn't compile anymore because you have deleted mPrivMember when reimplementing DoSomething. To prevent existance of such code, you restrict access to mPrivMember. The mechanism to do that is through access qualifiers such as private and protected.

This allows you to refactor code in the future without worrying that other code might use internal members.

Recap

public private and protected are compile time mechanisms in C++. They do not exist in the generated binary form of your program, and thusly such protections are not enforced. Anything is accessible from anywhere.

In Java however, classes can be created at runtime if I'm not mistaken. Which is the reason why it also has to check for access at runtime too so it can enforce them, thus Private Public and Protected do exist in a Java binary.

like image 135
Ed Rowlett-Barbu Avatar answered Sep 23 '22 17:09

Ed Rowlett-Barbu