Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Why is there no "package-private interfaces" and "abstract static"?

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Why is there no "package-private interfaces" and "abstract static"?

    These are very theoretic questions, I know, but nevertheless I am curious as to why this is not possible; what is the reasoning behind this?

    Question 1).
    Why must interfaces be public? Why can I not make them package-private (and their methods too)?
    I would like to use interfaces to structure the internal code of an engine, but many of these methods must NOT be called from outside of the engine as they may easily result in undefined behaviour if called at a bad time. But unfortunately I am forced to make them public since they are part of an interface. I am very disappointed by this.

    Question 2).
    Why is it not possible to declare "abstract static" methods? I know that this would be against the OOP paradigm, but so are static methods alone. It would really be nice sometimes to be able to do this.

    Thanks for your answers.


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Why is there no "package-private interfaces" and "abstract static"?

    Hello.
    Did you do the experiment once before posting the first question?
    If not can you do one experiment before reading further.
    If you cannot then try this experiment and tell me if it is valid or not.
      public class Run  {
        private interface I1  {}
        protected interface I3 {}
        public static void main(String str[])  {
          System.out.println("private, package, protected and public interfaces are all valid in Java");
        }
      }
      interface I2  {}

    Coming to the second question, an abstract method is one that does not contain body. A static method is one that can be invoked irrespective of whether an instance of the class to which it belongs to is created or not. Hence these two modifiers "static" and "abstract" cannot appear together because each one violates the property of the other.

    Thanks,
    Syed.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Why is there no "package-private interfaces" and "abstract static"?

    I think that came out wrong; of course I know that an interface can be declared private; but all of its methods are still public.
    Here is an example of what I mean:


    I have an Engine, and an important EngineClass which is part of the Engine.
    I have several kinds of EngineParts and they are all supposed to implement the interface PrivateInterface which provides the key functionality of the Engine.
    However, some functions, like the doStuff() function, are not supposed to be visible to the user outside. This is not possible in java.

    This is what is possible in java:
    package enginepkg;
     
    public class Engine {
     
    	private static interface PrivateInterface {
    		public void doStuff();
    	}
     
    	public static class EngineClass implements PrivateInterface {
    		public void doStuff() {
     
    		}
    	}
     
    }
    package userpkg;
     
    import enginepkg.Engine.EngineClass;
     
    public class OutsideClass {
     
    	public void test() {
    		EngineClass ec = new EngineClass();
    		ec.doStuff();
    	}
     
    }

    This is what I would like to have:
    package enginepkg;
     
    public class Engine {
     
    	private static interface PrivateInterface {
    		void doStuff();
    	}
     
    	public static class EngineClass implements PrivateInterface {
    		void doStuff() {
     
    		}
    	}
     
    }
    package userpkg;
     
    import enginepkg.Engine.EngineClass;
     
    public class OutsideClass {
     
    	public void test() {
    		EngineClass ec = new EngineClass();
    		//ec.doStuff(); <- This method can not be called by a class from outside the package "enginepkg".
    	}
     
    }

    Coming to the second question, an abstract method is one that does not contain body. A static method is one that can be invoked irrespective of whether an instance of the class to which it belongs to is created or not. Hence these two modifiers "static" and "abstract" cannot appear together because each one violates the property of the other.
    I dont see how one violates the property of the other.
    An abstract static method would be a method which can only be invoked on the subclasses of an abstract class; and each of these subclasses has to define the static method in question.

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Why is there no "package-private interfaces" and "abstract static"?

    Hello.
    Let me first clarify the second question.
    You said static and abstract do not violate the each others properties. Yes it does. Let me quote your statement,
    "An abstract static method would be a method which can only be invoked on the subclasses of an abstract class; and each of these subclasses has to define the static method in question".
    When a method is static it can be invoked with or without instantiating the class to which it belongs to. There is a possibility that someone will invoke that method without creating an object of that class. But if the method is abstract what will JVM do?

    Coming to the first question, an interface must contain public methods only. Even if we do not mention the access specifier by default it will become public. Actually the real power of interfaces will be known if we develop real enterprise applications. There could be good reasons why methods in interfaces are forced to be public. I do not know them as of now. May be someone else will give. If I find them out I shall share with you.

    Syed.

    --- Update ---

    Cornix,
    Also, in your first post you said "Why must interfaces be public? Why can I not make them package-private (and their methods too)?".
    After my reply then in your second post you said "of course I know that an interface can be declared private".
    You contradicted yourself.

    Syed.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Why is there no "package-private interfaces" and "abstract static"?

    1). The compiler can check if you are trying to call an abstract method or not. In the same way the compiler could check if you are calling an abstract static method and if you try it will not compile. Thats nothing impossible and I still can not see why you think this would not work in any way.

    2). I know what interfaces can do and what they cant do. I want to know why this artificial limit is imposed on us as it doesnt seem to have any purpose in my opinion. I can not see any good reason why a package-private interface method should be impossible.

  6. #6
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Why is there no "package-private interfaces" and "abstract static"?

    Hello.
    I think you should discuss these two matters with James Gosling.
    Let me know once you have answers from him.

    Thanks,
    Syed.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Why is there no "package-private interfaces" and "abstract static"?

    1. It's perfectly legal to have an interface with package-access only

    package packA;
     
    interface MyInterface
    {
        void foo();
    }

    Note that this is a compile error:

    package packB;
     
    class MyClass implements packA.MyInterface
    {
        void foo() {}
    }

    Interface methods can't be declared private for a very good reason: private means they are only accessible from the immediate class/interface. An interface cannot provide an implementation for any of its methods, thus a private interface method can never be implemented.

    It does seem like not allowing protected methods in interfaces is a bit of an oversight, though. The best solution in Java is to design your class hierarchy such that you can extend some key common classes which do allow protected abstract methods. This is part of the trade-off between multiple inheritance and single inheritance+interfaces (more power vs. more safety).

    abstract static is dis-allowed because of the way static methods are implemented they cannot support the necessary implementation details that instances have to support inheritance and method overriding.

  8. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Why is there no "package-private interfaces" and "abstract static"?

    abstract static is dis-allowed because of the way static methods are implemented they cannot support the necessary implementation details that instances have to support inheritance and method overriding.
    But why is that? The good thing about programming is that you can change things later on without any material loss, the designers of java could just change the way static methods work and allow abstract static method declarations. I would like to hear a good argument as to why this doesnt work.

    It does seem like not allowing protected methods in interfaces is a bit of an oversight, though. The best solution in Java is to design your class hierarchy such that you can extend some key common classes which do allow protected abstract methods. This is part of the trade-off between multiple inheritance and single inheritance+interfaces (more power vs. more safety).
    The problem is if you already extend a different, equally important class. It seems to me as if inheritance is completely useless in java if you program on a higher level. I just want to have a clean and simple program structure but the way java forces single-inheritance and interfaces to only have public methods makes this very hard sometimes.
    The best way to achieve what I am trying to do seems to be delegation, but that just produces more code and makes it less readable in my opinion.

    I would just like to know a good reason for this. I dont want to think that this has been done haphazardly but rather there were very good arguments for this design of the java language.

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Why is there no "package-private interfaces" and "abstract static"?

    single inheritance was done as a backlash to the "diamond problem" which languages with multiple inheritance have (such as C++). The designers of Java decided that single inheritance+interfaces was a "safer" structure because of this. Like I said, it's a trade off between what you can do and what safety you want. Personally, I'm not happy with it but I rarely have to contend with this if I structure my code cleverly, or I just don't use Java.

    But why is that? The good thing about programming is that you can change things later on without any material loss, the designers of java could just change the way static methods work and allow abstract static method declarations. I would like to hear a good argument as to why this doesnt work.
    In order to instantiate anything, you need to have a complete type. Complete types must have all methods defined, not merely declared. Abstract prevents you from having a complete type because you are intentionally saying "this bit is missing here, look elsewhere for it". So what happens with this code?

    public class A
    {
        public static int some_val;
        public abstract static void foo();
    }

    I can never have an instance of the actual class A because it is incomplete (there is no definition of A.foo() by design). Therefore I can never instantiate the actual class A, and get access to the static member some_val.

  10. #10
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Why is there no "package-private interfaces" and "abstract static"?

    But the compiler / JVM is not forced to do it this way.
    They could load the class and everything and give you access to everything that is not abstract. This is not magic, this is programming. Whatever we want to do is possible because we make it.

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Why is there no "package-private interfaces" and "abstract static"?

    I suppose the key question is what problem are you trying to solve with static abstract? It's not immediately obvious to me. Do you want to force all inheriting classes to implement a certain static method? That doesn't encapsulate the idea of abstract at all and is open to a host of problems. For example, what if it doesn't make sense for B extends A to implement foo, but it does for C extends B? Does the compiler complain that a static method isn't implemented in B? What if B extends A implements foo, but C extends B does not? Can C "piggy back" off of B's implementation, or must it provide its own complete implementation?

    Abstract means that I don't have enough information for an actual instance, but I have enough information to view/use a given object. Your solution is to allow partial instantiation and usage of a class with static abstract violates this because we have a partial instance of the actual class which we're saying is legal for the original declaring class, but it is not immediately obvious for what inheriting classes it would be a compile error.

    Additionally, in order to use a static method you must quantify with a type that is the given type, or is a child of the given type. We can never run into the issue where we want to call a static method with a parent type.

    Not everything is possible just because we want them to be. Programs must adhere to the rules and laws of mathematics, and eventually our code must run on real hardware. Certain things just aren't feasible or possible with current hardware technology.

  12. #12
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Why is there no "package-private interfaces" and "abstract static"?

    I am not trying to solve any problem with abstract static. As I stated in my initial post, this is a theoretical question. I was just wandering why this was not implemented as it seemed like a very useful feature to me.

    For example, imagine you need to know the size in bytes for data types. (maybe to exchange data with another process or maybe to use a native library). It would be nice to have an abstract class like "DataType" which is a super class of Integer, Float, Double, etc. And "DataType" has an abstract static method called "sizeOf()" which returns the size of the type in bytes. The class Integer could implement the method and return 4, the same goes for Float, while Short would return 2 and Double would return 8.
    It doesnt make much sense to have this method not static. Its not like every Integer would have a different size. It just feels correct to do it this way, using polymorphism and such.

    But that was just a quick example I made up on the spot. Maybe there are much better examples which I just cant think of at the moment.

  13. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Why is there no "package-private interfaces" and "abstract static"?

    A better solution IMO is duck-typing+constraints. This is the approach C++ is moving towards.

    Why should sizeof() be limited to just integral types (a.k.a. types which extend DataType)? If it has a sizeof method (better yet, have a sizeof operator like in C++), I should be able to call it. The compiler can check if it really does, and if so use it. If not, throw a compiler error. There is also on-going work in C++ development to create a "constraints" or "concepts-lite" which encapsulates this idea better. The details allow the developer to explicitly spell out what it means for a class to encapsulate a certain idea, for example the mergable idea, or copyable. A linked list node could be mergable, and two vectors/array lists could be mergable. Hashsets should be mergable, etc.

    Of course, this is all C++, not Java. I don't think this is something that's coming to Java anytime soon because of the way Java generics work. There's not one "best" language, and our goal as programmers is to pick a suitable language to solve the problem at hand.

    There's also the issue of backwards compatibility, and exact details which need to be worked out. With C++ any idea proposal needs to have at least 2 things:

    1. What type of problems the idea is intended to solve
    2. How the solution would work (a.k.a. a prototype implementation with standardese draft documents of the feature)

    Java's language has really been stagnant for quite a while because the maintainers of the language can't agree on what direction to take Java, plus there's a general lack of people who want to work on the latter part. A few features of Java 7's initial design were post-poned to Java 8 or indefinitely because of the lack of people working on implementing/specifying the features.

Similar Threads

  1. "Ask a Question" menu item dumps into "What's Wrong With My Code?"
    By GregBrannon in forum Forum Updates & Feedback
    Replies: 1
    Last Post: August 6th, 2013, 03:37 AM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM