Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello Don\'t answer using your hanwriter please Thank you Forum Discussion Descr

ID: 3838653 • Letter: H

Question

Hello

Don't answer using your hanwriter please

Thank you

Forum Discussion Descripton av discussion 052017 board Question board e me what is the member difference Properties various discussions of use class When Public functions between private public, (methods). you use variables, access for to a such it want variables levels class do e? as are member Example: setter However, that visible all variables, for a are to y or should nly the as privete class provide to class string ovhmer functions data, to they car which eaue but belong. wish to control it, you would Insert Prtsc Desete Home End Lock Pgup Poon Shift

Explanation / Answer

The main and the basic difference between private and public keywords are:-

private keyword: using the private keyword before the variable name means that that variable scope is within its class and it cant be accessed outside the class. It is generally the access specifier. The private access specifier is more restricted as it cant be accessed outside its scope of class.

we can define the private keywords before variables, methods or class.

if a variable, class or method is private that means it can only be accessed only within its class or scope of defination.

ex:-

private class Student{ //class private

private String name; //variable private

private int rollNumber;

private void display(){ //method private

System.out.println("name"+name+"roll number"+rollNumber);

}

}

so, if we access the class Student in other class it gives error since its scope is within the class.

if we access the private variable and method they cant be accessed outside Student class.

Public keyword.:- using the public keyword before the variable name means that, that variable scope is anywhere in any of the class and it can be accessed outside the class. It is generally the access specifier. The public access specifier is less restricted as it can be accessed anywhere outside or inside or any other package.

we can define the public keywords before variables, methods or class.

if a variable, class or method is public that means it can only be accessed anywhere in any of the class or scope of defination.

ex:-

public class Student{ //class public

public String name; //variable public

public int rollNumber;

public void display(){ //method public

System.out.println("name"+name+"roll number"+rollNumber);

}

}

so, if we access the class Student in other class or other package it can be easily accessed outside the class.

if we access the public variable and method they can be accessed outside Student class. Since public keyword variables, class or method can be accessed from anywhere or in any of the class.

i can understand that public access specifiers are less restricted and they are not secure while the private access specifiers are more restricted and more secure.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote