Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 23 : Generics King Fahd University of Petroleum & Minerals College of Computer Science.
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
CS102--Object Oriented Programming Lecture 15: Interfaces Copyright © 2008 Xiaoyan Li.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
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.
Inheritance using Java
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
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.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
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
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 14 : Swing II King Fahd University of Petroleum & Minerals College of Computer Science.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Inheritance and Polymorphism
Interfaces Professor Evan Korth.
Packages, Interfaces & Exception Handling
Comp 249 Programming Methodology
Chapter 13 Abstract Classes and Interfaces
Comp 249 Programming Methodology
Comp 249 Programming Methodology
Interface.
CMSC 202 Interfaces.
Advanced Java Programming
CSE 1030: Implementing GUI Mark Shtern.
Interfaces and Inner Classes
Java Inheritance.
CMSC 202 Interfaces.
Chapter 14 Abstract Classes and Interfaces
Abstract Classes and Interfaces
Final and Abstract Classes
Abstract Classes and Interfaces
CMSC 202 Interfaces.
Inheritance Lakshmish Ramaswamy.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Inheritance Single Vs Multiple Inheritance Base_Class_1 Derived_Class_1Derived_Class_2Derived_Class_3

Multiple Inheritance : when a class inherits from two or more different base classes : Multiple inheritance can create significant complications because the implementations that are inherited could be conflicting Single Vs Multiple Inheritance Base_Class_1 Derived_Class_1Derived_Class_2Derived_Class_3 Base_Class_2

Student StudentEmployee Employee Person Difficulty with Multiple Inheritance String name getHours() How many name will be in StudentEmployee? Which version of getHours() will be used in StudentEmployee?

Multiple Inheritance in Java Multiple inheritance is not allowed for classes. Multiple inheritance is allowed only for interfaces. An interface can be seen as a class without any implementation. So Interfaces are Java ’ s way of approximating multiple inheritance. What is an interface? next slide …

Interfaces An interface is something 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 class that implements the interface The syntax for defining an interface is similar to that of defining a class Except the word interface is used in place of class An interface specifies a set of methods that any class that implements the interface must have It contains method headings and constant definitions only It contains no instance variables nor any complete method definitions An interface and all of its method headings should be declared public They cannot be given private, protected, or package access

The Ordered Interface

Interfaces To implement an interface, a concrete class must do two things: It must include the phrase implements Interface_Name at the start of the class definition ( If more than one interface is implemented, each is listed, separated by commas) The class must implement all the method headings listed in the definition(s) of the interface(s)

Implementation of an Interface

Abstract Classes Implementing Interfaces Abstract classes may implement one or more interfaces Any method headings given in the interface that are not given definitions are made into abstract methods A concrete class must give definitions for all the method headings given in the abstract class and the interface

An Abstract Class Implementing an Interface

Derived Interfaces Like classes, an interface may be derived from a base interface This is called extending the interface The derived interface must include the phrase extends BaseInterfaceName A concrete class that implements a derived interface must have definitions for any methods in the derived interface as well as any methods in the base interface

Extending an Interface

Defined Constants in Interfaces An interface can contain defined constants in addition to or instead of method headings Any variables defined in an interface must be public, static, and final Because this is understood, Java allows these modifiers to be omitted Any class that implements the interface has access to these defined constants

Pitfall: Inconsistent Interfaces When a class implements two interfaces: One type of inconsistency will occur if the interfaces have constants with the same name, but with different values In this case, the class will be compiled correctly but when any of the constants is accessed via a reference of the class type, a compilation error will occur. If the reference is up-casted to one of the interfaces, the constant from that interface will be used without any error Another type of inconsistency will occur if the interfaces contain methods with the same name but different return types This is an error, the class will not compile

The end