problem about access-modifiers-Collection of common programming errors


  • Oli Charlesworth
    java class classloader access-modifiers
    I just came across this weird scenario while playing 🙂 Java’s documentation specifies that a file can contain all non public classes and the file name not matching any of the class. When one tries to run after compilation it will generate Exception even after one of the classes having main method specified. Explanation is needed as to how to execute this file’s main method.My test code is as follows in file named NoPublicClasses.java:class Class1{}class Class2{ public static void main(String[]

  • PeeHaa
    php oop access-modifiers
    I have very strong basic in Java, and when comes to PHP I got some problem with the OOP.Please take a look at this two classes below:Contacts.php<?php public class Contacts {} ?>UnitTest.php<?phprequire_once ‘PHPUnit/Framework.php’;include ‘Contacts.php’;class UnitTest extends PHPUnit_Framework_TestCase {public function testRead() {$temp = new Contacts();}} ?>When I trying to test the UnitTest.php using phpunit I got this error message:PHP Parse error: syntax error, unexpected T_PUB

  • HunderingThooves
    java access-modifiers
    I’ve been trying to learn more about access modifiers in java, and everyone has said “Use private to hide things that you don’t want other classes / methods using” and I’m just left wondering why that needs to be done. I can’t think of a single case where private would make sense to use as a sole developer…That issue extends to not mentioning an access specifier too. It seems like it’s just limiting the flow of information, since the user has no way of seeing the code that we’re generating.

  • Innominate
    java access-modifiers
    Fairly new to Java, but I’m wondering why package access is considered “more restrictive” than subclass access. That is, every access modifier which provides subclasses with access to a member also provides the whole package with access, and there are modifiers whic provide package access but not subclass access.Isn’t this totally backwards? Let’s say I have a class ControlledInstantiation in some package. If I have another class AlsoControlledInstantiation extends ControlledInstantiation, I am

  • Alan Stokes
    java reflection language-design access-modifiers
    Just wondering, why did the people who invented Java write methods like setAccessible(boolean flag), which makes the access-modifiers (specially private) useless and cannot protect fields, methods, and constructors from being reached? Look at the following simple example:public class BankAccount {private double balance = 100.0;public boolean withdrawCash(double cash){if(cash <= balance){balance -= cash;System.out.println(“You have withdrawn ” + cash + ” dollars! The new balance is: ” + balanc

  • Cracker
    c# access-modifiers
    What is the difference in the compiled code between a private or a protected and a public class member? Is there any check during runtime whether one can access the member or not? I am asking these questions in context of C#.

  • Bruce
    c++ inheritance polymorphism access-modifiers
    #include<iostream>using namespace std; class base { public:virtual void add() {cout << “hi”;} };class derived : public base { private:void add() {cout << “bye”;} };int main() {base *ptr;ptr = new derived;ptr->add();return 0; }Output is byeI dont have a problem with how this is implemented. I understand you use vtables and the vtable of derived contains the address of the new add() function. But add() is private shouldn’t compiler generate an error when I try to access it out

  • ipkiss
    java access-modifiers
    I have the following code:public class Library {public void myFunction() {// do something} } public class Book extends Library{protected void myFunction() { // Error here// do something}}The above code has error because the Book class is trying to override a function of the supper class with a weaker access modifier. I know this is the rule of Java. But I am curious why is that? What the problem it might cause?

  • gabe
    asp.net-mvc asp.net-mvc-3 viewmodel access-modifiers strongly-typed-view
    I have some internal automatic properties in my view model but my strongly-typed view doesn’t see them. Everything is in the same assembly, so why is this happening?public class MyViewModel {public int PublicProperty { get; set; }internal int InternalProperty { get; set; } }.@*My view*@ @model [email protected]@Model.InternalProperty @*Causes compilation error*@

  • SysDragon
    vb.net winforms access-modifiers
    I have been creating some controls during runtime like below:pnl1.Controls.Add(New TextBox() With _ {.Name = “ItemName”, _.Anchor = AnchorStyles.Left + AnchorStyles.Right _ }And I tried to set its modifier as Public. Unfortunately all of my attempts got failed. My question is: Can we set modifers for dynamically created controls? If not, then how could I access those controls in other forms? (Ex: Form1 having dynamically created control ctrl1, Form2 which is present inside of the MDI container

  • CSharpLearner
    c# .net reflection access-modifiers
    This question already has an answer here:Does reflection breaks the idea of private methods, because private methods can be access outside of the class?14 answersWe can use reflection to get or set value of any private member in C# class. (using BindingFlags.NonPublic etc.) If this is the case, how come private member is really private? How can we be assured that the things which are purposely stored in the private fields remain private in true sense?

  • pauldoo
    c# access-modifiers
    This question already has an answer here:How to make a property protected AND internal in C#?7 answersOn a public class in C# is there a way to make a field/method accessible only to derived classes within the same assembly?From what I understand protected internal in C# means the same as protected or internal (ie accessible to derived classes or classes from the same assembly), which is not what I need.

  • Lightness Races in Orbit
    c++ inheritance polymorphism access-modifiers
    Possible Duplicate:Why can i access a derived private member function via a base class pointer to a derived object? #include <iostream> using namespace std;class B { public:virtual void fn1(void) {cout << “class B : fn one \n”; }virtual void fn2(void) {cout << “class B : fn two \n”; } };class D: public B {void fn1(void) {cout << “class D : fn one \n”; } private:void fn2(void) {cout << “class D : fn two \n”; } };int main(void) {B *p = new D;p->fn1();p->fn2()

  • Louis Rhys

  • Arne Evertsson

  • MD.Unicorn
    c# winforms access-modifiers design-time
    private void referenceDesk_DoubleClick(object sender, EventArgs e) {tabControl1.TabPages.Add(new TabPage(“Donkey Kong”)); }there is no tabControl1.Modifier type command to use, and also can’t usenew public TabPage(“”);

  • Anto
    java reflection access-modifiers
    i try to set values for the field available in a java bean like the following and i want to omit the static final fields:public Class creatObjectWithDefaultValue(String className) throws IllegalArgumentException, IllegalAccessException {DefaultParamValues defaultParamValues = null;Class objectClass = null;try {objectClass = Class.forName(className);Field[] fields = objectClass.getDeclaredFields();for(Field f:fields){f.setAccessible(true);//if(!f.isAccessible()){// f.setAccessible(true);Class<

  • noobprogrammer
    c# database winforms datatable access-modifiers
    I am getting the following unhandled exceptionNullReferenceException was unhandled. Object reference not set to aninstance of an object.and something like warningField ‘Project3_MineSweeper.Form3.form2’ is never assigned to, and will always have its default value nullHere is the code on my DB.cspublic class DB{…public DataTable GetData(){string spName = “GetTime”;Connection.Open();SqlCommand command = new SqlCommand(spName, Connection);command.CommandType = CommandType.StoredProcedure;SqlDataR

  • sharptooth
    c++ inheritance hierarchy access-modifiers
    I found some rather strange code:class Base { public:virtual bool IsDerived() const { return false; } };class Derived : public Base { public:bool IsDerived() const { return true; } };Derived* CastToDerived( Base* base ) {// private and protected inheritance from Derived is prohibitedDerived* derived = dynamic_cast<Derived*>(base);if( derived == 0 ) {assert( !base->IsDerived() );}return derived; }I don’t get the passage about private and protected inheritance.Suppose, I inherit from Deri

  • FredOverflow
    c++ object-oriented inheritance access-modifiers
    Today I noticed that I basically never use protected methods in C++ code, because I rarely feel the need to call non-public methods of a parent. I do use protected in Java in the template method pattern, but since you can override private methods in C++, I don’t need protected there, either.So what are some real-world scenarios where I would want to use protected methods in C++ code?(Note that I’m not too fond of implementation inheritance in general, that might explain a lot…)

  • Ajai
    javascript oop design-patterns access-modifiers private-members
    I was reading about private state maintenance in javascript across files having same module.Say we have file1:var Module = (function(){//Some private variables declared.// and initialized.})();and we have file2:var Module = (function(myModule){//Here the same private variables are required.// And the following implementation enables it.var _private = myModule._private = myModule._private || {},_seal = myModule._seal = myModule._seal || function () {delete myModule._private;delete myModule._seal;

Web site is in building