Lecture 5: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

Chapter 14 Abstract Classes and Interfaces 1. Objectives To design and use abstract classes (§14.2). To process a calendar using the Calendar and GregorianCalendar.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 10 Abstract Classes.
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.
9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.
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.
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
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,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
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.
1 Chapter 5 Abstract Classes and Interfaces. 2 Objectives u To design and use abstract classes. u To process a calendar using the Calendar and GregorianCalendar.
Lecture 6: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
Interfaces Professor Evan Korth.
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Interface.
Week 6 Object-Oriented Programming (2): Polymorphism
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 5:Interfaces and Abstract Classes Michael Hsu CSULA

Required Reading:  Oracle tutorial on Interfaces and Inheritrance: 

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 of 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); }

6 6 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.  By convention, the implements clause follows the extends clause, if there is one.

8 8 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).

9 9 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.

10 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.

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

13 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.

14 Integer and BigInteger Classes String and Date Classes

15 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));

16 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.

17 Defining Classes to Implement Comparable

18 Abstract Classes and Abstract Methods

19 abstract method in abstract class An abstract method cannot be contained in a nonabstract 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 nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass.

20 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.

21 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.

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

23 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.

24 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