200 likes | 368 Vues
Encapsulation and Java Interface. Chapter 3. Chapter Contents. Encapsulation (capsule, details hidden) Java Interfaces Writing an Interface Implementing an Interface An Interface as a Data Type Interfaces Versus Abstract Classes. Encapsulation.
E N D
Encapsulation and Java Interface Chapter 3
Chapter Contents • Encapsulation (capsule, details hidden) • Java Interfaces • Writing an Interface • Implementing an Interface • An Interface as a Data Type • Interfaces Versus Abstract Classes
Encapsulation An automobile's controls are visible to the driver, but its inner workings are hidden.
Encapsulation • Hides the fine detail of the inner workings of the class • The implementation is hidden • Often called "information hiding" • Part of the class is visible • The necessary controls for the class are left visible • The class interface is made visible, contains method declarations. • The programmer is given only enough information to use the class
Abstraction in Class Design • A process that focus on what instead of how • What you want to do with the data • The designer does not consider how the class's methods will accomplish their goals, and how the data will be represented • The client interface is the what • The implementation is the how
Abstraction • Separation of specification from implementation in a class definition • Interface • Implementation • Interface describe things a programmer needs to know to use the class. • Implementation defines the details of how the methods are done
Java Interface • A program component that contains • Public constants • Signatures for public methods • Comments that describe them • Java has its own commenting style • Begins like a class definition • Use the word interface instead of classpublic interface someClass{ public int someMethod(); • // more methods declarations…}
Java Interface Example public interface NameInterface { /** Task: Sets the first and last names. * @param firstName a string that is the desired first name * @param lastName a string that is the desired last name */ public void setName(String firstName, String lastName); /** Task: Gets the full name. * @return a string containing the first and last names */ public String getName(); public void setFirst(String firstName); public String getFirst(); public void setLast(String lastName); public String getLast(); public void giveLastNameTo(NameInterface child); public String toString(); } // end NameInterface
Implementing an Interface • The class must implement every method declared in the interface • A class that implements an interface must state so at start of definitionpublic class myClass implements someInterface • Multiple classes can implement the same interface • A class can implement more than one interface • public class A extends B implements InterfaceA, InterfaceB
Interface as Data Type • An interface can be used as a data typepublic void someMethod (someInterface x) • Any argument passed to this method must be an object of a class that implements someInterface. • Interface is independent of any class. • Ensure that the argument can invoke all and only those methods declared in the interface. • Q: • What if a class A does not begin with the phrase implements someInterface, yet still implements the methods in the interface? Could you pass an instance of C to someMethod?
Implementing an Interface The files for an interface, a class that implements the interface, and the client.
Extending an Interface • You can derive another interface from existing interface. • You can derive an interface from several interfaces, even though you cannot derive a class from several classes. • When you extends another interface, it has all the methods of the inherited interface plus some new methods.
Example public interface Nameable { public void setName(String petName); Public String getName(); } public interface Callable extends Nameable { public void come(String petName); } public interface Capable { public void hear(); public void respond(); } public interface Trainable extends Callable, Capable { public void sit(); public void speak(); }
Interfaces Versus Abstract Classes • Purpose of interface similar to purpose of abstract class • But … an interface is not a base class • It is not a class of any kind • Use an abstract base class when • You need a method or private data field that classes will have in common • Otherwise use an interface • A class can implement several interfaces and can extend only one abstract class