Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Object Oriented Programming
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.
Interfaces.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
Java Keywords CSE 115 Spring Purpose This set of slides contains a running list of the keywords that we have talked about so far this semester and.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
UML Class Diagram: class Rectangle
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Java Programming Review (Part I) Enterprise Systems Programming.
Abstract classes and Interfaces. Abstract classes.
Inheritance using Java
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Introduction to Object-Oriented Programming
Programming Languages and Paradigms Object-Oriented Programming.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Loops and Iteration for Statements, while Statements and do-while Statements.
Classes CS 21a: Introduction to Computing I First Semester,
Programming Languages and Paradigms Programming C++ Classes.
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.
Data Structures and Java CS /14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6:
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Interfaces & Abstract Classes (For CS III AP) March 2014.
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.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
1 More About Derived Classes and Inheritance Chapter 9.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Chapter 15 Abstract Classes and Interfaces
Sections Inheritance and Abstract Classes
Final and Abstract Classes
Interface.
UML Class Diagram: class Rectangle
Chapter 13 Abstract Classes and Interfaces
Inheritance, Polymorphism, and Interfaces. Oh My
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9: Polymorphism and Inheritance
Lecture 16 - Interfaces Professor Adams.
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Advanced Java Programming
CSE 1030: Implementing GUI Mark Shtern.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Programming Languages and Paradigms
Classes CS 21a: Introduction to Computing I
Final and Abstract Classes
Loops and Iteration CS 21a: Introduction to Computing I
Chengyu Sun California State University, Los Angeles
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of Horstmann text)

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. Inheritance Slide 2 Interfaces in Java Interface: collection of method signatures with no bodies Syntax public interface InterfaceName { public type methodName1(…); public type methodName2(…); … } A java class may implement an interface public class ClassName implements InterfaceName { // define methodName1, methodName2,... here }

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. Inheritance Slide 3 Two types of inheritance Class inheritance (extends) public class A extends B { … } Class A inherits fields and methods in class B public methods of B can be invoked on A objects, unless overridden in A Interface inheritance (implements) Also called implementation inheritance public class X implements Y { … } X must define all methods indicated in Y

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. Inheritance Slide 4 Why use interfaces? Interfaces enable and enforce “strict” sharing of methods among classes Recall that superclass variables can refer to subclass objects Suppose Y is an interface Y var; var can refer to an object of a class that implements Y var.methodName( … ) calls the actual method defined

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. Inheritance Slide 5 Example: Shape A Shape is anything that can be drawn: public interface Shape { public void draw(); } Any class that implements Shape must implement the draw() method Suppose Block, Triangle, and LetterT implement shape We can have an array of Shapes with elements referring to different kinds of objects Use a loop to call draw() on all of the shapes

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. Inheritance Slide 6 Example: Shape (and Block) public class Block implements Shape { private int size; public Block( int s ) { size = s; } public void draw() { for( int i = 1; i <= size; i++ ) { for( int j = 1; j <= size; j++ ) System.out.print( “*” ); System.out.println(); } Because it implements Shape, this class will not compile unless draw() is defined

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. Inheritance Slide 7 Example: array of Shapes null Shape[] list; … for ( int i = 0; i < 5; i++ ) { list[i].draw( ); } null list Block object Triangle object LetterT object Block object Triangle object

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. Inheritance Slide 8 Multiple inheritance Multiple (class) inheritance is not supported in Java public class A extends B1, B2, B3 { … } But a class may implement multiple interfaces public class X implements Y1, Y2, Y3 { … }  will not compile  will compile, but X must define methods in Y1, Y2, and Y3

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. Inheritance Slide 9 About interfaces Interfaces cannot be instantiated  Shape s = new Shape(); Interfaces can be extended public interface Y1 extends Y2 { … } public class X implements Y1 { … } X must define methods indicated in Y1 and Y2 Interfaces cannot declare instance fields and may not have any method definitions Use an abstract class instead