©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.

Slides:



Advertisements
Similar presentations
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advertisements

Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13  Inheritance and Polymorphism  Access Modifiers  Abstract.
1 interfaces Professor Evan Korth. review Based on the GeometricObject -> Circle -> Cylinder hierarchy from last class, which of these are legal? GeometricObject.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Polymorphism & Interfaces
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 10 Abstract Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Abstract Classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces.
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)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
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.
Lecture 5:Interfaces and Abstract Classes
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Advanced Programming in Java
Advanced Programming in Java
Abstract Classes and Interfaces in Java Reference: COS240 Syllabus
Interfaces Professor Evan Korth.
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Interface.
Chapter 12 Abstract Classes and Interfaces
Inheritance and Polymorphism
Advanced Programming in Java
Advanced Programming in Java
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 13 Abstract Classes and Interfaces Part 01
Presentation transcript:

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance allows you to derive new classes from existing classes known as inheritance

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Using Classes Effectively with Polymorphism Polymorphism allows a single variable to refer to objects from different classes. Also, polymorphism denotes the principle that behavior (methods) can vary (be called) depending on the actual type of an object. The actual type of the object determines the method to be called.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract Superclasses and Abstract Methods Abstract classes are like regular classes with data and methods, but you cannot create instances (objects) of abstract classes using the new operator. An abstract method cannot be placed in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be declared abstract..

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract Superclasses and Abstract Methods A classes that contains abstract methods must be abstract. However, it is possible to declare an abstract class that contains no abstract methods. A subclass can be abstract even if its superclass is concrete..

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract Superclasses and Abstract Methods When we define a superclass, we often do not need to create any instances of the superclass. Depending on whether we need to create instances (objects) of the superclass, we must define the class differently. We will study examples based on the Animal and Fruit super classes defined later.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract Superclasses and Abstract Methods An abstract class is a class defined with the modifier abstract. No instances can be created from an abstract class.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance versus Interface Java interface and inheritance both model an IS-A relationship: class ButtonHandler implements ActionListener Class SavingsAccount extends Account We say “ButtonHandler is an ActionListener” and “SavingsAccount is an Account.”

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance versus Interface However, their uses are very different. The Java interface is used to share common behavior (only method headers) among the instances of different classes. Inheritance is used to share common code (including both data members and methods) among the instances of related classes.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance versus Interface In your program designs, remember to use the Java interface to share common behavior. Use inheritance to share common code. If an entity A is a specialized form of another entity B, then model them by using inheritance. Declare A as a subclass of B.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces What is an interface? Creating an interface Implementing an Interface What is a Marker interface?

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interface An interface is a classlike construct. It contains only constants and abstract methods. An interface is treated like a a special class in Java. Each interface is compiled into a separate bytecode file same as a regular class. You cannot create an instance (object) from an interface using the new operator. Multiple classes can implement the same interface type The java.lang.Comparable interface defines the compareTo method. Many classes in the Java library implement Comparable.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display Define an Interface The data must be constants. Each method in an interface has only a signature without implementation. Syntax: public interface NewInterface { /** Constants declarations */ public static final int CONSTANT_J = 2; /** Method signatures*/ public abstract void AbstractMethod(); }

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Define an Interface Java allows only single inheritance for class extensions. Java allows multiple extensions for interfaces. public class SubClassName extends SuperClass implements Interface1, … InterfaceN { }

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interface Comparable //Interface for comparing objects, defined in java.lang // The compareTo method determines the order of this // object with the specified/ object o, and returns a // negative integer, zero, or a positive integer if this // object is less than, equal to, or greater than the specified // object o package java.lang; public interface Comparable { public int compareTo(Object o); }

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interface Comparable // Max.java: Find the maximum object of the //two objects. Note to use this method, you // must first implement the Comparable interface for // the class of these two objects. public class Max { /** Return the maximum between two objects */ public static Comparable max(Comparable o1, Comparable o2) { if (o1.compareTo(o2) > 0) return o1; else return o2; }

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. The Cloneable Interface –Marker Interface An interface contains constants and abstract methods, but the Cloneable interface is a special case: public interface Cloneable { } This interface is empty. An interface with an empty body is known as a marker interface.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. TestEdible (Driver) Animal ElephantTigerChicken > Fruit AppleCherry Orange

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Programs to Use Programs are stored on the M: drive in the Interface folder : TestEdible (Driver) Edible (an interface) Animal (super class) Chicken Tiger Elephant Fruit (super class) Apple Orange M:\Courses\OCP\CIS Classes\CIS 225 Advanced Java\FordeFolder\Interface Folder

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces vs. Abstract Classes InterfaceAbstract Constant Data Yes(only kind) Yes All Types of Data NoYes Abstract Methods Yes (only)Yes (at least one) Concrete Methods NoYes