I NTERFACES Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University of Hail Chapter.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Topic 5a – Interfaces Friends share all things. CISC370/Object Oriented Programming with Java.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
Chapter 10: Inheritance and Polymorphism
CS102--Object Oriented Programming Lecture 15: Interfaces Copyright © 2008 Xiaoyan Li.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Object Oriented Design and UML
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
COP 3003 Object-Oriented Programming - Polymorphism Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Java Interfaces. Interfaces An interface is something like an extreme case of an abstract class – However, an interface is not a class – It is a type.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Interfaces An interface is like an extreme case of an abstract class – However, an interface is not a class – It is a type that can be satisfied by any.
Interfaces and Inner Classes
1 Interface Design. 2 concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
BY:- TOPS Technologies
Interfaces CMSC 202. Public Interfaces Objects define their interaction with the outside world through the their public interface. A class' public interface.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 12 Inheritance.
Comp 249 Programming Methodology
Interfaces and Inner Classes
CS 302 Week 11 Jim Williams, PhD.
CMSC 202 Interfaces.
Java Programming Language
Lecture 16 - Interfaces Professor Adams.
Interfaces and Inner Classes
Inheritance Inheritance is a fundamental Object Oriented concept
CMSC 202 Interfaces.
Topics OOP Review Inheritance Review Abstract Classes
CMSC 202 Interfaces.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

I NTERFACES Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University of Hail Chapter 05

I NTERFACE An interface is something like an extreme case of an abstract class An abstract class can have non abstract methods But interface must have abstract methods only. However an interface is not a class It is a type that can be satisfied by any class that implements the interface 2 Simab Shahid UOH, Girls Branch

I NTERFACE S YNTAX o The syntax for defining an interface is similar to that of defining a class  Except the word interface is used in place of class o An interface specifies a set of methods that any class that implements the interface must have 3 Simab Shahid UOH, Girls Branch

I NTERFACE EXAMPLE interface FirstHelloInterface { void sayHello(); } Important Point: Method declared in interface is implicitly public and abstract 4 Simab Shahid UOH, Girls Branch

I MPORTANT ABOUT INTERFACE We cant create object of an interface just like we cant create object of an abstract class Because both the interface and abstract class have abstract methods Rather we create object of a class that implements the interface. 5 Simab Shahid UOH, Girls Branch

C LASS IMPLEMENTING AN INTERFACE If the class is not implementing all the abstract methods of given interface, then the class must be declared as abstract class. In the example below Cat is declared as abstract, coz it is not implementing the abstract method “void eat()” interface Animal { void talk( ); // abstract method void eat( ); // abstract method } public abstract class Cat implements Animal { void talk( ) // class is implementing this method { Sytem.out.println(“ Cat says Mao Mao”); } abstract void eat( ); // abstract method } 6 Simab Shahid UOH, Girls Branch

C LASS IMPLEMENTING AN INTERFACE If the class is implementing all the abstract methods of given interface, then the class will be declared as concrete class. In the example below Cat is declared as concrete class, because it is implementing all the abstract methods of interface “Animal” interface Animal { void talk( ); // abstract method void eat( ); // abstract method } public class Cat implements Animal { void talk( ) // class is implementing this method { Sytem.out.println(“ Cat says Mao Mao…..”); } void eat( ) { Sytem.out.println(“ Cat eats meat…..”); } 7 Simab Shahid UOH, Girls Branch

I NTERFACE Interface contains method headings and constant definitions only interface shape { String baseclass = "shap"; // constant public void Draw(); // method heading } 8 Simab Shahid UOH, Girls Branch

I NTERFACES Interface contains neither instance variables nor any complete method definitions interface shap { public String baseclass; // wrong Shap(){ } // wrong not allowed public void Draw() { System.out.println(“Implementation of Draw() Method”);/ /wrong } 9 Simab Shahid UOH, Girls Branch

I NTERFACES Interface cannot be instantiated interface A{ public void display( String s); } public class InterfaceExample { public static void main( String args[] ) { A objOfA = new A(); //WRONG: not allowed to create object of A(Interface) } 10 Simab Shahid UOH, Girls Branch

I NTERFACE o Interface variable is implicitly public, static and final o Interface method is implicitly public and abstract (is not implemented by this class) o A class can implement one or more interfaces o An interface can be implemented by several classes o Interface name can be used as the type of a variable i.e interface reference 11 Simab Shahid UOH, Girls Branch

I NTERFACE o An interface and all of its method headings should be declared public  They cannot be given private, protected, or package access o When a class implements an interface, it must make all the methods in the interface public o Because an interface is a type, a method may be written with a parameter of an interface type  That parameter will accept as an argument any class that implements the interface 12 Simab Shahid UOH, Girls Branch

I NTERFACE (E XAMPLES ) interface Communicate { int LOUD = 0; int SOFT = 1; int OFF = 2; void talk(); void listen(); } class Telephone implements Communicate { //implementation of talk() method of interface Communicate public void talk() { System.out.println(“Talk Method”); } //implementation of listen() method of Communicate interface public void listen(){ System.out.println(“Listen Method”); } //implementation of call() method,call() method is not member of //Communicate interface public void call( String number) { System.out.println(“Call Method”); } } 13 Simab Shahid UOH, Girls Branch

I NTERFACE (E XAMPLE ) class Professor implements Communicate { public void talk() { System.out.println(“Talk Method”); } public void listen() { System.out.println(“Listen Method”); } // other methods implemented void Lecture( String topic) { } } The keyword implements indicates that the class implements one or more interfaces. 14 Simab Shahid UOH, Girls Branch

E XERCISE (I NTERFACE ) 15 Simab Shahid UOH, Girls Branch // What compile-time error generated for this program? interface B { void display(); } class D0 { } class D1 implements B { public void display() { System.out.println( "D1" ); } class D2 implements B { public void display() { System.out.println( "D2" ); } class InterfaceRefVariable { public static void main( String [] args) { B b = new D0(); b.display(); b = new D1(); b.display(); b = new D2(); b.display(); }

16 Simab Shahid UOH, Girls Branch // Interface extends one or more interfaces interface J { int i=200; int J1(); } interface K { double K1(); } interface L extends J, K { boolean L1(); } class I implements L { public int J1() { return 4; } public double K1() { return 7.98; } public boolean L1() { return true ; } class InterfaceInheritance { public static void main( String[] args) { I a = new I(); System.out.println(a.i); System.out.println(a.J1()); System.out.println(a.K1()); System.out.println(a.L1()); }

I NTERFACE o The keyword extends is used to define an inheritance relationship between interfaces. o An interface may directly extend multiple interfaces. 17 Simab Shahid UOH, Girls Branch interface L1 { void f(); void g(); } interface L2 extends L1 { void f(); int g(); } class CE { public static void main(String [] args) { System.out.println( "Hello" ); } The following program generates a compile-time error.  Interface L1 declares f() and g(). Interface L2 declares the same except return type of g() is different. The compiler identifies it as an error.

I NSTANCEOF O PERATOR The instanceof operator is used to determine if an object is of a particular class or implements a specific interface. o Syntax: varName instanceof type o varName is an object reference variable o type is the name of either a class or an interface The expression evaluates to true if varName is a type. Otherwise, it evaluates to false. 18 Simab Shahid UOH, Girls Branch

I NSTANCEOF O PERATOR (E XAMPLE ) 19 Simab Shahid UOH, Girls Branch abstract class Fish { abstract void display(); } abstract class SaltWtrFish extends Fish { } abstract class FreshWtrFish extends Fish { } class Trout extends FreshWtrFish { void display() { System.out.println( "Trout" ); } class Tuna extends SaltWtrFish { void display() { System.out.println( "Tuna" ); } class InstantofOperator { public static void main( String[] arg) { Fish f[] = new Fish[3]; f[0] = new Trout(); f[1] = new Tuna(); f[2] = new Trout(); for (int j = 0; j < 3; j++) if ( f[j] instanceof Trout ) f[j].display(); } Output: Trout

I NSTANCEOF O PERATOR (E XAMPLE ) 20 Simab Shahid UOH, Girls Branch interface Vehicle { void drive(); } abstract class Mammal { } class Bear extends Mammal { } class Elephant extends Mammal implements Vehicle { public void drive() { System.out.println( "Elephant: Drive" ); } class Horse extends Mammal implements Vehicle { public void drive() { System.out.println( "Horse:Drive" ); } class Lion extends Mammal{ } class InstantofInterface { public static void main( String[] ar) { Mammal m[] = new Mammal[4]; m[0]= new Elephant(); m[1]= new Bear(); m[2]= new Horse(); m[3]= new Lion(); for ( int j = 0; j < 4; j++) { if ( m[j] instanceof Vehicle) { Vehicle v = (Vehicle)m[j]; v.drive(); } Output: Elephant: Drive Horse: Drive

E XERCISE (I NSTANCEOF OPERATOR ) Write a program that demonstrate the instanceof operator. Declare interface I1 and I2. Interface I3 extends both of these interface. Also declare interface I4. Class X implements I3. Class W extends X and implements I4. Create an object of class W. Use the instanceof operator to test if object implements each of the interface and is of type X. 21 Simab Shahid UOH, Girls Branch

C OMPARABLE I NTERFACE o Chapter 4 discussed the Merge Sort and the quick sort algorithms, and examined a method for sorting an array into increasing order  This code could be modified to sort into decreasing order, or to sort strings instead  Each of these methods would be essentially the same, but making each modification would be a nuisance. o The only difference would be the types of values being sorted, and the definition of the ordering. o Using the Comparable interface could provide a single sorting method that covers all these cases. 22 Simab Shahid UOH, Girls Branch

C OMPARABLE I NTERFACE o The Comparable interface is in the java.lang package, and so is automatically available to any program. o It has only the following method heading that must be implemented: public int compareTo(Object other); o It is the programmer's responsibility to follow the semantics of the Comparable interface when implementing it. 23 Simab Shahid UOH, Girls Branch

import java.lang ; public class Employee implements Comparable { int EmpID; String Ename; double Sal; public Employee( String Ename, double Sal) { EmpID ++; this. Ename =Ename; this. Sal = Sal; } public int compareTo(Object o1) { if ( this.Sal == ((Employee) o1).Sal) { return 0; } else if (( this.Sal) > ((Employee) o1).Sal) { return 1; } else return -1; } 24 Simab Shahid UOH, Girls Branch

Class Test { public static void main( String[] arg) { Employee Obj1 = new Employee(“Clary ”, 4000 ); Employee Obj2 = new Employee(“Amna ”, 6000 ); int returnValue = Obj2. compareTo(Obj1) if ( returnValue ==0 ) System.out.println(“ Both the employees have equal salaries ”); if ( returnValue >0 ) System.out.println(“Employee 1 salary is high ”); if ( returnValue < 0 ) System.out.println(“Employee 2 salary is high ”); } 25 Simab Shahid UOH, Girls Branch

C OMPARABLE I NTERFACE o The method compareTo must return:  A negative number if the calling object "comes before" the parameter other.  A zero if the calling object "equals" the parameter other.  A positive number if the calling object "comes after" the parameter other. o If the parameter other is not of the same type as the class being defined, then a ClassCastException should be thrown. 26 Simab Shahid UOH, Girls Branch

I NNER CLASSES Inner classes are classes defined within other classes. The class that includes the inner class is called the outer class. public class University { String name; String city; private class Student { private String ID ; private int Age ; private String Grade ; } o Here University is Outer class & Student is Inner class 27 Simab Shahid UOH, Girls Branch

P LACEMENT OF INNER CLASS IN O UTER C LASS o There is no particular location where the definition of the inner class (or classes) must be place within the outer class. o Placing it first or last, however, will guarantee that it is easy to find. 28 Simab Shahid UOH, Girls Branch

public class Person { private double age; Person() { age = 0.0; } public void setAge( double age) { this.age = age; } //Good to writeInner class at the //End, because it is easy to find it public class Name{ String f_name; String m_name; String l_name; } 29 Simab Shahid UOH, Girls Branch public class Person { //Good to write Inner class in the start, //because it is easy to find it public class Name{ String f_name; String m_name; String l_name; } private double age; Person() { age = 0.0; } public void setAge( double age) { this.age = age; } Example 01Example 02

30 Simab Shahid UOH, Girls Branch public class Person { private double age; Person() { age = 0.0; } //Not Good to write Inner class in the middle, because it is difficult to find it public class Name{ String f_name; String m_name; String l_name; } public void setAge( double age) { this.age = age; } Example 03

I NNER C LASS An inner class definition is a member of the outer class in the same way that the instance variables and methods of the outer class are members. In the previous example the class Person has three members private variable “ age” public function s etAge() And inner class Name. An inner class is local to the outer class definition. o The name of an inner class may be reused for something else outside the outer class definition. o If the inner class is private, then the inner class cannot be accessed by name outside the definition of the outer class. 31 Simab Shahid UOH, Girls Branch

U SES OF I NNER CLASSES o Within the definition of a method of an inner class:  It is legal to reference a private instance variable of the outer class  It is legal to invoke a private method of the outer class o Within the definition of a method of the outer class  It is legal to reference a private instance variable of the inner class on an object of the inner class  It is legal to invoke a (non static) method of the inner class as long as an object of the inner class is used as a calling object o Within the definition of the inner or outer classes, the modifiers public and private are equivalent. 32 Simab Shahid UOH, Girls Branch

I NNER C LASSES E XAMPLE 33 Simab Shahid UOH, Girls Branch public class University{ private class Student { private String ID ; private int Age ; private String Grade ; public Student (String I, int A, String G) { this.ID = I ; this.Age = A ; this.Grade = G ; } public String getGrade( ) { return Grade ; } public String getStudentInfo( ) { return ID +” ” +Age; } } // end of inner class private Student S ; public String getInfo( ) { return S.Grade + “ “ + S.getStudentInfo() ; } } // end of outerclass

A DVANTAGES I NNER C LASSES o There are two main advantages to inner classes.  They can make the outer class more self-contained since they are defined inside a class.  Both of their methods have access to each other's private methods and instance variables. o Using an inner class as a helping class is one of the most useful applications of inner classes.  If used as a helping class, an inner class should be marked private. 34 Simab Shahid UOH, Girls Branch