1 / 23

Collage of Information Technology University of Palestine, Gaza Prepared by:

A dvanced P rogramming. Lecture 4: Creating and Using Interfaces. Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra. Outlines. Creating and Using Interfaces Introduction What Are Interfaces?

peggy
Download Presentation

Collage of Information Technology University of Palestine, Gaza Prepared by:

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. Advanced Programming Lecture 4: Creating and Using Interfaces Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra

  2. Outlines • Creating and Using Interfaces • Introduction • What Are Interfaces? • Class VS interface & Abstract class VS interface • Defining an Interface • Implementing an Interface • Extending interface • Using an Interface as a Type • Shape interface example

  3. قالوا قال النبي صى الله عليه وسلم: (من صام رمضان ثم أتبعه ستـًا من شوال كان كصيام الدهر) رواه مسلم رمضان = 30 يوم X 10 = 300 6 من شوال X 10 = 60 300 + 60 = سنة كاملة

  4. Java Quiz # 1 5 Min. • How we can enter many types in one array ? • What is the difference between overloading and overridden ? • Is the multiple inheritance supported in Java ?

  5. Introduction • There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a "contract" that spells out how their software interacts. • Each group should be able to write their code without any knowledge of how the other group's code is written. • Generally speaking, interfaces are such contracts.

  6. What Are Interfaces? • Definitions: • An interface is a collection of method definitions (without implementations) and constant values. • An interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies. • Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

  7. What Are Interfaces? • Data structures people refer to the interface as the abstract data type

  8. Class VS interface • A classdefinition can contain instance/class variables and instance/class methods, including both the signature and the body • An interface contains only the signatures of public instance methods (and named constants) • a java interface acts like a specification • it says what it means to be e.g. a rectangle • to be a rectangle , an object must offer at least those methods in its public interface

  9. Abstract class VS interface • The class is considered to be abstract class if at least one method in the class has no implementation (at least one abstract method). • But, all methods in interface have no implementation … • More is a home homework …

  10. Keyword The name of interface Signatures of methods note no bodies for the methods Defining an Interface • Defining an interface is similar to creating a new class:

  11. Defining an Interface • Modifiers !!!

  12. Implementing an Interface • To use an interface, you write a class that implements the interface. • When an insatiable class implements an interface, it provides a method body for each of the methods declared in the interface.

  13. Implementing Multiple Interfaces • In Java, a class can inherit from only one class but it can implement more than one interface. • Therefore, objects can have multiple types: • the type of their own class • the types of all the interfaces that they implement. • This means that if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.

  14. Implementing Multiple Interfaces • A product lass would be used in the shipping and billing departments.

  15. Extending interface • Interfaces can extend other interfaces, which brings rise to sub-interfaces and super-interfaces. • Unlike classes, however, an interface can extend more than one interface at time

  16. Using an Interface as a Type • When you define a new interface, you are defining a new reference data type. • You can use interface names anywhere you can use any other data type name. • If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.

  17. Using an Interface as a Type Class product implements Billable { } product x = new product (); Billable var = x;

  18. Shape interface example

  19. Iteration

  20. Programming pitfalls • Error: Leaving a method of an interface undefined in a class that implements the interface results in a compile error indicating that the class must be declared abstract.

  21. Next Lecture isa Inner class

  22. Thank You Questions ?!!

More Related