private interfaces or abstract classes: what are my choices

So here is my problem:

I have used a number of interfaces in my project to help maintain good coding style, however, the design of my software suggests I may be able to export this as a library that I and others can use.

The problem is that of accessibility. In some of my interfaces I do not want others to have access to the methods, but Java enforces them to be public. This is OK in my own projects but I don't want others to be able to access these methods.

For example, I have one interface called Sprite with some of the methods below

Sprite: void update(Room room); void draw(Room world, Canvas c); void updateHealth(); void updateLocation(Room room); int getLayer(); void shoot(); void setDirection(Direction direction); ...

This is good because all Sprites need these methods but some of them such as draw() I only want my Game Engine to use.

What are my options?

-------------Problems Reply------------

I would say, narrow the interface of Spirte and move the methods you want to be private to AbstractSprite subclass. This is the easiest way.

Abstract classes should be fine if:

  • you can have some common implementation (even if it's a very simple one like a couple of properties)
  • you have to define those protected methods
  • and your THINGS are the same kind of objects (is-a relationship). You are saying "all my sprites" so you are saying: "all are sprites of some sub-type".

So... in your case, it seems that an abstract class would fit well.

Don't be afraid. If you need interfaces you can define them later, too (even abstract class implementing an interface with a subset of methods).

Or if you need a change, you can refactor.

What about creating a (or multiple) Facade?

If you have a larger set of classes/interfaces playing together in your library, then a Facade provides a controlled 'interface' to the outside world. More concretely, for your Sprite you can divide your interface into two interfaces: one which is available to the outside world through your Facade, but doesn't include things like draw(). And something like a DrawableSprite for your internal usage. Your internal objects would of course implement both of these interfaces then.

Java allows 4 levels of access private, protected, public and default. (default is with no keyword) Java in a Nutshell calls it default Sun java tutorial also says package-private

default allows access to from classes in the same package.

So in this case make interfaces that only you should access be default so cannot be seen from outside your package.

For Sprite you need to split into 2 interfaces public Sprite containing the ones that external code can use. default for only the Game Engine

The object you make for use by Game Engine can extend both interfaces,

Category:java Views:0 Time:2010-03-03
Tags: java

Related post

  • Java: Interface vs Abstract Class (regarding fields) 2009-01-20

    From what I have gathered, I want to force a class to use particular private fields (and methods) I need an abstract class because an interface only declares public/static/final fields and methods. Correct?? I just started my first big java project a

  • Interface vs Abstract Class (general OO) 2009-04-17

    I have had recently two telephone interviews where I've been asked about the differences between an Interface and an Abstract class. I have explained every aspect of them I could think of, but it seems they are waiting for me to mention something spe

  • What is the difference between an interface and abstract class? 2009-12-16

    What exactly is the difference between an interface and abstract class? --------------Solutions------------- Interfaces An interface is a contract: the guy writing the interface says, "hey, I accept things looking that way", and the guy using the int

  • What's the standard way to organize the contents of Java packages - specifically the location of interfaces and abstract classes 2010-06-11

    I suppose this could go for many OO languages. I'm building my domain objects and am not sure where the best place is for the interfaces & abstract classes. If I have a pets package with various implementations of the APet abstract class: should

  • C#: Interface and Abstract to enable Implement and Override 2011-05-19

    I've got to design about 5 different classes. A lot of the parts of these classes will be very similar, but they will have slight differences (of course). If I write an interface that each of my designed classes implement, then each abstract method i

  • Constraints on type parameters: interface vs. abstract class 2011-09-20

    Creating a simple list (UniqueList) of items with a Name property that must only contains unique items (defined as having different Names). The constraint on the UniqueList type can be an interface: interface INamed { string Name { get;} } or an abst

  • Is a Java interface an abstract class? 2008-11-03

    I'm working through some homework and a question on a previous exam paper asks to name all of the abstract classes in a given UML diagram. Fairly straightforward, I suppose. There is one abstract class and three interfaces. Do these interfaces qualif

  • What is a private interface? 2009-04-27

    In an interview a while ago for a .NET position the interviewer asked me "what would you use a private interface for?". I asked him did he mean the difference between implicit vs explicit interface implementation to which he answered no. So I'm wonde

  • Interface or abstract class? 2009-07-22

    For my new Pet-Project I have a question for design, that is decided already, but I want some other opinions on that too. I have two classes (simplified): class MyObject { string name {get;set;} enum relation {get;set;} int value {get;set;} } class M

  • When to use interfaces or abstract classes? When to use both? 2009-08-05

    While certain guidelines state that you should use an interface when you want to define a contract for a class where inheritance is not clear (IDomesticated) and inheritance when the class is an extension of another (Cat : Mammal, Snake : Reptile), t

  • What are the factors to consider when choosing between interfaces and abstract classes? 2010-01-25

    When designing my software, I began with interfaces since this seems to be the "standard". Then I switched to abstract classes because they seem better suited to the problem at hand. However I'm not sure if I've missed out on some considerations when

  • Why declare an interface as abstract? 2010-01-25

    What's point of declaring an interface as abstract? Same thing for an interface method. Is there a point to it? eg. public abstract interface Presenter { public abstract void go(final HasWidgets container); } --------------Solutions------------- Wher

  • Interface and abstract public function 2010-03-03

    It seems to me that both interface and abstract function are quite similar, it's like an order that some method must be implemented, so what's the difference? --------------Solutions------------- Have a look at this. Quoting: (Very Good Explantion by

  • Interfaces and abstract classes in object oriented programming 2010-08-18

    Possible Duplicate: Interface vs Abstract Class (general OO) Hi guys, I decided to dig deep into OOP and have been taking on Design patterns practicing with PHP. I just had a question I would like views on. Interfaces and Abstract classes, which has

  • help with interfaces and abstract classes 2010-09-02

    I'm recently getting a bit confused with interfaces and abstract classes and I feel I dont fully grasp it like I thought I did. I think I'm using them incorrectly. I'll describe what I'm doing at the moment, the problem I have faced, and then hopeful

  • Private interface vs. private method - objective c 2010-10-15

    What is the difference between a private method and a private interface? For example, I know that if you define a method in an implementation and its interface does not mention it, it is considered a private method. I have also seen things such as: @

  • An Interface with Abstract Methods 2011-01-04

    I came across some PHP code that was written by a co-worker (it was not used for anything). Basically it was an interface containing abstract methods. I then said that this was stupid and showed another co-worker sitting next to me. We laughed but th

  • What are the advantages of interfaces and abstract classes? 2011-01-05

    Possible Duplicates: purpose of interface in classes What is the difference between an interface and abstract class? Hi I am a php programmer. any body can explain what is the advantage of using interface and abstract class. --------------Solutions--

  • Interfaces and abstract classes 2011-01-28

    Which is not true about interfaces and Abstract classes? (at least mark two answers) a. Abstract class support to the inheritance relationship but not realization relationship. b. Interface supports to the realization relationship but not inheritance

Copyright (C), All Rights Reserved.

processed in 0.212 (s). 11 q(s)