C++ Granting Access - class

in C++,
While granting access demoting a variable from protected to public is not allowed but it is happening.
#include<iostream>
using namespace std;
class base {
protected: int x; // x is protected
};
class derived: private base {
public: base::x; //demoting from protected to public must not happen
};
int main(){
derived d1;
d1.x=10; //protected variable x is being accessed using an object**
cout<<d1.x<<endl;
}

The int object is not what is protected, merely the name base::x
You are incorrect in your claim "protected variable x is being accessed". It is not. derived::x is a public member, which merely refers to the base::x.
The public and protected members of base are visible to derived, which is where the protected variable base::x is being accessed, by the access declaration.

Related

Java program compilation failed

While studying interface I came along this weird behaviour
When I am running this
int num=20;
public void sound();
public void eat();
}
class Dog implements Animal{
public void sound(){
System.out.println("Wooof!!!!!!!");
}
public void eat(){
System.out.println("Food");
}
}
public class Main{
public static void main(String[]args){
Dog dog=new Dog();
dog.sound();
dog.eat();
System.out.println(Dog.num);
//System.out.println(Dog.num1);
}
}
It runs fine while If I declare a no static variable with same name i.e. num as of the one in interface like this
interface Animal{
int num=20;
public void sound();
public void eat();
}
class Dog implements Animal{
int num=10;
public void sound(){
System.out.println("Wooof!!!!!!!");
}
public void eat(){
System.out.println("Food");
}
}
public class Main{
public static void main(String[]args){
Dog dog=new Dog();
dog.sound();
dog.eat();
System.out.println(Dog.num);
//System.out.println(Dog.num1);
}
}
It gives this error Main.java:22: error: non-static variable num cannot be referenced from a static context
My question was since one from interface is static and is of class level why the child class i.e. Dog compilation fails when I declare a non static instance level variable.
The problem is your are trying to access Dog class into System.out.println(). Not the variable dog where the class has been initialized.
If variable num is not static is impossible to read it without initialize the class, so if you want to do Dog.num, that variable has to be static.
If you don't add the variable into Dog class, the value will be declared in the interface, but if the variable exists in the class, the compiler will try read that.

C# issue with class instantiation

I'm running a C# project on VS2019 with the following code structure:
In the Class1.cs file:
public class Class1
{
public class MyClass2 : Class2
{
...
}
private void RunAlgorithm<T>() where T : Class2, new()
{
T argInstance = new T();
...
}
static void Main(string[] args)
{
RunAlgorithm<MyClass2>();
}
}
In the Class2.cs file:
public class Class2
{
public Class2() { }
public string setParameters { get; set; }
}
I'm getting the following error for the line RunAlgorithm<MyClass2>();
'Class1.MyClass2' must be a non-abstract type with a public
parameterless constructor in order to use it as parameter 'T' in the
generic type or method 'Class1.RunAlgorithm()'
even if I change it to Public, the error persists
Well, minimally, it'll have to be protected so that MyClass can access it..
https://dotnetfiddle.net/XFeEdQ
public class Class1
{
class MyClass2 : Class2
{
}
private void RunAlgorithm<T>() where T : Class2, new()
{
T argInstance = new T();
}
public static void Main(string[] args)
{
new Class1().RunAlgorithm<MyClass2>();
}
}
public class Class2
{
protected Class2() { }
public string setParameters { get; set; }
}
So your "Class1.MyClass2
must have a public parameterless constructor" message is saying that your MyClass needs a constructor. Mine above has such a constructor even though it's not in the code; in the absence of the developer providing a constructor the compiler provides one that does nothing other than call the base parameterless constructor...
...which leads me to the next point; your MyClass2 extends Class2, and hence Class2's constructor needs to be accessible to it. While Class2's constructor is private, MyClass2's constructor can't call it. Every constructor on c# has to either call another constructor or a base constructor. If you don't specify which, the compiler will insert a call to base() for you, which will fail if the base constructor is inaccessible
For this all to work out you need a public parameterless constructor in MyClass2:
public MyClass2():base(){}
or without the base(compiler will add the base call)
or blank (compiler will add all of it)
and you need something that makes Class2's constructor accessible to MyClass2, ie declaring Class2's constructor as public or protected

C# how to override function with parameter

public class Arrow : MonoBehaviour {
public virtual void StopArrow(Transform p){
arrowRotation = transform.rotation;
isRelease = false;
rb.velocity = new Vector3(0, 0, 0);
transform.SetParent(p);
transform.rotation = arrowRotation;
}
}
public class ExplosiveArrow : Arrow {
override void StopArrow(Transform p){
base.StopArrow();
}
}
This gives me this error:
Assets/Script/ExplosiveArrow.cs(33,23): error CS0621:
`ExplosiveArrow.StopArrow(Transform)': virtual or abstract members
cannot be private
and
Assets/Script/ExplosiveArrow.cs(33,23): error CS0507:
ExplosiveArrow.StopArrow(UnityEngine.Transform): cannot change
access modifiers when overriding public inherited member
Arrow.StopArrow(UnityEngine.Transform)
Change override void StopArrow(Transform p){ to public override void StopArrow(Transform p){ in your child class and you're done.
Unlike C++, you can't change the access of an overridden method to private if it's marked as public in a parent class.
In C# if you don't declare the access modifiers of the variables and methods in your class, by default they are going to be private.
According Access Modifiers (C# Programming Guide):
The access level for class members and struct members, including
nested classes and structs, is private by default.
So as you can see in your code, Arrow has a public virtual void StopArrow but ExplosiveArrow has a override void StopArrow which by default is the same as private override void StopArrow.
So, if you declare an element as public in a parent class, then you cannot reduce the visibility of that variable/method. If you don't want to expose an element but still you want to be able to access in derived classes you should use protected.
Also...
Assuming only the code you posted, this base.StopArrow(); will also error.
StopArrow() doesn't exist in base class Arrow
You need base.StopArrow(p); there instead.

Public, private and protected access qualifiers for D classes

I'm a C++ programmer starting with D and I'm having some trouble understanding the access qualifiers for D classes. Consider the following example:
import std.stdio;
class Foo {
private void aPrivateMethod()
{
writeln("called aPrivateMethod");
}
protected void aProtectedMethod()
{
writeln("called aProtectedMethod");
}
public void aPublicMethod()
{
this.aPrivateMethod();
this.aProtectedMethod();
}
}
void main(string[] args)
{
Foo foo = new Foo();
foo.aPublicMethod(); // OK to call it from anywhere
foo.aPrivateMethod(); // Must not be allowed to call it outside Foo
foo.aProtectedMethod(); // Should only be callable from within Foo and derived classes
}
I would expect the previous code to fail compilation, since it is calling private and protected methods of class Foo in an external function. However, this is not the case, since the example above compiles and runs without errors or warnings on DMD v2.063.2. Clearly the keywords have different meaning from those of C++.
My questions are:
1) How to make a method and/or variable private to a class so that only the class in question can access it.
2) How to make a method and/or variable protected, so that only the class in question and its derived classes can access it.
the access modifiers are module/file level (only exception is protected)
to remove access to a class put it in its own mudule:
foo.d
import std.stdio;
class Foo {
private void aPrivateMethod()
{
writeln("called aPrivateMethod");
}
protected void aProtectedMethod()
{
writeln("called aProtectedMethod");
}
public void aPublicMethod()
{
this.aPrivateMethod();
this.aProtectedMethod();
}
}
main.d
import foo;
void main(string[] args)
{
Foo foo = new Foo();
foo.aPublicMethod(); // OK to call it from anywhere
foo.aPrivateMethod(); // compile error: Must not be allowed to call it outside foo.d
foo.aProtectedMethod(); // compile error: Should only be callable from within foo.d, Foo and derived classes
}
D has a slightly different meaning to terms public private and protected than C++
Private means that only members of the enclosing class can access the member, or members and functions in the same module as the enclosing class. Private members cannot be overridden. Private module members are equivalent to static declarations in C programs.
Package extends private so that package members can be accessed from code in other modules that are in the same package. This applies to the innermost package only, if a module is in nested packages.
Protected means that only members of the enclosing class or any classes derived from that class, or members and functions in the same module as the enclosing class, can access the member. If accessing a protected instance member through a derived class member function, that member can only be accessed for the object instance which can be implicitly cast to the same type as ‘this’. Protected module members are illegal.
Public means that any code within the executable can access the member.

Deserialization of ArrayList GWT

In my application I'm getting some data from a file located in the server. The data is stored in a text file (.obj), so I'm using an rpc to read the file and get the data. The file is read using a third party library http://www.pixelnerve.com/processing/libraries/objimport/ I'm sending the data to the client using ArrayLists, basicly I'm sending this: ArrayList[ArrayList[Vertex3dDTO]] where Vertex3dDTO is an serializable object with contains float parameters. ArrayList[Vertex3dDTO] is contained in another serializable class Face3dDTO, and ArrayList[Face3dDTO] is in the serializable class Group3dDTO.
package com.nyquicksale.tailorapp.shared;
import java.io.Serializable;
public class Vertex3dDTO implements Serializable {
float x,y,z;
public Vertex3dDTO(){
}
public Vertex3dDTO(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
}
}
public class Face3dDTO implements Serializable {
ArrayList<Vertex3dDTO> vL = new ArrayList<Vertex3dDTO>();
Vertex3dDTO normal = new Vertex3dDTO();
Vertex3dDTO color = new Vertex3dDTO();
public Face3dDTO(){
}
public Face3dDTO(ArrayList<Vertex3dDTO> v) {
for(Vertex3dDTO v3dDTO : v){
vL.add(v3dDTO);
}
updateNormal();
}
public class Group3dDTO implements Serializable {
ArrayList<Face3dDTO> fL = new ArrayList<Face3dDTO>();
String name;
public Group3dDTO(){
}
public Group3dDTO(ArrayList<Face3dDTO> f) {
for(Face3dDTO f3dDTO : f){
fL.add(f3dDTO);
}
}
}
Now, everything is working well in development mode, but when I tested the application in hosted mode, everything I receive as response is: //OK[0,1, ["java.util.ArrayList/4159755760"],0,7]
So, I've been checked some other questions and seems the problem is about deserialization, but I've not found anything concrete.
The question is what do I have to do to get the app working well in hosted mode?
To successfully use RPC, your object needs to implement Serializable and should also have a default no arg constructor
Have you made sure this is a serialization problem? You can write a simple RPC test method to pass an array list of your DTO's over the wire in hosted mode.
If I were to bet money on a guess, I would say the problem is those array lists are sent empty in hosted mode. The .obj file read could be the problem. Perhaps in hosted mode the path of file doesn't match as in dev mode(different server configurations perhaps?), since file operations are in a try catch block an exception is most likely swallowed.
Long word short, Did you make sure those array lists are not sent empty in hosted mode?
Your object may well be Serializable, but that doesn't equate to something usable by Remote Procedure Calls. You need to implement Serializable, have a default contructor with no arguments (that calls super() if necessary), and a serial version ID, like so:
public class MyObject implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1796729355279100558L;
private Float someValue;
public MyObject() {
super();
}
public MyObject(Float someValue) {
super();
this.someValue = someValue;
}
public Float getSomeValue() {
return someValue;
}
public void setSomeValue(Float someValue) {
this.someValue = someValue;
}
}