Use cases for “private” interfaces?

I was wondering if there was a valid use case for being able to properly define the specific internal properties and functions of a class in a way similar to how an interface defines the public properties and functions of a class.

Imagine the task that you have to build a class that describes a human being.

Obviously, each human is a humanoid creature but not every humanoid creature is a human so you’d probably have an interface IHumanoid with functions like these (since it isn’t useful to hardcode the body plan into the class):

public interface IHumanoid {
    function get head():IHead;
    function get torso():ITorso;
    function get leftArm():IArm;
    function get rightArm():IArm;
    function get leftLeg():ILeg;
    function get rightLeg():ILeg;
}

Further and also obviously, each human is a mammal but not every mammal is a human, so there’s probably another interface IMammal with two definitions for males and females floating around somewhere:

public interface IMammal {
    function procreate(partner:IMammal):void;
}

public interface IMaleMammal extends IMammal {
    function inseminate(female:IFemaleMammal):void;
}

public interface IFemaleMammal extends IMammal {
    function conceive(partner:IMaleMammal):Boolean;
    function giveBirth():IMammal;
    function nurse(offspring:IMammal):void;
}

Thus our class probably looks something like this now:

public class Human implements IHumanoid, IMammal {
    private var _head:IHead;
    private var _torso:ITorso;
    private var _leftArm:IArm;
    private var _rightArm:IArm;
    private var _leftLeg:ILeg;
    private var _rightLeg:ILeg;

    public function Human() {
        // ctor...
    }

    public function get head():IHead {
        return _head;
    }

    public function get torso():ITorso {
        return _torso;
    }

    public function get leftArm():IArm {
        return _leftArm;
    }

    public function get rightArm():IArm {
        return _rightArm;
    }

    public function get leftLeg():ILeg {
        return _leftLeg;
    }

    public function get rightLeg():ILeg {
        return _rightLeg;
    }

    public function procreate(partner:IMammal):void {
        // "abstract" function
    }
}

public class MaleHuman extends Human implements IMaleMammal {
    override public function procreate(partner:IMammal):void {
        if (partner is IFemaleMammal) {
            inseminate(partner);
        }
    }

    public function inseminate(female:IFemaleMammal):void {
        female.conceive(this);
    }
}

public class FemaleHuman extends Human implements IFemaleMammal {
    override public function procreate(partner:IMammal):void {
        if (partner is IMaleMammal) {
            conceive(partner);
        }
    }

    public function conceive(partner:IMaleMammal):Boolean {
        // ...
    }

    public function giveBirth():IMammal {
        // ...
    }

    public function nurse(offspring:IMammal):void {
        // ...
    }
}

From this we can implement our classes further and everything’s working nice and fine until we get the task to use the existing interfaces to implement some other classes. Perhaps a gorilla, an orca and a platypus.

Ignoring the massive issue the platypus will pose to our current interface structure (*cough* egg laying mammal *cough*), we have the “problem” that nothing prevents us from giving the gorilla 2 brains, the orca 8 lungs and the platypus half a dozen livers. And while we might be disciplined enough to follow the structure mammals typically have we cannot guarantee the same if we open the API for other developers who might code some seriously screwed up things which still look okay to the outside world.

Therefore I was wondering if there was a valid use case to create something like a “private interface” which defines non-public functions and properties. Perhaps something along these lines:

public structure SMammal {
    function get brain():IBrain;
    function get liver():ILiver;
    function get leftLung():ILung;
    function get rightLung():ILung;
    function get leftKidney():IKidney;
    function get rightKidney():IKidney;
}

public class Human implements IHumanoid, IMammal follows SMammal {
    private function get brain():IBrain {
        // ...
    }

    private function get liver():ILiver {
        // ...
    }

    // etc. etc.
}

Does such a feature exist in any programming language? Can abstract classes be used to solve this? Or shouldn’t we care about this at all as long as the public interface somehow works as expected?

2

And while we might be disciplined enough to follow the structure
mammals typically have we cannot guarantee the same if we open the API
for other developers who might code some seriously screwed up things
which still look okay to the outside world.

What exactly do you gain by forcing them to follow your structure? You possibly break some advanced usages, maybe there is a case for an animal with multiple brains. And the client programmer can mess up the implementation of a class in so many crazy ways that trying to prevent this feels like trying to a close window in an attempt to keep a horse in a barn.

Don’t treat your client programmers like prisoners. If they follow the public interface, let the code run.

3

As an interface type is by default a public member everything within it should be public but I have seen this:

https://stackoverflow.com/questions/792908/what-is-a-private-interface

from msdn:

Interfaces consist of methods, properties, events, indexers, or any
combination of those four member types. An interface cannot contain
constants, fields, operators, instance constructors, destructors, or
types. It cannot contain static members. Interfaces members are
automatically public, and they cannot include any access modifiers.

http://msdn.microsoft.com/en-us/library/ms173156.aspx

4

Does such a feature exist in any programming language? Can abstract classes be used to solve this? Or shouldn’t we care about this at all as long as the public interface somehow works as expected?

It sure does. Pretty much any feature exists in some language.

For example, in Objective-C++ (iOS/Mac programming) it’s standard procedure for a class to have at least two interfaces. On that is public, and one that is private. Sometimes they’ll also have extra interfaces that are defined elsewhere (eg, the low level string class has an interface for performing GUI screen drawing operations on the string, which is defined in a completely separate framework/library from the one that defines the string class).

Basically the way I treat public/private is simple:

Public interface elements can never be changed. Whenever you refactor or improve your code, the public interface still needs to behave exactly the same as before.

Private interfaces on the other hand, can be changed or completely removed whenever you want. As long as you update all the code in the class to understand the new behaviour, you’re fine. Often the public interface will be full of one or two line methods that hand the actual work over to the private interface.

Objective-C++ also has the concept of “protected” in the interface, where something is available to subclasses but not external classes.

Usually my code is split about half and half between public and private. I don’t use protected much at all, though it does come in handy once in a while.

At least in .NET languages, it’s possible to apply an access specifier to an interface. This can be helpful when an some combinations of an assembly’s internal classes share common abilities, but those combinations and abilities do not fit a hierarchical relationship. The fact that the abilities do not fit a hierarchical relationship makes it necessary to use interfaces to represent them, but in no way implies that external code should be allowed to define types that claim to implement the interfaces. Further, interfaces cannot contain any methods whose parameter types or return types have narrower access than the interfaces themselves. If class Fred is declared internal, and an interface IFred has a method which returns a Fred, then the interface must be declared internal. If nested class Fred.Joe is private and nested interface Fred.IJoe has a method that returns a Fred.Joe, then Fred.IJoe must likewise be declared private.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật