Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

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 Inheritance Reserved word protected Reserved word super
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.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
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.
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.
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.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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.
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 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
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.
1 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, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Abstract Classes.
CS 112 Programming 2 Lecture 10 Abstract Classes & Interfaces (1)
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
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.
1 1 Chapter 14 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code.
Lecture 5:Interfaces and Abstract Classes
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Chapter 15 Abstract Classes and Interfaces
Chapter 11 Abstract Classes and Interfaces
Inheritance and Polymorphism
IST311 / 602 Cleveland State University – Prof. Victor Matos
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Interface.
Interfaces.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Chapter 12 Abstract Classes and Interfaces
Advanced Java Programming
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
IST311 / 602 Cleveland State University – Prof. Victor Matos
Chapter 8 Class Inheritance and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 13 Abstract Classes and Interfaces Part 01
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA

Midterm  Thursday, 5/5/2016  20% of your grade  Covers material from lectures 2 – 6 (includes this one)  Two parts:  Part 1 (45 pts): written, closed books/notes/electronics/everything  Basic ideas from lecture slides, I will ask you write code snippets.  Bring your own pen, I will provide scratch paper  Part 2 (55 pts): Programming  Open everything except no communications with others  Turn in part 1 first, then work on part 2  You have the entire class to work on both parts

Reading for this week’s lecture:  Oracle tutorial on Interfaces and Inheritance:   MIT OpenCourseware:  introduction-to-computers-and-engineering-problem-solving-spring- 2012/recitations/MIT1_00S12_REC_6.pdf introduction-to-computers-and-engineering-problem-solving-spring- 2012/recitations/MIT1_00S12_REC_6.pdf  University of Western Ontario:  12/cs1025a/Notes/CS1025%2014%20--%20Intro%20to%20Java%20V%20-- %20abstract%20classes%20and%20interfaces.pdf 12/cs1025a/Notes/CS1025%2014%20--%20Intro%20to%20Java%20V%20-- %20abstract%20classes%20and%20interfaces.pdf

Why Interfaces?  Suppose you have a team of 400 software developers working on the same project, how do you ensure that each person’s code would work with each other?  It’ll be very difficult for each developer to understand how the other 399 developers wrote their code in order to integrate all the different parts.  What we need is some kind of contract so that people know exactly what each other are doing  The contract should define the behavior of the code so that everybody is on the same page.

What is an interface?  A reference type, similar to a class  Contains only constants, method signatures, default methods, static methods, and nested types.  Method bodies exist only for default methods and static methods.  Cannot be instantiated—they can only be implemented by classes or extended by other interfaces.  Used for APIs (Application Programming Interface)  Hides implementation from the user

Example of an Interface public interface DriveCar { int DRIVER_NUM = 20; void turn(String direction); int signalTurn(String direction, boolean signalOn); }

7 7 Define an Interface To distinguish an interface from a class, Java uses the following syntax to define an interface: public interface InterfaceName { constant declarations; abstract method signatures; } Example : public interface Edible { /** Describe how to eat */ public abstract String howToEat(); }

Implementing an Interface  To declare a class that implements an interface, you include an implements clause in the class declaration.  Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. This is java’s way of providing “multiple inheritance”  By convention, the implements clause follows the extends clause, if there is one.

Problem of Multiple Inheritance  Both B and C overrides m1  D inherits from both B and C  When you call D.m1(), which method implementation does it use?

10 Omitting Modifiers in Interfaces All data fields are public final static and all methods are public abstract in an interface. For this reason, these modifiers can be omitted, as shown below: A constant defined in an interface can be accessed using syntax InterfaceName.CONSTANT_NAME (e.g., T1.K).

11 Example You can now use the Edible interface to specify whether an object is edible. This is accomplished by letting the class for the object implement this interface using the implements keyword. For example, the classes Chicken and Fruit implement the Edible interface.

12 Interface is a Special Class  An interface is treated like a special class in Java. Each interface is compiled into a separate bytecode file, just like a regular class.  You cannot create an instance from an interface using the new operator, but in most cases you can use an interface more or less the same way you use an abstract class.  For example, you can use an interface as a data type for a variable, as the result of casting, ArrayList types, and so on.

Default Methods and Static Methods  A Default Method allows interfaces to specify method body without the client having to implement it.  Allows you to add functionality to interfaces without breaking other people’s code  Static methods are shared between all the client classes.

14 Example: The Comparable Interface // This interface is defined in // java.lang package package java.lang; public interface Comparable { public int compareTo(E o); }

15 The toString, equals, and hashCode Methods Each wrapper class overrides the toString, equals, and hashCode methods defined in the Object class. Since all the numeric wrapper classes and the Character class implement the Comparable interface, the compareTo method is implemented in these classes.

16 Integer and BigInteger Classes String and Date Classes

17 Example 1 System.out.println(new Integer(3).compareTo(new Integer(5))); 2 System.out.println("ABC".compareTo("ABE")); 3 java.util.Date date1 = new java.util.Date(2013, 1, 1); 4 java.util.Date date2 = new java.util.Date(2012, 1, 1); 5 System.out.println(date1.compareTo(date2));

18 Generic sort Method Let n be an Integer object, s be a String object, and d be a Date object. All the following expressions are true. The java.util.Arrays.sort(array) method requires that the elements in an array are instances of Comparable.

19 Defining Classes to Implement Comparable

20 Abstract Classes and Abstract Methods

abstract method in abstract class  An abstract method cannot be contained in a non-abstract class.  If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined abstract.  In other words, in a non-abstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass. 21

object cannot be created from abstract class  An abstract class cannot be instantiated using the new operator, but you can still define its constructors, which are invoked in the constructors of its subclasses.  For instance, the constructors of GeometricObject are invoked in the Circle class and the Rectangle class. 22

abstract class without abstract method  A class that contains abstract methods must be abstract.  However, it is possible to define an abstract class that contains no abstract methods.  In this case, you cannot create instances of the class using the new operator. This class is used as a base class for defining a new subclass. 23

superclass of abstract class may be concrete  A subclass can be abstract even if its superclass is concrete.  For example, the java.lang.Object class is concrete, but its subclasses, such as GeometricObject, may be abstract. 24

concrete method overridden to be abstract  A subclass can override a method from its superclass to define it abstract. This is rare, but useful when the implementation of the method in the superclass becomes invalid in the subclass. In this case, the subclass must be defined abstract. 25

26 abstract class as type You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. Therefore, the following statement, which creates an array whose elements are of GeometricObject type, is correct. GeometricObject[] geo = new GeometricObject[10];

The Java Collections Framework: Common Classes