Exploring Smartphone Influence on the Brain: Neurological Perspectives
Angular vs. React: Which One Should You Choose?
Discover Serenity: Your Guide to Mindfulness Apps
Discover how smartphone apps can enhance your mindfulness practice: 5 Senses Exercise with Apps
Discover How Smartwatches Measure Blood Pressure: Explained Simply
13 Exciting Games Launching in April 2024: From Freedom Planet 2 to TopSpin 2K25!
Discover the 5 Amazon Big Spring Deals on Tablets from Samsung, Lenovo
Big Savings Alert: Amazfit Smart Watches Now on Sale on Amazon!
Amazon's Big Spring Sale: Top 6 Anker Souncore Headphones and Earbuds Deals
Affordable VR Adventures: The Best Budget VR Headsets
Fly in Peace: Discover the Ultimate Noise-Cancelling Headphones for Flying
Bringing AI to Life: NVIDIA's Digital Human Technolgies in Healthcare, Gaming, and More
Discover Exciting New Games on NVIDIA GeForce NOW!
Steam Spring Sale 2024 is here: Explore the 10 Best FPS Games
The Future of iPhones: Apple's Bold Step into AI with DarwinAI
Discover the Magic of Sonos Soundbar: Transform Your Home Entertainment Experience!
Enhance Your Home Fun: 5 Best Sonos Soundbars to Amp Up Your Entertainment!
Pinterest Introduces AI-Powered Body Type Ranges for Inclusive Searches
Embrace the Next Wave: 35+ AI Tools for Enhanced Productivity in 2024
Xbox Gaming Bonanza: Lots of New Games with Xbox Game Pass!
Sony Paves the Way for Gaming Evolution with 'Super-Fungible Tokens' Patent
Smart Printing Choices: 10 Key Factors to Consider When Buying an HP Printer or Any Printer
Projector Picks: Explore the Best Mini Projectors for Big Screen Fun!
JavaScript Essentials: Your Quick Start to Web Coding
Gaming Laptop Guide 2024: 10 Crucial Checks Before You Buy + Top 5 Picks for you!
Gaming Joy Awaits: Explore the Best PS5 Games of 2024
Epic Games Special: Dive into Astro Duel 2 for FREE this week. See What’s Coming Next Week!
Fitbit Fitness Tracker Guide 2024: Choose Your Perfect Fit
Feel the Beat: Exploring Top Over-Ear Headphones
Explore the Web Development Strategies in 2024: A Developers Handbook
Explore Must-Play Nintendo Switch Games in 2024!
Eclipse Ready: CE and ISO Certified Solar Eclipse Glasses for a Safe Sky Spectacle
Disney and Pixar’s Inside Out 2 Introduces New Emotions to Riley's World
Discover Waze's cool new features for safer and happier drives!
Discover the Top Picks: Best Smartwatches for Your Lifestyle
Discover the Best Smartphones Trending Now: Your Easy Guide to the Best Picks!
Sound Revolution: Discover the Best Bluetooth Speakers of 2024!
Discover the 10 Best Productivity Apps to Supercharge Your Daily Tasks
Discover,Install and Enjoy: The Best Chrome Extensions for Developers in 2024
Crack the Code: Your Guide to Computer Programming Magic in 2024
Boost Your Content Creation: 10 ChatGPT Prompts to Supercharge Content Creation Productivity
10 Best Tech Companies in Silicon Valley
Top 10 Web Development Interview Questions you can...
Learn how to Answer Tell me about Yourself
5 Books You Need to Read Right Now
25 Practical Ways to Earn Money Online
Translate this page in your preferred language:
Article by: Manish Methani
Last Updated: October 27, 2021 at 10:04am IST
What is Java Class? Why is it being used in Object-Oriented programming language? What is Object-Oriented programming?
Do you play games? Yes, a game like Super Mario, Pacman, breakout game, snake game, etc. These different types of games are part of one big blueprint named "GAME" :). So there is something that creates a blueprint of various types of games and keeps them together in a player-like video games device (eg, Play Station, etc).
As I explained in the above example, a Class is just a blueprint that contains stuff like properties, variables, member functions, and all. A class definition starts with class keyword followed by the class name and class body is enclosed by pair of parenthesis
modifier class { }
The modifier in the given syntax is called an access modifier in java. Let's have a look at the difference between public, private, protected access modifiers in Java.
There are various types of Access Modifiers in Java as follows:-
1) public
2) private
3) protected
When a method or variable member is declared public, it means all other classes, regardless of the package they belong to, can access the member. In simple language, you can assume the packages in Java as the folders in the computer system with some permissions.
Assume there are two different packages (Packages in Java are nothing but folders in simple language) named aPackage & bPackage. aPackage contains one class & bPackage contains one class.
package aPackage; import bPackage.*; // Import all classes in the bPackage package class SuperClass { public static void main(String[] args) { SubClass sub = new SubClass(); sub.display(); } }
Now look at the second file:
package bPackage; public class SubClass { public void display() { System.out.println("This is subclass"); } }
This is subclass
Here, there are two packages (Consider Packages as folders always) named as aPackage & bPackage . aPackage contains SuperClass.java & bPackage contains SubClass.java Both the classes have public access.
In the first file Superclass.java, we created an instance of Subclass and called its display() method and since that display method was public inside Subclass that's why the method is visible in Superclass. And as you see both the files were in two different folders (Packages) public access allows using its methods from different classes too.
When a method or variable member is declared private, no other class, no other packages can access the private properties except the class in which those private members or methods are written. This access modifier is called a private access modifier.
Assume there are two different packages (Packages in Java are nothing but folders in simple language) named aPackage & bPackage. aPackage contains one class & bPackage contains one class.
package aPackage; import bPackage.*; // Import all classes in the bPackage package class SuperClass { public static void main(String[] args) { SubClass sub = new SubClass(); sub.display(); // Compilation error at this line } }
Now look at the second file:
package bPackage; public class SubClass { private void display() { System.out.println("This is subclass"); } }
As you see display() method in SubClass is private . So in SuperClass when it tries to access display method, compilation error appears because you cannot access private methods. But SubClass is public thats why you can access it but not its methods . If you make SubClass modifier also private , then
package aPackage; import bPackage.*; // Import all classes in the bPackage package class SuperClass { public static void main(String[] args) { SubClass sub = new SubClass(); // Compilation error at this line sub.display(); // Compilation error at this line } }
Now look at the second file:
package bPackage; private class SubClass { private void display() { System.out.println("This is subclass"); } }
default access allows accessing the members & methods only if both classes are in the same folder (package) whereas protected access allows to access the members & methods of the same class, subclass through inheritance irrespective of any package access. Any package classes can use methods but only through inheritance.
Let's discuss the default access modifier in the same package and in a different package. It works in the same package through inheritance but not in another package.
package aPackage; class SuperClass { void display() { // No modifier means method has default // access System.out.println("First Class Method"); } } public class SubClass extends SuperClass { public static void main(String[] args) { SubClass sub = new SubClass(); sub.display(); } }
First Class Method
As you see, the display method in SuperClass contains no access modifier. So access modifier in this case is the default. default modifier will not allow using methods of superclass object of another package but in the same package, it will work fine through inheritance( As you see inside SubClass, we are calling the display() method of SuperClass through Subclassinstance sub)
package aPackage; public class SuperClass { void display() { // No modifier means method has default // access System.out.println("First Class Method"); } }
package bPackage; import aPackage.*; class SubClass { static public void main(String[] args) { SuperClass firstClass = new SuperClass(); firstClass.display(); // Compilation Error } }
As you see, the display method in SuperClass contains no access modifier. So access modifier in this case is default. default modifier will not allow using methods of superclass object of another package but in the same package, it will work fine through inheritance. But in this program, both the classes are in different packages. SuperClass is in aPackage and SubClass is in bPackage and if we tried to call display() method of SuperClass in SubClass then compilation error is being shown.
protected access is used to access the methods of same class plus another class in same as well as in another package but only through inheritance.
package aPackage; public class SuperClass { protected void display() { System.out.println("SuperClass Method"); } }
package bPackage; import aPackage.*; public class SubClass extends SuperClass{ public static void main(String []args) { SubClass superClass = new SubClass(); superClass.display(); } }
SuperClass Method.
Test your C Programming skills with this comprehensive mock test on C Programming.
Take TestSolve most asked Interview Questions on Flutter and Test your foundational skills in flutter.
Take TestSolve most asked GATE Questions in Operating Systems and test your Gate Score.
Take TestThis is a mock test designed to help you assess your knowledge and skills in HTML and CSS.
Take TestSolve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.
Take Test