difference between protected in C++ and protected in Java

In C++, protected members cannot be accessed outside the class and its derived classes using obj.membername

#include <QtCore>
//#include "windows.h"

//#include <QApplication>

class A
{
public:
    void fun()
    {
        qDebug()<<"a";
        A a;
        a.aa=123;
        this->aa=123;
        A::aa=123;
    }
protected:
     int aa;

};

class B
{
public:
    void fun()
    {
        A a;
        a.aa=123; //illegal
    }
};

class AA:public A
{
    void fun()
    {
        //A*pa=new A;
        A::aa=123;
        this->aa=123;
        aa=123;
        AA aa;
        aa.aa=123;

        A a;
        a.aa=123; //illegal
    }
};

int main(int argc, char *argv[])
{
    A a;
   // a.a=123;
    a.aa=123; //illegal
   // A::aa=123;
    return 0;
}

Protected members can be visited in the derived classes using the member name directly, parentclass::membername, this->membername, subclassobj.membername, but cannot be accessed using parentclassobj.membername.

Protected members in Java behavior differently due to the existence of package. They are like public members for non-derived classes in the same package. They can be accessed using obj.membername, while this is not true in C++.  For non-derived classes in different packages, they are not accessible. In derived classes in the same package, they can be accessed using parentclassobj.membername(which is not true in C++), subclassobj.membername, membername itself. For derived classes that are not in the same package, they can be accessed by membername, subclassobj.membername, but cannot be accessed using parentclassobj.membername. So the cross-package access for protected members in Java keeps the same as  in C++.

//App.java
package fruit;

public class App 
{
  String a="aa";
  protected void fun()
  {
    System.out.println("in App");
  }
}

class App1
{
  String a="aa";
  void fun()
  {
    App app=new App();
    app.fun();
    
    System.out.println("in App1");
  }
}

class App2 extends App
{
  String a="aa";
  void fun1()
  {
    App app=new App();
    app.fun();
    fun();
    
    App2 app2=new App2();
    app2.fun();
    System.out.println("in App2");
  }
}

 

//Test.java
import fruit.App;
class Test extends App
{
  String a="aa";
  public static void main(String[] args)
  {
    App app=new App();
    //app.fun();
    //fun();
    System.out.println("hello");
  }
  void fun2(/*App app*/)
  {
    fun();
    Test app=new Test();
    app.fun();
    App ap=new App();
    ap.fun();//illegal
  }
}

 

 

 

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:

Comments are closed, but trackbacks and pingbacks are open.