{"id":365,"date":"2025-07-01T12:26:08","date_gmt":"2025-07-01T12:26:08","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=365"},"modified":"2025-07-16T10:05:35","modified_gmt":"2025-07-16T10:05:35","slug":"how-will-you-access-default-and-protected-class","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/how-will-you-access-default-and-protected-class\/","title":{"rendered":"How will you access default and protected class?"},"content":{"rendered":"\n<p>\ud83d\udd10 Access Modifiers Overview in Java<\/p>\n\n\n\n<p>Java provides four levels of access control:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Modifier<\/th><th>Same Class<\/th><th>Same Package<\/th><th>Subclass (any pkg)<\/th><th>Other Packages<\/th><\/tr><\/thead><tbody><tr><td><code>public<\/code><\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><\/tr><tr><td><em>default<\/em> (no modifier)<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u274c<\/td><td>\u274c<\/td><\/tr><tr><td><code>protected<\/code><\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u274c<\/td><\/tr><tr><td><code>private<\/code><\/td><td>\u2705<\/td><td>\u274c<\/td><td>\u274c<\/td><td>\u274c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udcd8 Default Access (Package-Private)<\/p>\n\n\n\n<p>\u2714\ufe0f Meaning:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>No modifier means <strong>default access<\/strong>.<\/li>\n\n\n\n<li>The class or member is accessible <strong>only within the same package<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udce6 Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ File: com\/example\/Vehicle.java\npackage com.example;\n\nclass Vehicle {  \/\/ default access\n    void move() {\n        System.out.println(\"Vehicle is moving\");\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ File: com\/example\/Test.java\npackage com.example;\n\npublic class Test {\n    public static void main(String&#91;] args) {\n        Vehicle v = new Vehicle();  \/\/ \u2705 Accessible (same package)\n        v.move();\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ File: com\/other\/Test2.java\npackage com.other;\n\nimport com.example.Vehicle;\n\npublic class Test2 {\n    public static void main(String&#91;] args) {\n        Vehicle v = new Vehicle();  \/\/ \u274c Compile error (different package)\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\ud83d\udd12 Protected Access<\/strong><\/p>\n\n\n\n<p>\u2714\ufe0f Meaning:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Accessible in the <strong>same package<\/strong><\/li>\n\n\n\n<li>Also accessible in <strong>subclasses<\/strong>, even in different packages (through inheritance only)<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udce6 Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ File: com\/example\/Animal.java\npackage com.example;\n\npublic class Animal {\n    protected void sound() {\n        System.out.println(\"Animal sound\");\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ File: com\/other\/Dog.java\npackage com.other;\n\nimport com.example.Animal;\n\npublic class Dog extends Animal {\n    public void bark() {\n        sound();  \/\/ \u2705 Allowed because Dog extends Animal\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ File: com\/other\/TestDog.java\npackage com.other;\n\nimport com.example.Animal;\n\npublic class TestDog {\n    public static void main(String&#91;] args) {\n        Animal a = new Animal();\n        a.sound();  \/\/ \u274c Not allowed: protected method via reference\n    }\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <em>To access a protected method in a subclass outside the package, you must call it from inside that subclass\u2014not via object reference.<\/em><\/p>\n\n\n\n<p>\ud83c\udfaf Interview Tips &amp; Common Questions<\/p>\n\n\n\n<p><strong>\ud83d\udcd6 Common Java Interview Questions<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Q: What&#8217;s the difference between default and protected access?<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>A:<\/strong> Default allows access within the same package only. Protected allows access within the same package + from subclasses outside the package (via inheritance).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Q: Can a protected class be accessed from a different package?<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>A:<\/strong> A class <strong>cannot<\/strong> be declared protected. Only class members (fields and methods) can be protected.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Q: How do protected members behave in subclass vs object reference?<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>A:<\/strong> In a subclass, you can access protected members directly. But from an object of superclass, you <strong>cannot<\/strong> access protected members outside the package.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Q: Can a top-level class have default or protected access?<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>A:<\/strong> A top-level class <strong>can be <code>public<\/code> or default<\/strong> only. <code>protected<\/code> is <strong>not allowed<\/strong> for top-level classes.<\/li>\n<\/ul>\n\n\n\n<p><strong>\ud83e\udde0 Key Takeaways<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udfe2 Use <strong>default<\/strong> when your class is only used internally in the same package.<\/li>\n\n\n\n<li>\ud83d\udfe2 Use <strong>protected<\/strong> to expose members to subclasses across packages.<\/li>\n\n\n\n<li>\u274c <code>protected class<\/code> is invalid for <strong>top-level<\/strong> classes.<\/li>\n\n\n\n<li>\u26a0\ufe0f Be careful when accessing protected methods\u2014<strong>it must be through inheritance<\/strong> in other packages.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how default and protected classes are accessed in Java. Understand package-level visibility, subclass access, and object-oriented design principles.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":414,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/365\/revisions\/414"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}