Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Object Oriented Programming
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
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.
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Java Interfaces Overview Java Interfaces: A Definition.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Abstract Classes and Interfaces
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
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.
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.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Peyman Dodangeh Sharif University of Technology Fall 2014.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Web Design & Development Lecture 9
Sixth Lecture ArrayList Abstract Class and Interface
Objects as a programming concept
Inheritance and Polymorphism
EKT 472: Object Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
CMSC 202 Interfaces.
Java Programming Language
Advanced Java Programming
Java Inheritance.
CMSC 202 Interfaces.
Object-Oriented Programming
Chapter 14 Abstract Classes and Interfaces
Abstract Classes and Interfaces
Review of Previous Lesson
Abstract Classes and Interfaces
CMSC 202 Interfaces.
Presentation transcript:

Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces Cannot Grow Abstract Classes Versus Interfaces

Unit 032 What is an Interface? We have seen that inheritance allows code reusability - a subclass inherits the properties of its super class. Inheritance also allows type sharing - an instance of a subclass is of type its class, its superclass, and any class up its class-hierarchy tree. But Java allows only single inheritance. So are there other ways of achieving more of the above advantages? Regarding having more code reusability, the answer is NO. However, regarding having more type sharing, the answer is YES. It is possible for an object to be of a type outside its inheritance tree - This is what interfaces are about.

Unit 033 What is an Interface? –Cont’d Interfaces are used to realize some of the advantages of multiple inheritance, while avoiding its complexity. For example, both GraduateStudent and Faculty might implement the Instructor interface. Interfaces only declare methods that objects must have but with no implementation - all methods are abstract. Interfaces differ from abstract classes in that: –They cannot have implemented methods –No instance variables except constants. –No constructors Interface names are often adjectives, ending in -able. examples: Colorable, Rotatable, Comparable, Runnable Interfaces that specify roles can be named with nouns. examples: Container, Mover, Instructor

Unit 034 Interface Declaration Syntax Interfaces are declared similar to classes, except "interface" is used instead of "class". 1.public interface Instructor{ 2. int universityCode = 31261; 3. String getOfficeHours(); 4.Course[] getTeachingCourses(); 5.} All methods in an interface are automatically public and abstract. All fields in an interface are automatically static and final. Thus, the above declaration is the same as the following: 1.public interface Instructor{ 2. static final int universityCode = 31261; 3.public abstract String getOfficeHours(); 4.public abstract Course[] getTeachingCourses(); 5.}

Unit 035 Implementing Interfaces A class implements an interface similar to the way it extends a superclass but using "implements" instead of "extends". A class can extend another class and at the same time implement one or more interfaces. 1.public class GraduateStudent extends Student implements Instructor{ 2.private String thesisTitle; 3.private String officeHours; 4.private Course[] teachingCourses; 5.public GraduateStudent(int id, String name, double gpa, String title){ 6.super(id, name, workLoad); 7.thesisTitle = title; 8.} 9.//... other methods 10.public String getOfficeHours(){ 11.return officeHours(); 12.} 13.public Course[] getTeachingCourses(){ 14.return teachingCourses; 15.} 16.}

Unit 036 Implementing Interfaces – Cont’d Note: although methods of an interface are automatically public, they must be specified as such in the implementing class. If a class implements more than one interface, the interface names are separated by commas. What happens if a class does not define all the methods of an interface it claims to implement? Same thing that happens if an abstract method of a superclass is not defined - the implementing class is abstract. If a super class implements an interface, then all its subclasses are considered to implement it too.

Unit 037 Using Interfaces as Types Interfaces can be used as types of variables and parameters. For example, an object of GraduateStudent can be of type GraduateStudent, Student, Object or Instructor. Similarly, if Faculty is defined as shown below, then its instance can be of type Faculty, MonthlyEmployee, Employee, Instructor, Comparable, or Object. Thus, a method with the following header can accept both an object of Faculty or that of GraduateStudent.

Unit 038 Interfaces and Inheritance Like classes, interfaces can extend other interfaces For example, if we have a Container interface, we can extend it to have SearchableContainer interface. 1.public interface Container{ 2.void add(Object o); 3.int getCount(); 4.boolean isEmpty(); 5.boolean isFull(); 6.void purge(); 7.} 1.public interface SearchableContainer extends Container{ 2.boolean isMember(Object object); 3.void remove(Object object); 4.} Unlike classes, interfaces can extend any number of other interfaces This is because interfaces merely declare methods, but do not implement, so no collision can occur as in multiple inheritance.

Unit 039 Conflicting Methods in Interfaces

Unit 0310 Interfaces Cannot Grow It important to think through very well about the methods (and their signatures) when designing an interface. This is because Interfaces are like foundation to a building. To make changes, the whole building must be destroyed. If you add a single method to an interface or change the signature of a method, then all classes that implement the old Interface will break because they don't implement the interface anymore! The only solution in this case is to extend the interface.

Unit 0311 Abstract Classes Versus Interfaces InterfacesAbstract Classes Defines a set of methodsModels an object with properties and methods Factors out common methods of potentially dissimilar objects Factors out common properties and methods of similar objects Does not implement its methodsMay implement some or all of its methods A class can implement multiple interfaces A class can extend only one abstract class

Unit 0312 Exercises Write each of the following: 1.an interface, Instructor, which has two methods: String getOfficeHours() and Course[ ] getTeachingCourses(). 2.a class, Course, with instance variables: courseCode, creditHours, and title; and appropriate accessor, mutator and toString methods. 3.a class Faculty that extends MonthlyEmpolyee and implements the Instructor interface. It has two additional instance variables: String officeHourse and Course[] teachingCourses. 4.classes, Student and GraduateStudents as described in page 3 of the inheritance session. 5.a class, ReaseachAssistant, that extends GraduateStudent and implements the Instructor interface 6.A test class, TestInstructors, that has the following methods: void PrintTeachingDetails(Instructor i): that prints the office hours and teaching courses for i; and a main method, that creates instances of Faculty and ReasearchAssistant and prints their teaching details.