“B is a subtype of A” means “every B is an A.” Instead, those values are all ArrayList objects, or LinkedList objects, or objects of another class that implements List.Ī subtype is simply a subset of the supertype: ArrayList and LinkedList are subtypes of List. If we think about all possible List values, none of them are List objects: we cannot create instances of an interface. The Java List type is defined by an interface. Unfortunately, the compiler doesn’t check for us that the code adheres to the specs of those methods that are written in documentation comments. Java’s static type checking allows the compiler to catch many mistakes in implementing an ADT’s contract.įor instance, it is a compile-time error to omit one of the required methods, or to give a method the wrong return type. We explored two different representations for MyString, but we couldn’t have both representations for the ADT in the same program. In the MyString example from Abstract Data Types, MyString was a single class. When an abstract data type is represented just as a single class, without an interface, it’s harder to have multiple representations. The implementation is kept well and truly separated, in a different class altogether.Īnother advantage is that multiple different representations of the abstract data type can co-exist in the same program, as different classes implementing the interface. The client can’t create inadvertent dependencies on the ADT’s rep, because instance variables can’t be put in an interface at all. The interface is all a client programmer needs to read to understand the ADT. One advantage of this approach is that the interface specifies the contract for the client and nothing more. So one way to define an abstract data type in Java is as an interface, with its implementation as a class implementing that interface. Java’s interface is a useful language mechanism for expressing an abstract data type.Īn interface in Java is a list of method signatures, but no method bodies.Ī class implements an interface if it declares the interface in its implements clause, and provides method bodies for all of the interface’s methods.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |