Chapter 13 Abstract Classes and Interfaces Part 01

Slides:



Advertisements
Similar presentations
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.
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.
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 interfaces Professor Evan Korth. review Based on the GeometricObject -> Circle -> Cylinder hierarchy from last class, which of these are legal? GeometricObject.
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.
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.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
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, 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 6 Interfaces. Class Status CU will be close for winter break from Dec. 22. till Jan.2 We have 3 classes left after tonight (Jan 8,15, and 22)
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.
1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface.
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 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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
1 P ART 4 A BSTRACT C LASSES AND I NTERFACES, W RAPPER C LASSES, AND H ANDLING E XCEPTIONS 1.
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
Abstract classes and interfaces
Chapter 15 Abstract Classes and Interfaces
Abstract Classes and Interfaces in Java Reference: COS240 Syllabus
Chapter 11 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
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
Chapter 9 Inheritance and Polymorphism
Chapter 19 Generics Dr. Clincy - Lecture.
Interface.
Chapter 12 Abstract Classes and Interfaces
Advanced Java Programming
Java Inheritance.
CMSC 202 Interfaces.
Chapter 14 Abstract Classes and Interfaces
IST311 / 602 Cleveland State University – Prof. Victor Matos
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Chapter 13 Abstract Classes and Interfaces Part 01

Abstract Classes & Abstract Methods An abstract class is a class that is declared abstract Abstract classes cannot be instantiated, but they can be subclassed Abstract methods are declared when their implementations are dependent on the subclasses where they are invoked, e.g. getArea() in GeometricObject Abstract methods are non-static methods that are declared without implementations (without {}, but followed by a ;): public abstract class Account { // declare data fields // declare concrete methods abstract void check(double balance); } 2

Why do we use Abstract Classes ? The purpose of an abstract class is to function as a base for subclasses. Inheritance child can have only one base abstract class

Abstract Classes & Abstract Methods GeometricObject Circle Rectangle TestGeometricObject 4 Run

Abstract Classes & Abstract Methods In a concrete subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined abstract An abstract class may contain just concrete methods, or just abstract methods, or both or none! A concrete class cannot contain abstract methods 5

Abstract Classes 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 We cannot create an instance from an abstract class using the new operator, but: We can include constructors (to be used by subclasses) in an abstract class An abstract class can be used as a data type. For example, the following statement, which creates an array whose elements are of abstract class GeometricObject type, is correct GeometricObject[] geo = new GeometricObject[10]; 6

Case Study: Abstract Number Class LargestNumbers Run 7

Source: https://docs. oracle. com/javase/7/docs/api/index. html

Abstract Calendar Class & its Concrete GregorianCalendar Subclass 9

Source: https://docs. oracle. com/javase/7/docs/api/index. html

Abstract Calendar Class & its Concrete GregorianCalendar Subclass An instance of java.util.Date represents a specific instant in time with millisecond precision java.util.Calendar is an abstract base class for extracting detailed information such as year, month, date, hour, minute and second from a Date object Subclasses of Calendar can implement specific calendar systems such as Gregorian calendar, Lunar Calendar and Jewish calendar Currently, java.util.GregorianCalendar for the Gregorian calendar is supported in the Java API 11

The GregorianCalendar Concrete Class We can use new GregorianCalendar() to construct a default GregorianCalendar object with the current time We can also use new GregorianCalendar(year, month, date) to construct a GregorianCalendar object with the specified year, month, and date The month parameter is 0-based, i.e., 0 is for January 12

get() in Calendar Class get(int field) is used to extract detailed information from Calendar objects. The possible fields are: TestCalendar Run 13

Interfaces The Apple & Chicken classes are based on different superclasses, but both share a common behavior: both are edible! We can model this common behavior using an interface Interfaces can be used to specify common behavior for objects of unrelated classes as well as related classes Abstract classes model the common behavior of related classes only 14

interface & class Similarities An interface is treated like a special class in Java Each interface is compiled into a separate bytecode file, just like a regular class Like an abstract class, we cannot create an instance from an interface using the new operator, but in most cases we can use an interface more or less the same way we use an abstract class For example, we can use an interface as a data type and cast a variable of an interface type to its subclass, and vice versa 15

Defining an interface Example: An interface is a class-like construct that contains only constants and abstract methods public interface InterfaceName { constant declarations; abstract method signatures; } Example: public interface Edible { /** Describe how to eat */ public abstract String howToEat(); } 16

interface inheritance We can use the Edible interface to specify whether an object is edible. This is accomplished by letting the class for the object implement this interface (or inherit the interface) using the implements keyword Example: Chicken & Fruit implement the Edible interface in TestEdible Edible TestEdible Run 17

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) 18

Example: The Comparable Interface This interface defines compareTo() for comparing objects compareTo() 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 o package java.lang; public interface Comparable<E> { public int compareTo(E o); } 19

compareTo() in Integer, BigInteger, String & Date All numeric wrapper classes, Character and String implement Comparable, thus compareTo() is implemented in all of them 20

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

The Comparable Interface Let n be an Integer object, s be a String object and d be a Date object. Then, all the following expressions are true n instanceof Integer n instanceof Object n instanceof Comparable   s instanceof String s instanceof Object s instanceof Comparable   d instanceof java.util.Date d instanceof Object d instanceof Comparable   22