access-specifier

Why is PHP private variables working on extended class?

不打扰是莪最后的温柔 提交于 2021-02-02 09:26:21
问题 Shouldn't it generate error when i try to set the value of a property from the extended class instead of a base class? <?php class first{ public $id = 22; private $name; protected $email; public function __construct(){ echo "Base function constructor<br />"; } public function printit(){ echo "Hello World<br />"; } public function __destruct(){ echo "Base function destructor!<br />"; } } class second extends first{ public function __construct($myName, $myEmail){ $this->name = $myName; $this-

Why protected method is not accessible from subclass?

浪尽此生 提交于 2020-12-01 06:54:10
问题 Consider the following code snippets: package vehicle; public abstract class AbstractVehicle { protected int speedFactor() { return 5; } } package car; import vehicle.AbstractVehicle; public class SedanCar extends AbstractVehicle { public static void main(String[] args) { SedanCar sedan = new SedanCar(); sedan .speedFactor(); AbstractVehicle vehicle = new SedanCar(); // vehicle //WON'T compile // .speedFactor(); } } SedanCar is a subclass of AbstractVehicle which contains a protected method

Why protected method is not accessible from subclass?

狂风中的少年 提交于 2020-12-01 06:48:48
问题 Consider the following code snippets: package vehicle; public abstract class AbstractVehicle { protected int speedFactor() { return 5; } } package car; import vehicle.AbstractVehicle; public class SedanCar extends AbstractVehicle { public static void main(String[] args) { SedanCar sedan = new SedanCar(); sedan .speedFactor(); AbstractVehicle vehicle = new SedanCar(); // vehicle //WON'T compile // .speedFactor(); } } SedanCar is a subclass of AbstractVehicle which contains a protected method

Ruby Class#new - Why is `new` a private method?

瘦欲@ 提交于 2020-01-31 19:06:05
问题 I made a Matrix class and I want to use it in various parts of my code. class Matrix def initialize(x, y, v=0) @matrix = Array.new (0..y).each do |j| @matrix[j] = Array.new (0..x).each do |i| @matrix[j][i] = v end end end end When this code is included in the same class as the code that uses it, everything runs fine. When I move this code to lib/matrix.rb and require it, I get the following error: ./phylograph:30:in `block in run': private method `new' called for Matrix:Class (NoMethodError)

Why should I declare implemented interface methods as “public”?

…衆ロ難τιáo~ 提交于 2020-01-24 20:46:05
问题 interface Rideable { String getGait(); } public class Camel implements Rideable { int weight = 2; String getGait() { return " mph, lope"; } void go(int speed) {++speed; weight++; int walkrate = speed * weight; System.out.print(walkrate + getGait()); } public static void main(String[] args) { new Camel().go(8); } } Upon compiling the above code I've got a compilation error, related to access modifier of getGait() method. Please explain, why should I declare getGait() with public access

“Personal” method in ruby

∥☆過路亽.° 提交于 2020-01-24 07:44:56
问题 I'm looking for a way of making a method "personal" - note NOT PRIVATE to a class here is an example - by "personal" I mean the behaviour of method "foo" class A def foo "foo" end end class B < A def foo "bar" end end class C < B end a=A.new; b=B.new;c=C.new I'm looking for a way of producing the following behaviour a.foo #=> "foo" b.foo #=> "bar" c.foo #=> "foo" (ultimate base class method called) 回答1: Instead of creating 'personal' methods, change your inheritance structure. It appears that

Why == overloading can access private members of argument [duplicate]

流过昼夜 提交于 2020-01-20 23:01:50
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: why private value of the obj can be changed by class instance? Consider the following (partial) code: class Group { private: int id; public: void set_id(int); int get_id(); bool operator==(const Group&); }; bool Group::operator==(const Group& g) { if(g.id == this->id) { /* id is private? */ return true; } return false; } The code compiles and results seem proper. However, in the if part of the operator

Are the private members of superClass inherited by a subClass… Java?

拈花ヽ惹草 提交于 2020-01-13 05:42:31
问题 I have gone through this: Do subclasses inherit private fields? But I'm still confused... I'm talking about inheriting only and not accessing. I know that they aren't visible out side class. But does the subclass' object has it's own copies of those private members in super class? For example... class Base { private int i; } class Derived extends Base { int j; } Now, Base b = new Base(); Derived d = new Derived(); size of int is 4 Now, Will the size of b be 4 and size of d be 8 or size of d

C++ Is private really private?

时间秒杀一切 提交于 2020-01-02 00:14:33
问题 I was trying out the validity of private access specifier in C++. Here goes: Interface: // class_A.h class A { public: void printX(); private: void actualPrintX(); int x; }; Implementation: // class_A.cpp void A::printX() { actualPrintX(); } void A::actualPrintX() { std::cout << x: } I built this in to a static library (.a/.lib). We now have a class_A.h and classA.a (or classA.lib) pair. I edited class_A.h and removed the private: from it. Now in another classTester.cpp: #include "class_A.h"

When should we consider using private or protected?

痴心易碎 提交于 2020-01-01 08:43:30
问题 Just wondering, when should we actually must use private or protected for some methods in the model? Sometimes I can't not be bothered to group my methods in private nor protected . I just leave it as it is. But I know it must be a bad practice, otherwise these 2 groupings won't be created in programming. Thanks. 回答1: If you plan to call a method externally, record.method() , then "public" If it will be used only internally, self.method() , then "private" If you plan to use it internally, but