230 likes | 393 Views
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?
E N D
Advanced Programming 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? • Class VS interface & Abstract class VS interface • Defining an Interface • Implementing an Interface • Extending interface • Using an Interface as a Type • Shape interface example
قالوا قال النبي صى الله عليه وسلم: (من صام رمضان ثم أتبعه ستـًا من شوال كان كصيام الدهر) رواه مسلم رمضان = 30 يوم X 10 = 300 6 من شوال X 10 = 60 300 + 60 = سنة كاملة
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 ?
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.
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.
What Are Interfaces? • Data structures people refer to the interface as the abstract data type
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
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 …
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:
Defining an Interface • Modifiers !!!
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.
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.
Implementing Multiple Interfaces • A product lass would be used in the shipping and billing departments.
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
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.
Using an Interface as a Type Class product implements Billable { } product x = new product (); Billable var = x;
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.
Next Lecture isa Inner class
Thank You Questions ?!!