Interfaces & Abstract Classes (For CS III AP) March 2014.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Java Programming Abstract classes and Interfaces.
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.
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
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Java Interfaces Overview Java Interfaces: A Definition.
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Inheritance and Access Control CS 162 (Summer 2009)
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
ABSTRACT DATA TYPES, ABSTRACT CLASSES AND INTERFACES And abstract art….
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
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.
Chapter 11 Polymorphism. Contents I. Polymorphism 1. Polymorphism 2. Polymorphism and Dynamic Binding 3. The “is-a” Relationship Does Not Work in Reverse.
Modern Programming Tools And Techniques-I
Chapter 15 Abstract Classes and Interfaces
Lecture 12 Inheritance.
EKT 472: Object Oriented Programming
Interface.
Chapter 11: Inheritance and Polymorphism
UML Class Diagram: class Rectangle
Chapter 13 Abstract Classes and Interfaces
Interface.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Implementing Non-Static Features
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Final and Abstract Classes
Abstract Classes and Interfaces
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Interfaces & Abstract Classes (For CS III AP) March 2014

Definitions Interface – collection of methods for which all ‘members’ must have implementations What are members ? – any classes implementing the Interface Interfaces are often very general nouns, such as a Shape interface, a Sports interface, which can in theory be used in a wide variety of applications (not just to one program) ^In this sense it is different from extending a superclass In fact, an interface cannot even be instantiated, so it cannot have a Constructor

Defining an Interface public interface interfaceName (e.g. Shape ) { /*method signatures & return types *are listed*/ public int getPerimeter(); public int getArea (); public void printName (); … } All of these are ‘abstract’ methods since they do not have a given implementa- tion *More specifically, they are given implementations in the class implementing the Interface.

An Example Class (Implementing the Interface) public class className (e.g. Rectangle ) implements Shape { /*method implementations and data *are listed*/ int width, height; // Assume a Constructor already exists public int getPerimeter() { return ((2 * width) + (2 * height)); } public int getArea () { return width * height; } public void printName () { System.out.println (“Rectangle”); } … } Here’s a challenge: What if we wanted to make a.toString() method instead?

Examples Comparable – very common example of Interface in CS III (and later CS classes!) – What are the methods in this example? obj1.compareTo (Object obj2); Returns -1, 0, or 1 if obj1 has a value less than, equal to, or greater than that of obj2 There are other Interfaces as well! – Collection, Set, Map, etc. – Data Structures – Runnable – threaded programming

Other Important Interface Laws Never write: Shape thisShape = new Shape(); No data fields are included in interfaces, except constants (static final). No methods can be implemented directly in an interface; all must be abstract. A class can extend (have a formal Is-A relationship with) only one superclass, but it may implement multiple interfaces

UML Diagrams Help Specify These Class Relationships

An Alternative to Interfaces Abstract classes –alternatives to interfaces, but different in many ways: – More application-specific – Can include some implementations and/or data fields – However – you still cannot create an instance of an abstract class; subclasses are expected to be used. (Teukolsky 139)

Thank you very much This will be posted on the CS Club website: Any questions? Next meeting will be 27 th March. Some other Sources, for your Interest: – erface.html erface.html – nterface.html nterface.html – m m