All Questions
Tagged with visibility oop
60
questions
0
votes
1
answer
742
views
why can't I access a protected method from a private method if the protected ones are inherited by the subclasses?
I do not understand this particular case or what Alexander says.
class instance is not the same as class?
As already Alexander Larikov said that you can't access protected
methods from class instance ...
1
vote
2
answers
2k
views
Private property in parent class can be accessed and re-assigned through the instance of child class in PHP but protected one can't
I am very confused why $produk1->harga = 500; can still make a change
(or re-assign the value 500) to private $harga property despite private $harga in class Produk has PRIVATE visibility ? $...
7
votes
5
answers
242
views
Is it possible to use a class declared in Implementation section from Interface section
If I understand correctly, the interface section is visible to other units, and the implementation section is visible only in the current .pas file.
I have two classes, class TA should be visible to ...
1
vote
1
answer
146
views
Inheritance in C: how to hide "super class" attributes to the class extending it
As a learning experience I'm trying to achieve simple class inheritence in C, among other OOP features.
So far I've been able to properly hide "private" functions and attributes of a class from the ...
0
votes
2
answers
297
views
Method defined in superclass but implemented in subclass: accessing subclass attributes?
Here's my class structure:
class Parent {
public:
void foo(); // foo() defined in superclass
};
class Child : public Parent {
private:
int bar;
public:
Child(int);...
3
votes
1
answer
1k
views
Some C symbols in a C shared library are not exported even with explicit visibility
Preface
I'm developing an object oriented library in C that targets both Linux and Windows.
At the moment I'm developing on a Linux VM (Guest), because I'm using clang sanitizers.
I'm building with ...
1
vote
2
answers
141
views
Can I use packages without making all of the methods/ attributes public?
I have a project with about 10 classes. I moved some classes to a package for clarity. Is there a safe way to connect between classes without data leaks? -using public is dangerous because anyone can ...
4
votes
1
answer
814
views
Why can child override and access its parent private method?
I'm used to put method as protected in my PHP classes. But playing with private I'm starting to have doubts.
I know that it might be a duplicate but I can't find clarity from the SO-answers I've ...
0
votes
1
answer
535
views
Parent's protected properties inaccessible to child object
I have a parent class I'll be calling 'ParentClass' and a child class (that extends from it) which I'll be calling 'ChildClass'.
ParentClass has protected properties $prop1 and $prop2 which I want ...
1
vote
2
answers
61
views
Java: protected, visibitily [duplicate]
I'm a bit confused about the whole protected thing in java. If something is protected only the classes within the same package can access it, right?
Should we use protected private attributes in a ...
0
votes
4
answers
742
views
How to make a member of a class to be accessible only in subclasses in any packages?
How to make a member of a class to be accessible only in subclasses in any packages? Protected is not a solution since it will open the member to other non subclasses classes.
2
votes
3
answers
2k
views
PHP: Changing default visibility of undeclared class properties to protected/private
In PHP, undeclared class properties/variables will default to "public" visibility.
Is there a way (for certain classes, but not all) to change the default visibility to "protected" (or private)?
I ...
0
votes
1
answer
130
views
Is it possible to truly limit access of this private property?
I'm using three tricks in my attempt:
Reflections can't be used with dynamic class properties
__get() or __set() must be called when accessing dynamic class properties
debug_backtrace() can be ...
-1
votes
1
answer
67
views
Strange PHP Reaction
I'm getting some wierd reactions when running this script.
<?php
error_reporting(E_ALL);
class A {
protected $varOne = array(
"subVar1" => "",
"subVar2"...
6
votes
2
answers
2k
views
What's the visibility of a class by default in PHP?
I could find the default visibility of a property and a method in the PHP manual. But i couldn't find any info regarding the class itself.
My guess is that it's public. But if someone could link to ...
3
votes
1
answer
128
views
Making an instance method inaccessible from a subclass
Given:
class Base
def foo
puts 'foo!!!'
end
end
class Ball < Base
end
I want:
Ball.new.foo to return an error: No Method Found.
Base.new.foo to return: foo!!!.
My attempts are:
I ...
-1
votes
2
answers
66
views
Visibility of class variables in parent class
I have a class Player.java which extends Entity.java. In entity I define the x and y coordinates. In Game.java I create a player object: Player player = new Player(0, 0);. What should the visibility ...
0
votes
0
answers
39
views
Visibility of a method in JAVA [duplicate]
I am blocking on this question :
Is every instance of Y able to call the method aMethod() of every other instance of Y.
The answer I found is NO.
package a;
class X{
protected void ...
4
votes
3
answers
2k
views
What is the difference between the keyword 'Private' and 'Final'?
Having a confusion between Private and Final in PHP.
For example I have 3 classes:
Class A (parent class)
Class B (child class)
Class C (other class)
What I understand:
A: Public variables and ...
2
votes
2
answers
560
views
How do you override self?
I want to build class that implement a lot of methods based on it's id,
so I want to have one class parent that implement the methods!
and when i want to use this class I will extend it and just ...
4
votes
4
answers
2k
views
How can protected property of a class be visible from a static method in PHP?
I understand OOP. What I understand so far is that private and protected cannot be referenced from outside the class using $this->blah notation. If that is correct, how can the following code work?
&...
0
votes
1
answer
38
views
PHP providing parameter visible only inslide class
I might not grasp nature of PHP OOP system so that's maybe not impossible but let me ask is there a way to create parameter that's accesible only by methods of this class?
I want to craete 'local' ...
6
votes
5
answers
2k
views
How can a public method return a private type?
Why is it possible to return a private nested class from a public method in a public class? Shouldn't the compiler complain about the return type's visibility being less than the method?
public final ...
1
vote
1
answer
925
views
can classes have visibility to its namespace only
Is it possible to declare a class to be only visible or usable by other classes inside the same namespace?
example:
namespace Dirtyredz;
class BikeStore{}
private Class Bike{}
I want Bike to only ...
3
votes
1
answer
152
views
How to access this parameter of an object
So, I have two classes. One class, called Data, and another class, called Part. (It is for an inventory tracking program). Data handles the serialization of info and processing of data for operation ...
-2
votes
1
answer
557
views
Cause of the error: “Using $this when not in object context protected function”
I get the following error when using the following class in my code:
Using $this when not in object context protected function
Here is my code:
<?php
class Sign {
private $db;
private $...
4
votes
1
answer
5k
views
Private method overriding and visibility
I'm having a hard time trying to understand the output of the following code:
class Bar
{
public function test() {
$this->testPublic();
$this->testPrivate();
}
...
0
votes
1
answer
875
views
Accessing child property from Parent class in php
I come from java background and recently testing some OOP methodology in PHP. I found that it is permitted for a parent class to use Child class property. The code below describes what I am trying to ...
-2
votes
3
answers
98
views
Object not visible in own class PHP 5 OOP [closed]
I have a problem with a "Call to a member function on a non-object" - Error.
The field $other_class is not setable for future operations.
How can I fill and use the object $other_class? Thx.
$...
3
votes
2
answers
1k
views
Access Sibling Class Method
I'm working on a basic MVC for practice, but I get this error:
Fatal error: Call to a member function run() on a non-object in Router.php on line 5
What am I doing wrong?
Core:
<?php
class ...
0
votes
3
answers
670
views
PHP: Best practice regarding "public" visibility in OOP
Do you always add public beside methods and properties inside your classes? Or do you leave it out?
1. Option 1, without public:
<?php
class SimpleClass {
// property declaration
$var = '...
1
vote
2
answers
78
views
Restricting public accessable members - workaround
I want to break my applications model in the logical parts 'Data', 'Commands' and 'Engines'. Everyone outside the application should get a read-only access to the data and access to the commands. With ...
10
votes
4
answers
10k
views
How can I hide a class in C++?
Let's say I have 2 classes that I want to be visible (within a given header file) and one class that is their ancestor, which one I want to be visible only to the previously mentioned two. How can I ...
2
votes
4
answers
13k
views
Calling a child method from the parent class in PHP
Having the following class hierarchy:
class TheParent{
public function parse(){
$this->validate();
}
}
class TheChild extends TheParent{
private function validate(){
...
2
votes
3
answers
6k
views
How do I scope a class only to be used inside the Class Library project?
I have a class library with some public classes, and many internal utility classes. These utilities should not be visible/usable to people who link against the DLL.
namespace MyProject {
public ...
1
vote
4
answers
394
views
php oops: visibility
I'm revising my OOPs concepts in PHP. I have a simple php code here for practising visibility.
When i declare $name as private in Big_Animal,
1)Why it doesn't throw an error when i try to assign a ...
7
votes
5
answers
6k
views
PHP Visibility with Magic Methods __get & __set
I recently went to an interview and my code I supplied had magic functions to get and set variables. My code was as follows:
public function __get($name){
try {
return $this->$name;
...
14
votes
4
answers
9k
views
Why does PHP allow protected and private methods to be made public via an override in subclasses?
From some brief fiddling about, I find I get an error when overriding superclass methods in a subclass when I do the following:
Override a protected superclass method with a private subclass method
...
0
votes
3
answers
162
views
OOP Visibility, in general and specifically in PHP
This is probably a general OOP question, but I'm using PHP, so if it exists in general, I'd like to know, but give me warning if you know it's missing or warped in PHP.
Is it possible for a class ...
1
vote
2
answers
6k
views
Accessing protected variables in PHP
I'm trying to access protected variables in a second-child class extended from its parent, but every time I try to access them, they are NULL.
What's weird to me is that I can access the parent's ...
45
votes
12
answers
16k
views
Why can attributes in Java be public?
As everybody knows, Java follows the paradigms of object orientation, where data encapsulation says, that fields (attributes) of an object should be hidden for the outer world and only accessed via ...
0
votes
1
answer
50
views
With two classes extending another, how to avoid sharing an attribute
I have the following classes.
The problem is that $_instance is getting shared amongst both AuthFactory and UserFactory.
class Factory {
protected static $_instance;
/*...*/
}
class ...
44
votes
12
answers
2k
views
Why is the amount of visibility on methods and attributes important?
Why shouldn't one leave all methods and attributes accessible from anywhere (i.e. public)?
Can you give me an example of a problem I can run into if I declared an attribute as public?
2
votes
3
answers
498
views
JavaScript var visibility
Given this code:
function MyClass() {
var v = '1';
this.hi = function() {
console.log('Value of V is ' + v);
var v = '2';
console.log('Value of V is ' + v);
...
1
vote
8
answers
5k
views
Access private data member from a subclass
I would like to declare a data member of a superclass, private:
public abstract class superclass {
private int verySensitive;
abstract int setVerySensitive(int val); // must be overriden by ...
0
votes
1
answer
569
views
Ruby Protected Methods Issue
I have a simple Ruby base class where all the methods need to have protected visibility. The problem arises when another class inherits the base class and calls its methods. The Ruby interpreter ...
43
votes
8
answers
15k
views
Reasons to use private instead of protected for fields and methods
This is a rather basic OO question, but one that's been bugging me for some time.
I tend to avoid using the 'private' visibility modifier for my fields and methods in favor of protected.
This is ...
2
votes
3
answers
2k
views
Variable visibility in subclass
This is a very simple question, and I apologize for being so noobish :/
I'm new(ish) to Java. I've got a game on the Market that works quite well, but the code is all rubbish. Basically, I wrote it ...
10
votes
3
answers
12k
views
PHP Inherited parent method can't access child's private property
First of all: A quite similar problem has been posted and somehow solved already, but is still not answering my specific problem. More about this later.
In words: I have a base class which provides ...
5
votes
2
answers
696
views
Visibility of object constants
I found out that object constants in PHP always have public visibility so it is not possible to set them to protected or private like this:
<?php
class MyClass {
protected const constant = "...