Presentation is loading. Please wait.

Presentation is loading. Please wait.

Encapsulation and Java Interface

Similar presentations


Presentation on theme: "Encapsulation and Java Interface"— Presentation transcript:

1 Encapsulation and Java Interface
Chapter 3

2 Chapter Contents Encapsulation (capsule, details hidden)
Java Interfaces Writing an Interface Implementing an Interface An Interface as a Data Type Interfaces Versus Abstract Classes

3 Encapsulation An automobile's controls are visible to the driver, but its inner workings are hidden.

4 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

5 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

6 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

7 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 class public interface someClass { public int someMethod(); // more methods declarations… }

8 Java Interface Example
public interface NameInterface { /** Task: Sets the first and last names. firstName a string that is the desired first name lastName a string that is the desired last name */ public void setName(String firstName, String lastName); /** Task: Gets the full name. 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

9 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 definition public 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

10 Interface as Data Type An interface can be used as a data type public 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?

11 Implementing an Interface
The files for an interface, a class that implements the interface, and the client.

12 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.

13 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();

14 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


Download ppt "Encapsulation and Java Interface"

Similar presentations


Ads by Google