From the course: Java Object-Oriented Programming

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Discovering access modifiers

Discovering access modifiers - Java Tutorial

From the course: Java Object-Oriented Programming

Discovering access modifiers

- In Java, we can achieve encapsulation by using access modifiers. Different access modifiers determine where certain variables and methods can be accessed in your code. In other words, they allow you to restrict to the scope of specific functionality in your program. The three different access modifiers in Java are private, protected, and public. We use these key words on pieces of a given class to give it a certain access or visibility level. Private variables and methods are only visible in the class that they live in. Protected variables and methods are visible to the package and all subclasses. Public members are accessible everywhere within the program. If no modifiers provided, it's only visible to the package it lives in. In the main class, the main method has the public access modifier. This allows it to be invoked by the JVM or Java Virtual Machine in order to execute a program. In the tree class,…

Contents