Real example for access identifiers [public, protected, private]

I am new to OOP.

I know that there are three identifiers, and they should be used in different situations. I also read a lot of discussions about whether it is dangerous to use a "public" identifier too causally. But I do not understand why this is dangerous.

Say I have a Windows application or a web application. In these applications, I declared some public methods or variables, how dangerous is that? I mean, my applications accept user input and then output, so how can this be dangerous? How other or other programs can attack or take advantage of, or in some way harm the application due to a "public" identifier.

Can someone describe an example of real life? Thank.

+3
source share
5 answers

Here is your answer: https://softwareengineering.stackexchange.com/questions/143736/why-do-we-need-private-variables

In short, it is a matter of preserving complexity and reuse in the long run. By ensuring that the variables are private to the class, you can prevent other programmers (or even your future self) from changing anything important that affects the interiors of your classes.

, , , , , . : - . , , .

. .

+1
Modifier    | Class | Package | Subclass | World
β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”
public      |  βœ”    |    βœ”    |    βœ”     |   βœ”
β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”
protected   |  βœ”    |    βœ”    |    βœ”     |   ✘
β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”
no modifier |  βœ”    |    βœ”    |    ✘     |   ✘
β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”+β€”β€”β€”β€”β€”β€”β€”
private     |  βœ”    |    ✘    |    ✘     |   ✘

Private:

,

, , private, .

. .

, private, , getter. , , , .

.

, , .

, .

Public

, , , .., , .

, , , , , Java Universe.

http://javarevisited.blogspot.com/2012/10/difference-between-private-protected-public-package-access-java.html

+6

- , Person.

- name, age, gender, weight, height, hairColor, eyeColor .. , , , "" . , eyeColor, ...

, , . , , , , .. , . "" "" .

, , , , . , , .

, , - , . , "private".

- .

  • public , , . , public , , "Overflow Prime", .

  • protected , , . , , Child extends Parent , Parent .

  • "package-private" " " - , . , public , - .

  • private - , . , , , .

, ?

, , - , , . , 600 79 999 , , ( ), .

encapsulation. , , - , .

+2

.

package com.oops;

public class Modifiers {
    public int a = 0;
    private int b = 0;
    protected int c = 0;
    int d = 0;

    public static void main(String[] args) {
        new TestPublic().getAndPrintA();
        new TestPrivate().cannotGetB();
        new TestProtected().getProtected();
        new TestDefault().getDefault();
    }
}

class TestPublic {
    public void getAndPrintA() {
        Modifiers modifiers = new Modifiers();
        modifiers.a = 10; // Public variables can be used from anywhere.
    }
}

class TestPrivate {
    public void cannotGetB() {
        Modifiers modifiers = new Modifiers();
        // modifiers.b; //Compile time error: The field Modifiers.b is not
        // visible
    }
}

class TestProtected {
    public void getProtected() {
        Modifiers modifiers = new Modifiers();
        modifiers.c = 10; // Visible here, but will not be visible to the
                            // outside world.
        // Protected means package and subclass
    }
}

class TestDefault {
    public void getDefault() {
        Modifiers modifiers = new Modifiers();
        modifiers.d = 10; // Visible here, but will not be visible to the
                            // outside world and subclass.
        // Default means only within package.
    }
}
0

, , . . : myarea

 package myarea;

    public class MyHome{
         private int frontDoorLock;
         public int myAddress;
         int defaultWifiPaswd;
    }

(/ ) , : myarea

package myarea;

public class MyBedroom{
public static void main(String[] args) {
       MyHome a = new MyHome();
       int v1 = a.myAddress; // works
       int v2 = a.defaultWifiPaswd; // works
       int v3 = a.frontDoorLock; // doesn’t work
    }
}

: neighbourArea

package neighbourArea;

import myarea.MyHome;

public class NeighbourHome{
public static void main(String[] args) {
       MyHome a = new MyHome();
       int v1 = a.myAddress; // works
       int v2 = a.defaultWifiPwd; // doesn’t work
       int v3 = a.privateVar; // doesn’t work
}

, Geeks Java-.

0

All Articles