Skip to main content
Active reading [<en.wiktionary.org/wiki/now#Adverb> <en.wiktionary.org/wiki/your#Determiner> <en.wikipedia.org/wiki/Java_version_history#Java_17> <english.stackexchange.com/questions/4645/is-it-ever-correct-to-have-a-space-before-a-question-or-exclamatio#comment206109_4645>]. Dressed the naked link.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

For beginners, considering this example can be helpful;

Consider I have developed MyClass in the foo package and it has a fantastic method named print which you are interested in calling it ( itit could be a method or property  ):

package foo; // I am in foo  public class MyClass { private void print() { //This is private System.out.println("I can print!"); } } 
package bar; \\You are not in same package as me  import foo.MyClass;  public class YourClass { void test() { MyClass myClass = new MyClass(); myClass.print(); } } 
package foo;  public class MyClass {  void print() { // No access modifier means default or package-private  System.out.println("I can print!"); }  } 

NoNow consider this: Again you come to me

  • You: My boss told me I can not change my package ( Inin the actual world, you can not change youyour package to use other classes methods)
  • Me : There is another way, if you extend me and I make print() protected, then you can use it whether you change you package or not. ( So sub classingSo subclassing will always give you access to my method).
package foo; protected class MyClass { // itIt is now protected protected void print() { System.out.println("I can print!"); }  } 

Here is YourClass:

package bar; // You are on your own package import foo.MyClass;   public class YourClass extends MyClass { void test() { // You initiate yourself! But as you extend me, you can call my print() YourClass yourClass = new YourClass();  yourClass.print(); } } 

You may have noticed that by making a method protected allall other classes can use it by extending it, and you can not easily control how can use it can be used. This was solved in java 17Java 17 by introducing sealed and permits words. So you can define which classes can extend you. By something like public sealed class MyClass permits YourClass See What are sealed classes in Java 17?What are sealed classes in Java 17? for more infoinformation.

For beginners considering this example can be helpful;

Consider I have developed MyClass in foo package and it has a fantastic method named print which you are interested in calling it ( it could be a method or property  )

package foo; // I am in foo  public class MyClass { private void print() { //This is private System.out.println("I can print!"); } } 
package bar; \\You are not in same package as me  import foo.MyClass;  public class YourClass { void test() { MyClass myClass = new MyClass(); myClass.print(); } } 
package foo;  public class MyClass {  void print() { //No access modifier means default or package-private  System.out.println("I can print!"); }  } 

No consider this: Again you come to me

  • You: My boss told me I can not change my package ( In actual world you can not change you package to use other classes methods)
  • Me : There is another way, if you extend me and I make print() protected, then you can use it whether you change you package or not. ( So sub classing will always give you access to my method).
package foo; protected class MyClass { // it is now protected protected void print() { System.out.println("I can print!"); }  } 

Here is YourClass

package bar; // You are on your own package import foo.MyClass; public class YourClass extends MyClass { void test() { //You initiate yourself! But as you extend me you can call my print() YourClass yourClass = new YourClass();  yourClass.print(); } } 

You may have noticed that by making a method protected all other classes can use it by extending it, you can not easily control how can use it. This was solved in java 17 by introducing sealed and permits words. So you can define which classes can extend you. By something like public sealed class MyClass permits YourClass See What are sealed classes in Java 17? for more info

For beginners, considering this example can be helpful;

Consider I have developed MyClass in the foo package and it has a fantastic method named print which you are interested in calling it (it could be a method or property):

package foo; // I am in foo public class MyClass { private void print() { //This is private System.out.println("I can print!"); } } 
package bar; \\You are not in same package as me import foo.MyClass; public class YourClass { void test() { MyClass myClass = new MyClass(); myClass.print(); } } 
package foo; public class MyClass { void print() { // No access modifier means default or package-private System.out.println("I can print!"); } } 

Now consider this: Again you come to me

  • You: My boss told me I can not change my package (in the actual world, you can not change your package to use other classes methods)
  • Me : There is another way, if you extend me and I make print() protected, then you can use it whether you change you package or not. (So subclassing will always give you access to my method).
package foo; protected class MyClass { // It is now protected protected void print() { System.out.println("I can print!"); } } 

Here is YourClass:

package bar; // You are on your own package import foo.MyClass;   public class YourClass extends MyClass { void test() { // You initiate yourself! But as you extend me, you can call my print() YourClass yourClass = new YourClass(); yourClass.print(); } } 

You may have noticed that by making a method protected all other classes can use it by extending it, and you can not easily control how it can be used. This was solved in Java 17 by introducing sealed and permits words. So you can define which classes can extend you. By something like public sealed class MyClass permits YourClass See What are sealed classes in Java 17? for more information.

added 609 characters in body
Source Link
Alireza Fattahi
  • 46.1k
  • 18
  • 134
  • 187
  • You: My boss told me I can not change my package ( In actual world you can not change you package to use other classes methods)
  • Me : There is another way, if you extend me and I make print() protected, then you can use it whether you change you package or not. ( So sub classing will always give you access to my method).

You may have noticed that by making a method protected all other classes can use it by extending it, you can not easily control how can use it. This was solved in java 17 by introducing sealed and permits words. So you can define which classes can extend you. By something like public sealed class MyClass permits YourClass See What are sealed classes in Java 17? for more info

  • You: My boss told me I can not change my package ( In actual world you can not change you package to use other classes methods)
  • Me : There is another way, if you extend me and I make print() protected, then you can use it whether you change you package or not.
  • You: My boss told me I can not change my package ( In actual world you can not change you package to use other classes methods)
  • Me : There is another way, if you extend me and I make print() protected, then you can use it whether you change you package or not. ( So sub classing will always give you access to my method).

You may have noticed that by making a method protected all other classes can use it by extending it, you can not easily control how can use it. This was solved in java 17 by introducing sealed and permits words. So you can define which classes can extend you. By something like public sealed class MyClass permits YourClass See What are sealed classes in Java 17? for more info

Source Link
Alireza Fattahi
  • 46.1k
  • 18
  • 134
  • 187

For beginners considering this example can be helpful;

Consider I have developed MyClass in foo package and it has a fantastic method named print which you are interested in calling it ( it could be a method or property )

package foo; // I am in foo public class MyClass { private void print() { //This is private System.out.println("I can print!"); } } 

You have developed YourClass in bar package, and you are interested to use MyClass#print

package bar; \\You are not in same package as me import foo.MyClass; public class YourClass { void test() { MyClass myClass = new MyClass(); myClass.print(); } } 

Your code is not compile and you get error The method print() is undefined for the type MyClass

You come to me:

  • You: I want to use your method but it is private. Can you make it public?
  • Me : No I don't want others use it
  • You: I am your friend at least let me not others use it.
  • Me : Ok I will remove the private keyword. My Access modifier will be default or private package. As you are my friend you must be in same package as me. So you must come to my package foo. I mean exact package not even a sub package.

Then MyClass will be

package foo; public class MyClass { void print() { //No access modifier means default or package-private System.out.println("I can print!"); } } 

YourClass will be :

package foo;//You come to my package public class YourClass { void test() { MyClass myClass = new MyClass(); myClass.print(); } } 

No consider this: Again you come to me

  • You: My boss told me I can not change my package ( In actual world you can not change you package to use other classes methods)
  • Me : There is another way, if you extend me and I make print() protected, then you can use it whether you change you package or not.

Here is MyClass

package foo; protected class MyClass { // it is now protected protected void print() { System.out.println("I can print!"); } } 

Here is YourClass

package bar; // You are on your own package import foo.MyClass; public class YourClass extends MyClass { void test() { //You initiate yourself! But as you extend me you can call my print() YourClass yourClass = new YourClass(); yourClass.print(); } }