1 / 13

Interfaces and Abstract Classes

Interfaces and Abstract Classes. March 4, 2005. Outline. More abstract trivia Java interfaces. Stuff from last time. Abstract methods do not have implementations Static member functions can not be overridden Classes that are declared final can not have derived classes

sandro
Télécharger la présentation

Interfaces and Abstract Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interfaces and Abstract Classes March 4, 2005

  2. Outline • More abstract trivia • Java interfaces

  3. Stuff from last time • Abstract methods do not have implementations • Static member functions can not be overridden • Classes that are declared final can not have derived classes • The super keyword can be used within a derived class in order to call a method currently overridden by the derived class.

  4. Constructor Trivia • Constructors are not inherited • Use this for calling other constructors of the same class • Calling a different constructor using this or super must be the first line in the constructor’s implementation

  5. Guidelines About Abstract Classes • When you want to declare a method to indicate something that should be done, but you want to defer the implementation to a subclass, you use an abstract class. • You can mix implementing (concrete) methods and abstract methods in an abstract class.

  6. Multiple Inheritance • Allowed in C++ • Not allowed in Java

  7. Interfaces • Java’s version of a completely abstract class • Generally, a interface defines what a class does, instead of what a class is. • A class can implement zero or more interfaces. • If a class does implement an interface, it must implement ALL methods defined in the interface. • Unless it is abstract • Interfaces can extend other interfaces.

  8. Example public interface MyInterface { void aMethod(int i); } public class MyClass implements MyInterface { public void aMethod(int i) { //implementation } }

  9. Why use Interfaces? • They encourage smart application design • They promote readability • They promote maintainability • Interface as a Supertype • They allow flexibility in your class definitions • Multiple “Inheritance”

  10. Rules for Writing an Interface • Interfaces can not extend classes. • Interface methods are implicitly abstract; they can not be final. • Interface methods are implicitly public. • Interfaces can define variables, but each variable must be declared public, static, and final.

  11. Is and Does • Whereas an abstract class defines what something is, an interface defines what it can do • Abstract class or Interface? • Person • Employee • Student • Programmer • Skier • Wild Animal • Data Accessor

  12. Subtypes and Interfaces • If interface I extends interface J, then I is a subtype of J. • If class C implements interface I, then C is a subtype of I. •  interfaces I, I is a subtype of Object.

  13. Name Collisions among Interfaces • A class implementing two different interfaces containing functions with the same name • Different signatures -> overloading • Same signature/return type -> collapsed into 1 function • Same signature/different return type -> compilation error

More Related